annotate Objects/stringobject.c @ 32305:13656d58d450 legacy-trunk

Patch #774665: Make Python LC_NUMERIC agnostic.
author Martin v. Löwis <martin@v.loewis.de>
date Tue, 08 Jun 2004 18:52:54 +0000
parents eaa1a4b74328
children 64534fb3afcd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* String object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
5 #include <ctype.h>
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
6
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
7 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
8 int null_strings, one_strings;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
9 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
10
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
11 static PyStringObject *characters[UCHAR_MAX + 1];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
12 static PyStringObject *nullstring;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
13
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
14 /* This dictionary holds all interned strings. Note that references to
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
15 strings in this dictionary are *not* counted in the string's ob_refcnt.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
16 When the interned string reaches a refcnt of 0 the string deallocation
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
17 function will delete the reference from this dictionary.
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
18
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
19 Another way to look at this is that to say that the actual reference
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
20 count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
21 */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
22 static PyObject *interned;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
23
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
24
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
25 /*
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
26 For both PyString_FromString() and PyString_FromStringAndSize(), the
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
27 parameter `size' denotes number of characters to allocate, not counting any
21737
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
28 null terminating character.
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
29
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
30 For PyString_FromString(), the parameter `str' points to a null-terminated
21737
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
31 string containing exactly `size' bytes.
30a817038af0 Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents: 21327
diff changeset
32
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
33 For PyString_FromStringAndSize(), the parameter the parameter `str' is
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
34 either NULL or else points to a string containing at least `size' bytes.
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
35 For PyString_FromStringAndSize(), the string in the `str' parameter does
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
36 not have to be null-terminated. (Therefore it is safe to construct a
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
37 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.)
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
38 If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1'
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
39 bytes (setting the last byte to the null terminating character) and you can
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
40 fill in the data yourself. If `str' is non-NULL then the resulting
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
41 PyString object must be treated as immutable and you must not fill in nor
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
42 alter the data yourself, since the strings may be shared.
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
43
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
44 The PyObject member `op->ob_size', which denotes the number of "extra
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
45 items" in a variable-size object, will contain the number of bytes
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
46 allocated for string data, not counting the null terminating character. It
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
47 is therefore equal to the equal to the `size' parameter (for
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
48 PyString_FromStringAndSize()) or the length of the string in the `str'
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
49 parameter (for PyString_FromString()).
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
50 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
51 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
52 PyString_FromStringAndSize(const char *str, int size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 {
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
54 register PyStringObject *op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
55 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
56 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
57 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
58 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
59 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
60 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
61 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
62 if (size == 1 && str != NULL &&
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
63 (op = characters[*str & UCHAR_MAX]) != NULL)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
64 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
65 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
66 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
67 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
68 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
69 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
70 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
71
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24872
diff changeset
72 /* Inline PyObject_NewVar */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
73 op = (PyStringObject *)
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
74 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
75 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
76 return PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
77 PyObject_INIT_VAR(op, &PyString_Type, size);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 op->ob_shash = -1;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
79 op->ob_sstate = SSTATE_NOT_INTERNED;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
80 if (str != NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
81 memcpy(op->ob_sval, str, size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
82 op->ob_sval[size] = '\0';
22689
614167df2096 Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents: 22651
diff changeset
83 /* share short strings */
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
84 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
85 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
86 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
87 op = (PyStringObject *)t;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 nullstring = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
89 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
90 } 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
91 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
92 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
93 op = (PyStringObject *)t;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
94 characters[*str & UCHAR_MAX] = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
95 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
96 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
97 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
100 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
101 PyString_FromString(const char *str)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 {
21263
2f2abeeefd87 PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21254
diff changeset
103 register size_t size;
17720
ede29ad6440a Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents: 17712
diff changeset
104 register PyStringObject *op;
21263
2f2abeeefd87 PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21254
diff changeset
105
2f2abeeefd87 PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21254
diff changeset
106 assert(str != NULL);
2f2abeeefd87 PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents: 21254
diff changeset
107 size = strlen(str);
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
108 if (size > INT_MAX) {
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
109 PyErr_SetString(PyExc_OverflowError,
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
110 "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
111 return NULL;
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
112 }
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
113 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
114 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
115 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
116 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
117 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
118 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
119 }
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
120 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
121 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
122 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
123 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
124 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
125 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
126 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
127
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24872
diff changeset
128 /* Inline PyObject_NewVar */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
129 op = (PyStringObject *)
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
130 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
131 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
132 return PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
133 PyObject_INIT_VAR(op, &PyString_Type, size);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
134 op->ob_shash = -1;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
135 op->ob_sstate = SSTATE_NOT_INTERNED;
21327
34a1a50e6ab6 SF patch #491049 (David Jacobs): Small PyString_FromString optimization
Guido van Rossum <guido@python.org>
parents: 21263
diff changeset
136 memcpy(op->ob_sval, str, size+1);
22689
614167df2096 Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents: 22651
diff changeset
137 /* share short strings */
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
138 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
139 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
140 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
141 op = (PyStringObject *)t;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
142 nullstring = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
143 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
144 } 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
145 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
146 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
147 op = (PyStringObject *)t;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
148 characters[*str & UCHAR_MAX] = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
149 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
150 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
151 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
154 PyObject *
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
155 PyString_FromFormatV(const char *format, va_list vargs)
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
156 {
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
157 va_list count;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
158 int n = 0;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
159 const char* f;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
160 char *s;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
161 PyObject* string;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
162
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
163 #ifdef VA_LIST_IS_ARRAY
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
164 memcpy(count, vargs, sizeof(va_list));
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
165 #else
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
166 #ifdef __va_copy
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
167 __va_copy(count, vargs);
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
168 #else
20221
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
169 count = vargs;
abaf9ea2e560 SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents: 20140
diff changeset
170 #endif
24400
81ca6ca5c307 Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 24280
diff changeset
171 #endif
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
172 /* step 1: figure out how large a buffer we need */
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
173 for (f = format; *f; f++) {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
174 if (*f == '%') {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
175 const char* p = f;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
176 while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
177 ;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
178
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
179 /* skip the 'l' in %ld, since it doesn't change the
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
180 width. although only %d is supported (see
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
181 "expand" section below), others can be easily
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
182 added */
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
183 if (*f == 'l' && *(f+1) == 'd')
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
184 ++f;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
185
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
186 switch (*f) {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
187 case 'c':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
188 (void)va_arg(count, int);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
189 /* fall through... */
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
190 case '%':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
191 n++;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
192 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
193 case 'd': case 'i': case 'x':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
194 (void) va_arg(count, int);
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
195 /* 20 bytes is enough to hold a 64-bit
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
196 integer. Decimal takes the most space.
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
197 This isn't enough for octal. */
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
198 n += 20;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
199 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
200 case 's':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
201 s = va_arg(count, char*);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
202 n += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
203 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
204 case 'p':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
205 (void) va_arg(count, int);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
206 /* maximum 64-bit pointer representation:
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
207 * 0xffffffffffffffff
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
208 * so 19 characters is enough.
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
209 * XXX I count 18 -- what's the extra for?
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
210 */
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
211 n += 19;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
212 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
213 default:
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
214 /* if we stumble upon an unknown
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
215 formatting code, copy the rest of
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
216 the format string to the output
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
217 string. (we cannot just skip the
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
218 code, since there's no way to know
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
219 what's in the argument list) */
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
220 n += strlen(p);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
221 goto expand;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
222 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
223 } else
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
224 n++;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
225 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
226 expand:
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
227 /* step 2: fill the buffer */
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
228 /* Since we've analyzed how much space we need for the worst case,
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
229 use sprintf directly instead of the slower PyOS_snprintf. */
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
230 string = PyString_FromStringAndSize(NULL, n);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
231 if (!string)
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
232 return NULL;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
233
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
234 s = PyString_AsString(string);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
235
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
236 for (f = format; *f; f++) {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
237 if (*f == '%') {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
238 const char* p = f++;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
239 int i, longflag = 0;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
240 /* parse the width.precision part (we're only
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
241 interested in the precision value, if any) */
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
242 n = 0;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
243 while (isdigit(Py_CHARMASK(*f)))
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
244 n = (n*10) + *f++ - '0';
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
245 if (*f == '.') {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
246 f++;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
247 n = 0;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
248 while (isdigit(Py_CHARMASK(*f)))
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
249 n = (n*10) + *f++ - '0';
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
250 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
251 while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
252 f++;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
253 /* handle the long flag, but only for %ld. others
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
254 can be added when necessary. */
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
255 if (*f == 'l' && *(f+1) == 'd') {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
256 longflag = 1;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
257 ++f;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
258 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
259
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
260 switch (*f) {
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
261 case 'c':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
262 *s++ = va_arg(vargs, int);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
263 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
264 case 'd':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
265 if (longflag)
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
266 sprintf(s, "%ld", va_arg(vargs, long));
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
267 else
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
268 sprintf(s, "%d", va_arg(vargs, int));
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
269 s += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
270 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
271 case 'i':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
272 sprintf(s, "%i", va_arg(vargs, int));
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
273 s += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
274 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
275 case 'x':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
276 sprintf(s, "%x", va_arg(vargs, int));
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
277 s += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
278 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
279 case 's':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
280 p = va_arg(vargs, char*);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
281 i = strlen(p);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
282 if (n > 0 && i > n)
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
283 i = n;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
284 memcpy(s, p, i);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
285 s += i;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
286 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
287 case 'p':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
288 sprintf(s, "%p", va_arg(vargs, void*));
19486
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
289 /* %p is ill-defined: ensure leading 0x. */
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
290 if (s[1] == 'X')
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
291 s[1] = 'x';
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
292 else if (s[1] != 'x') {
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
293 memmove(s+2, s, strlen(s)+1);
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
294 s[0] = '0';
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
295 s[1] = 'x';
5c859d2cb37b PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents: 19475
diff changeset
296 }
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
297 s += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
298 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
299 case '%':
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
300 *s++ = '%';
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
301 break;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
302 default:
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
303 strcpy(s, p);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
304 s += strlen(s);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
305 goto end;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
306 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
307 } else
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
308 *s++ = *f;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
309 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
310
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
311 end:
19489
3e74318a1b76 Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents: 19486
diff changeset
312 _PyString_Resize(&string, s - PyString_AS_STRING(string));
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
313 return string;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
314 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
315
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
316 PyObject *
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
317 PyString_FromFormat(const char *format, ...)
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
318 {
19489
3e74318a1b76 Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents: 19486
diff changeset
319 PyObject* ret;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
320 va_list vargs;
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
321
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
322 #ifdef HAVE_STDARG_PROTOTYPES
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
323 va_start(vargs, format);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
324 #else
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
325 va_start(vargs);
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
326 #endif
19489
3e74318a1b76 Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents: 19486
diff changeset
327 ret = PyString_FromFormatV(format, vargs);
3e74318a1b76 Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents: 19486
diff changeset
328 va_end(vargs);
3e74318a1b76 Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents: 19486
diff changeset
329 return ret;
19475
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
330 }
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
331
bc63afc701ef PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
332
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
333 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
334 int size,
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
335 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
336 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
337 {
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
338 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
339
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
340 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
341 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
342 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
343 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
344 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
345 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
346 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
347
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
348 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
349 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
350 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
351 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
352 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
353
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
354 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
355 PyErr_BadArgument();
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
356 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
357 }
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
358
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
359 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
360 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
361 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
362 #else
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
363 PyErr_SetString(PyExc_ValueError, "no encoding specified");
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
364 goto onError;
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
365 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
366 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
367
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
368 /* 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
369 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
370 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
371 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
372
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
373 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
374
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
375 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
376 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
377 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
378
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
379 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
380 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
381 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
382 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
383 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
384
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
385 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
386 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
387 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
388
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
389 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
390 /* 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
391 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
392 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
393 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
394 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
395 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
396 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
397 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
398 #endif
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
399 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
400 PyErr_Format(PyExc_TypeError,
13561
ea732719b3fd Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents: 13544
diff changeset
401 "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
402 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
403 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
404 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
405 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
406
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
407 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
408
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
409 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
410 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
411 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
412
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
413 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
414 int size,
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
415 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
416 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
417 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
418 PyObject *v, *str;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
419
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
420 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
421 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
422 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
423 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
424 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
425 return v;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
426 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
427
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
428 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
429 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
430 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
431 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
432 PyObject *v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
433
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
434 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
435 PyErr_BadArgument();
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
436 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
437 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
438
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
439 if (encoding == NULL) {
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
440 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
441 encoding = PyUnicode_GetDefaultEncoding();
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
442 #else
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
443 PyErr_SetString(PyExc_ValueError, "no encoding specified");
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
444 goto onError;
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
445 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
446 }
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
447
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
448 /* 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
449 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
450 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
451 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
452
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
453 return v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
454
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
455 onError:
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
456 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
457 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
458
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
459 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
460 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
461 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
462 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
463 PyObject *v;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
464
18086
592109f2a6fd Fix for bug #432384: Recursion in PyString_AsEncodedString?
Marc-André Lemburg <mal@egenix.com>
parents: 17964
diff changeset
465 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
466 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
467 goto onError;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
468
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
469 #ifdef Py_USING_UNICODE
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
470 /* 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
471 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
472 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
473 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
474 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
475 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
476 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
477 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
478 #endif
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
479 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
480 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
481 "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
482 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
483 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
484 goto onError;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
485 }
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
486
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
487 return v;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
488
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
489 onError:
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
490 return NULL;
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
491 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
492
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
493 static void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
494 string_dealloc(PyObject *op)
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
495 {
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
496 switch (PyString_CHECK_INTERNED(op)) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
497 case SSTATE_NOT_INTERNED:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
498 break;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
499
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
500 case SSTATE_INTERNED_MORTAL:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
501 /* revive dead object temporarily for DelItem */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
502 op->ob_refcnt = 3;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
503 if (PyDict_DelItem(interned, op) != 0)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
504 Py_FatalError(
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
505 "deletion of interned string failed");
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
506 break;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
507
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
508 case SSTATE_INTERNED_IMMORTAL:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
509 Py_FatalError("Immortal interned string died.");
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
510
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
511 default:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
512 Py_FatalError("Inconsistent interned string state.");
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
513 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20221
diff changeset
514 op->ob_type->tp_free(op);
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
515 }
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
516
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
517 /* Unescape a backslash-escaped string. If unicode is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
518 the string is a u-literal. If recode_encoding is non-zero,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
519 the string is UTF-8 encoded and should be re-encoded in the
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
520 specified encoding. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
521
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
522 PyObject *PyString_DecodeEscape(const char *s,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
523 int len,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
524 const char *errors,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
525 int unicode,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
526 const char *recode_encoding)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
527 {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
528 int c;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
529 char *p, *buf;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
530 const char *end;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
531 PyObject *v;
25100
bc590beadf0b Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
532 int newlen = recode_encoding ? 4*len:len;
bc590beadf0b Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
533 v = PyString_FromStringAndSize((char *)NULL, newlen);
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
534 if (v == NULL)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
535 return NULL;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
536 p = buf = PyString_AsString(v);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
537 end = s + len;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
538 while (s < end) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
539 if (*s != '\\') {
25173
f4e9f09df4f2 Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents: 25100
diff changeset
540 non_esc:
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
541 #ifdef Py_USING_UNICODE
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
542 if (recode_encoding && (*s & 0x80)) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
543 PyObject *u, *w;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
544 char *r;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
545 const char* t;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
546 int rn;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
547 t = s;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
548 /* Decode non-ASCII bytes as UTF-8. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
549 while (t < end && (*t & 0x80)) t++;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
550 u = PyUnicode_DecodeUTF8(s, t - s, errors);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
551 if(!u) goto failed;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
552
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
553 /* Recode them in target encoding. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
554 w = PyUnicode_AsEncodedString(
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
555 u, recode_encoding, errors);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
556 Py_DECREF(u);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
557 if (!w) goto failed;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
558
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
559 /* Append bytes to output buffer. */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
560 r = PyString_AsString(w);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
561 rn = PyString_Size(w);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
562 memcpy(p, r, rn);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
563 p += rn;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
564 Py_DECREF(w);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
565 s = t;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
566 } else {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
567 *p++ = *s++;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
568 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
569 #else
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
570 *p++ = *s++;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
571 #endif
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
572 continue;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
573 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
574 s++;
24809
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
575 if (s==end) {
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
576 PyErr_SetString(PyExc_ValueError,
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
577 "Trailing \\ in string");
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
578 goto failed;
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
579 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
580 switch (*s++) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
581 /* XXX This assumes ASCII! */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
582 case '\n': break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
583 case '\\': *p++ = '\\'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
584 case '\'': *p++ = '\''; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
585 case '\"': *p++ = '\"'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
586 case 'b': *p++ = '\b'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
587 case 'f': *p++ = '\014'; break; /* FF */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
588 case 't': *p++ = '\t'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
589 case 'n': *p++ = '\n'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
590 case 'r': *p++ = '\r'; break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
591 case 'v': *p++ = '\013'; break; /* VT */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
592 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
593 case '0': case '1': case '2': case '3':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
594 case '4': case '5': case '6': case '7':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
595 c = s[-1] - '0';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
596 if ('0' <= *s && *s <= '7') {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
597 c = (c<<3) + *s++ - '0';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
598 if ('0' <= *s && *s <= '7')
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
599 c = (c<<3) + *s++ - '0';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
600 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
601 *p++ = c;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
602 break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
603 case 'x':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
604 if (isxdigit(Py_CHARMASK(s[0]))
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
605 && isxdigit(Py_CHARMASK(s[1]))) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
606 unsigned int x = 0;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
607 c = Py_CHARMASK(*s);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
608 s++;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
609 if (isdigit(c))
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
610 x = c - '0';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
611 else if (islower(c))
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
612 x = 10 + c - 'a';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
613 else
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
614 x = 10 + c - 'A';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
615 x = x << 4;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
616 c = Py_CHARMASK(*s);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
617 s++;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
618 if (isdigit(c))
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
619 x += c - '0';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
620 else if (islower(c))
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
621 x += 10 + c - 'a';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
622 else
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
623 x += 10 + c - 'A';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
624 *p++ = x;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
625 break;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
626 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
627 if (!errors || strcmp(errors, "strict") == 0) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
628 PyErr_SetString(PyExc_ValueError,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
629 "invalid \\x escape");
24809
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
630 goto failed;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
631 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
632 if (strcmp(errors, "replace") == 0) {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
633 *p++ = '?';
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
634 } else if (strcmp(errors, "ignore") == 0)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
635 /* do nothing */;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
636 else {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
637 PyErr_Format(PyExc_ValueError,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
638 "decoding error; "
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
639 "unknown error handling code: %.400s",
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
640 errors);
24809
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
641 goto failed;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
642 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
643 #ifndef Py_USING_UNICODE
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
644 case 'u':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
645 case 'U':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
646 case 'N':
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
647 if (unicode) {
24872
cf561e612378 Get this to compile again if Py_USING_UNICODE is not defined.
Neal Norwitz <nnorwitz@gmail.com>
parents: 24831
diff changeset
648 PyErr_SetString(PyExc_ValueError,
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
649 "Unicode escapes not legal "
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
650 "when Unicode disabled");
24809
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
651 goto failed;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
652 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
653 #endif
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
654 default:
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
655 *p++ = '\\';
25173
f4e9f09df4f2 Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents: 25100
diff changeset
656 s--;
f4e9f09df4f2 Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents: 25100
diff changeset
657 goto non_esc; /* an arbitry number of unescaped
f4e9f09df4f2 Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents: 25100
diff changeset
658 UTF-8 bytes may follow. */
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
659 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
660 }
25100
bc590beadf0b Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
661 if (p-buf < newlen)
bc590beadf0b Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents: 25091
diff changeset
662 _PyString_Resize(&v, (int)(p - buf));
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
663 return v;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
664 failed:
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
665 Py_DECREF(v);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
666 return NULL;
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
667 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
668
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
669 static int
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
670 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
671 {
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
672 char *s;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
673 int len;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
674 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
675 return -1;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
676 return len;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
677 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
678
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
679 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
680 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
681 {
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
682 char *s;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
683 int len;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
684 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
685 return NULL;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
686 return s;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
687 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
688
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1908
diff changeset
689 int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
690 PyString_Size(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 {
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
692 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
693 return string_getsize(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
694 return ((PyStringObject *)op) -> ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 /*const*/ char *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
698 PyString_AsString(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 {
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
700 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
701 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
702 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
703 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
704
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
705 int
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
706 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
707 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
708 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
709 {
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
710 if (s == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
711 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
712 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 }
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
714
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
715 if (!PyString_Check(obj)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
716 #ifdef Py_USING_UNICODE
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
717 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
718 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
719 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
720 return -1;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
721 }
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
722 else
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
723 #endif
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
724 {
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
725 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
726 "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
727 "%.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
728 return -1;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
729 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
730 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
731
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
732 *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
733 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
734 *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
735 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
736 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
737 "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
738 return -1;
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
739 }
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14492
diff changeset
740 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
742
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
745 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
746 string_print(PyStringObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 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
750 int quote;
20464
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
751
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
752 /* XXX Ought to check for interrupts when writing long strings */
20464
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
753 if (! PyString_CheckExact(op)) {
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
754 int ret;
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
755 /* A str subclass may have its own __str__ method. */
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
756 op = (PyStringObject *) PyObject_Str((PyObject *)op);
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
757 if (op == NULL)
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
758 return -1;
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
759 ret = string_print(op, fp, flags);
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
760 Py_DECREF(op);
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
761 return ret;
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
762 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
763 if (flags & Py_PRINT_RAW) {
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
764 #ifdef __VMS
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
765 if (op->ob_size) fwrite(op->ob_sval, (int) op->ob_size, 1, fp);
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
766 #else
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
767 fwrite(op->ob_sval, 1, (int) op->ob_size, fp);
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26273
diff changeset
768 #endif
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
769 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
771
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
772 /* 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
773 quote = '\'';
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
774 if (memchr(op->ob_sval, '\'', op->ob_size) &&
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
775 !memchr(op->ob_sval, '"', op->ob_size))
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
776 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
777
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
778 fputc(quote, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 for (i = 0; i < op->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 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
781 if (c == quote || c == '\\')
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
782 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
783 else if (c == '\t')
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
784 fprintf(fp, "\\t");
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16014
diff changeset
785 else if (c == '\n')
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
786 fprintf(fp, "\\n");
16226
c7c404b35f33 Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents: 16014
diff changeset
787 else if (c == '\r')
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
788 fprintf(fp, "\\r");
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
789 else if (c < ' ' || c >= 0x7f)
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
790 fprintf(fp, "\\x%02x", c & 0xff);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 else
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
792 fputc(c, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
794 fputc(quote, fp);
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
795 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
797
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
798 PyObject *
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
799 PyString_Repr(PyObject *obj, int smartquotes)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 {
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
801 register PyStringObject* op = (PyStringObject*) obj;
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
802 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
803 PyObject *v;
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
804 if (newsize > INT_MAX) {
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
805 PyErr_SetString(PyExc_OverflowError,
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
806 "string is too large to make repr");
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
807 }
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
808 v = PyString_FromStringAndSize((char *)NULL, newsize);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 if (v == NULL) {
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
810 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 else {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 register char c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 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
816 int quote;
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
817
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
818 /* 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
819 quote = '\'';
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
820 if (smartquotes &&
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
821 memchr(op->ob_sval, '\'', op->ob_size) &&
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
822 !memchr(op->ob_sval, '"', op->ob_size))
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
823 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
824
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
825 p = PyString_AS_STRING(v);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
826 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 for (i = 0; i < op->ob_size; i++) {
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
828 /* There's at least enough room for a hex escape
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
829 and a closing quote. */
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
830 assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 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
832 if (c == quote || c == '\\')
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
833 *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
834 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
835 *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
836 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
837 *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
838 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
839 *p++ = '\\', *p++ = 'r';
25721
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
840 else if (c < ' ' || c >= 0x7f) {
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
841 /* For performance, we don't want to call
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
842 PyOS_snprintf here (extra layers of
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
843 function call). */
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
844 sprintf(p, "\\x%02x", c & 0xff);
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
845 p += 4;
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
846 }
8b20b26f1186 Back out #479898.
Martin v. Löwis <martin@v.loewis.de>
parents: 25719
diff changeset
847 else
25643
5c6fac95a94b Patch #479898: Use multibyte C library for printing strings if available.
Martin v. Löwis <martin@v.loewis.de>
parents: 25233
diff changeset
848 *p++ = c;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 }
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
850 assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
851 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 *p = '\0';
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
853 _PyString_Resize(
21147
90a0d80033b4 PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents: 21138
diff changeset
854 &v, (int) (p - PyString_AS_STRING(v)));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
855 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
858
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
859 static PyObject *
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
860 string_repr(PyObject *op)
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
861 {
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
862 return PyString_Repr(op, 1);
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
863 }
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
864
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24736
diff changeset
865 static PyObject *
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
866 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
867 {
20464
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
868 assert(PyString_Check(s));
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
869 if (PyString_CheckExact(s)) {
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
870 Py_INCREF(s);
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
871 return s;
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
872 }
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
873 else {
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
874 /* Subtype -- return genuine string with the same value. */
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
875 PyStringObject *t = (PyStringObject *) s;
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
876 return PyString_FromStringAndSize(t->ob_sval, t->ob_size);
98df0115ecf9 SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents: 20415
diff changeset
877 }
17645
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
878 }
b5f583eecb20 Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents: 17640
diff changeset
879
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
881 string_length(PyStringObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
883 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
885
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
886 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
887 string_concat(register PyStringObject *a, register PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 register unsigned int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
890 register PyStringObject *op;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
891 if (!PyString_Check(bb)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
892 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
893 if (PyUnicode_Check(bb))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
894 return PyUnicode_Concat((PyObject *)a, bb);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
895 #endif
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
896 PyErr_Format(PyExc_TypeError,
20604
7945639df265 Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
897 "cannot concatenate 'str' and '%.200s' objects",
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12615
diff changeset
898 bb->ob_type->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
901 #define b ((PyStringObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
902 /* Optimize cases with empty left or right operand */
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
903 if ((a->ob_size == 0 || b->ob_size == 0) &&
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
904 PyString_CheckExact(a) && PyString_CheckExact(b)) {
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
905 if (a->ob_size == 0) {
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
906 Py_INCREF(bb);
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
907 return bb;
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
908 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
909 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
910 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
912 size = a->ob_size + b->ob_size;
24898
a869d2de536f Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents: 24872
diff changeset
913 /* Inline PyObject_NewVar */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
914 op = (PyStringObject *)
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
915 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
916 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
917 return PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
918 PyObject_INIT_VAR(op, &PyString_Type, size);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
919 op->ob_shash = -1;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
920 op->ob_sstate = SSTATE_NOT_INTERNED;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
921 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
922 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
923 op->ob_sval[size] = '\0';
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
924 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
927
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
928 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
929 string_repeat(register PyStringObject *a, register int n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
930 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 register int i;
27101
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
932 register int j;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5098
diff changeset
933 register int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
934 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
935 size_t nbytes;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
936 if (n < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 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
938 /* 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
939 * 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
940 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 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
942 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
943 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
944 "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
945 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
946 }
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
947 if (size == a->ob_size && PyString_CheckExact(a)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
948 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
949 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 }
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
951 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
952 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
953 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
954 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
955 "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
956 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
957 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
958 op = (PyStringObject *)
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
959 PyObject_MALLOC(sizeof(PyStringObject) + nbytes);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
960 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
961 return PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12371
diff changeset
962 PyObject_INIT_VAR(op, &PyString_Type, size);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
963 op->ob_shash = -1;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
964 op->ob_sstate = SSTATE_NOT_INTERNED;
27123
1b881d5e4a90 GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents: 27101
diff changeset
965 op->ob_sval[size] = '\0';
1b881d5e4a90 GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents: 27101
diff changeset
966 if (a->ob_size == 1 && n > 0) {
1b881d5e4a90 GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents: 27101
diff changeset
967 memset(op->ob_sval, a->ob_sval[0] , n);
1b881d5e4a90 GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents: 27101
diff changeset
968 return (PyObject *) op;
1b881d5e4a90 GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents: 27101
diff changeset
969 }
27101
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
970 i = 0;
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
971 if (i < size) {
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
972 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
973 i = (int) a->ob_size;
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
974 }
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
975 while (i < size) {
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
976 j = (i <= size-i) ? i : size-i;
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
977 memcpy(op->ob_sval+i, op->ob_sval, j);
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
978 i += j;
6d7daa5329c6 Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents: 26825
diff changeset
979 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
980 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
982
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
984
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
985 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
986 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
987 /* j -- may be negative! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 if (i < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 i = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 if (j < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 j = 0; /* Avoid signed/unsigned bug in next line */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 if (j > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 j = a->ob_size;
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
995 if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) {
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
996 /* It's the same as a */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
997 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
998 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 if (j < i)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 j = i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1002 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1005 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1006 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
1007 {
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1008 const char *lhs, *rhs, *end;
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1009 int size;
25017
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1010
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1011 if (!PyString_CheckExact(el)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1012 #ifdef Py_USING_UNICODE
25017
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1013 if (PyUnicode_Check(el))
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1014 return PyUnicode_Contains(a, el);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1015 #endif
25017
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1016 if (!PyString_Check(el)) {
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1017 PyErr_SetString(PyExc_TypeError,
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1018 "'in <string>' requires string as left operand");
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1019 return -1;
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1020 }
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1021 }
25017
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1022 size = PyString_GET_SIZE(el);
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1023 rhs = PyString_AS_STRING(el);
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1024 lhs = PyString_AS_STRING(a);
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1025
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1026 /* optimize for a single character */
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1027 if (size == 1)
25017
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1028 return memchr(lhs, *rhs, PyString_GET_SIZE(a)) != NULL;
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1029
aa4503556339 string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents: 25004
diff changeset
1030 end = lhs + (PyString_GET_SIZE(a) - size);
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1031 while (lhs <= end) {
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1032 if (memcmp(lhs++, rhs, size) == 0)
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1033 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
1034 }
24598
12e146171f3d Committing patch #591250 which provides "str1 in str2" when str1 is a
Barry Warsaw <barry@python.org>
parents: 24561
diff changeset
1035
11942
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1036 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
1037 }
421b1620f582 Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents: 11919
diff changeset
1038
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1039 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1040 string_item(PyStringObject *a, register int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1042 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
1043 char *pchar;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1045 PyErr_SetString(PyExc_IndexError, "string index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1046 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 }
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
1048 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
1049 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
1050 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
1051 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
1052 else {
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1053 #ifdef COUNT_ALLOCS
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1054 one_strings++;
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1055 #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
1056 Py_INCREF(v);
17712
2a7cfc172a68 Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents: 17711
diff changeset
1057 }
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
1058 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1061 static PyObject*
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1062 string_richcompare(PyStringObject *a, PyStringObject *b, int op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 {
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1064 int c;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1065 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
1066 int min_len;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1067 PyObject *result;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1068
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20057
diff changeset
1069 /* Make sure both arguments are strings. */
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20057
diff changeset
1070 if (!(PyString_Check(a) && PyString_Check(b))) {
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1071 result = Py_NotImplemented;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1072 goto out;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1073 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1074 if (a == b) {
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1075 switch (op) {
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1076 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
1077 result = Py_True;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1078 goto out;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1079 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
1080 result = Py_False;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1081 goto out;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1082 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1083 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1084 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
1085 /* 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
1086 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
1087 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
1088 && (a->ob_sval[0] == b->ob_sval[0]
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
1089 && memcmp(a->ob_sval, b->ob_sval,
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1090 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
1091 result = Py_True;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1092 } else {
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1093 result = Py_False;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1094 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1095 goto out;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1096 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1097 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
1098 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
1099 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
1100 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
1101 if (c==0)
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1102 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
1103 }else
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1104 c = 0;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1105 if (c == 0)
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1106 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
1107 switch (op) {
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1108 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
1109 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
1110 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
1111 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
1112 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
1113 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
1114 default:
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1115 result = Py_NotImplemented;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1116 goto out;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1117 }
17964
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1118 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
1119 out:
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1120 Py_INCREF(result);
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1121 return result;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1122 }
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1123
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1124 int
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1125 _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
1126 {
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1127 PyStringObject *a, *b;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1128 a = (PyStringObject*)o1;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1129 b = (PyStringObject*)o2;
5e1141157d53 Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents: 17835
diff changeset
1130 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
1131 && *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
1132 && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1135 static long
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1136 string_hash(PyStringObject *a)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1137 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1138 register int len;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1139 register unsigned char *p;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1140 register long x;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1141
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1142 if (a->ob_shash != -1)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1143 return a->ob_shash;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1144 len = a->ob_size;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1145 p = (unsigned char *) a->ob_sval;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1146 x = *p << 7;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1147 while (--len >= 0)
4283
839f72610ae1 Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
1148 x = (1000003*x) ^ *p++;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1149 x ^= a->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1150 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1151 x = -2;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
1152 a->ob_shash = x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1153 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1154 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1155
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1156 static PyObject*
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1157 string_subscript(PyStringObject* self, PyObject* item)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1158 {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1159 if (PyInt_Check(item)) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1160 long i = PyInt_AS_LONG(item);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1161 if (i < 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1162 i += PyString_GET_SIZE(self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1163 return string_item(self,i);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1164 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1165 else if (PyLong_Check(item)) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1166 long i = PyLong_AsLong(item);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1167 if (i == -1 && PyErr_Occurred())
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1168 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1169 if (i < 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1170 i += PyString_GET_SIZE(self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1171 return string_item(self,i);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1172 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1173 else if (PySlice_Check(item)) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1174 int start, stop, step, slicelength, cur, i;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1175 char* source_buf;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1176 char* result_buf;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1177 PyObject* result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1178
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1179 if (PySlice_GetIndicesEx((PySliceObject*)item,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1180 PyString_GET_SIZE(self),
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1181 &start, &stop, &step, &slicelength) < 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1182 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1183 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1184
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1185 if (slicelength <= 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1186 return PyString_FromStringAndSize("", 0);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1187 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1188 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1189 source_buf = PyString_AsString((PyObject*)self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1190 result_buf = PyMem_Malloc(slicelength);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1191
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1192 for (cur = start, i = 0; i < slicelength;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1193 cur += step, i++) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1194 result_buf[i] = source_buf[cur];
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1195 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1196
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1197 result = PyString_FromStringAndSize(result_buf,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1198 slicelength);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1199 PyMem_Free(result_buf);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1200 return result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1201 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1202 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1203 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1204 PyErr_SetString(PyExc_TypeError,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1205 "string indices must be integers");
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1206 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1207 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1208 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1209
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1210 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1211 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
1212 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1213 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
1214 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
1215 "accessing non-existent string segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1216 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1217 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1218 *ptr = (void *)self->ob_sval;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1219 return self->ob_size;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1220 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1221
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1222 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1223 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
1224 {
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
1225 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
1226 "Cannot use string as modifiable buffer");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1227 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1228 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1229
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1230 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1231 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
1232 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1233 if ( lenp )
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1234 *lenp = self->ob_size;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1235 return 1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1236 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1237
9305
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1238 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1239 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
1240 {
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1241 if ( index != 0 ) {
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1242 PyErr_SetString(PyExc_SystemError,
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1243 "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
1244 return -1;
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1245 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1246 *ptr = self->ob_sval;
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1247 return self->ob_size;
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1248 }
083efa076366 Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents: 9191
diff changeset
1249
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1250 static PySequenceMethods string_as_sequence = {
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1251 (inquiry)string_length, /*sq_length*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1252 (binaryfunc)string_concat, /*sq_concat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1253 (intargfunc)string_repeat, /*sq_repeat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1254 (intargfunc)string_item, /*sq_item*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1255 (intintargfunc)string_slice, /*sq_slice*/
259
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
1256 0, /*sq_ass_item*/
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
1257 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
1258 (objobjproc)string_contains /*sq_contains*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1259 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1261 static PyMappingMethods string_as_mapping = {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1262 (inquiry)string_length,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1263 (binaryfunc)string_subscript,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1264 0,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1265 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
1266
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1267 static PyBufferProcs string_as_buffer = {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1268 (getreadbufferproc)string_buffer_getreadbuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1269 (getwritebufferproc)string_buffer_getwritebuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1270 (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
1271 (getcharbufferproc)string_buffer_getcharbuf,
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1272 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1273
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1274
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1275
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1276 #define LEFTSTRIP 0
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1277 #define RIGHTSTRIP 1
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1278 #define BOTHSTRIP 2
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1279
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1280 /* Arrays indexed by above */
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1281 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1282
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1283 #define STRIPNAME(i) (stripformat[i]+3)
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1284
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1285 #define SPLIT_APPEND(data, left, right) \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1286 str = PyString_FromStringAndSize((data) + (left), \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1287 (right) - (left)); \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1288 if (str == NULL) \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1289 goto onError; \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1290 if (PyList_Append(list, str)) { \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1291 Py_DECREF(str); \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1292 goto onError; \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1293 } \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1294 else \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1295 Py_DECREF(str);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1296
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1297 #define SPLIT_INSERT(data, left, right) \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1298 str = PyString_FromStringAndSize((data) + (left), \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1299 (right) - (left)); \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1300 if (str == NULL) \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1301 goto onError; \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1302 if (PyList_Insert(list, 0, str)) { \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1303 Py_DECREF(str); \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1304 goto onError; \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1305 } \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1306 else \
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1307 Py_DECREF(str);
11383
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 static PyObject *
13460
e6d17f15ae6a Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents: 13459
diff changeset
1310 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
1311 {
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1312 int i, j;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1313 PyObject *str;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1314 PyObject *list = PyList_New(0);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1315
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1316 if (list == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1317 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1318
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1319 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
1320 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
1321 i++;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1322 j = i;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1323 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
1324 i++;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1325 if (j < i) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1326 if (maxsplit-- <= 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1327 break;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1328 SPLIT_APPEND(s, j, i);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1329 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
1330 i++;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1331 j = i;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1332 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1333 }
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1334 if (j < len) {
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1335 SPLIT_APPEND(s, j, len);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1336 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1337 return list;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1338 onError:
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1339 Py_DECREF(list);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1340 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1341 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1342
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1343 static PyObject *
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1344 split_char(const char *s, int len, char ch, int maxcount)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1345 {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1346 register int i, j;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1347 PyObject *str;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1348 PyObject *list = PyList_New(0);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1349
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1350 if (list == NULL)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1351 return NULL;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1352
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1353 for (i = j = 0; i < len; ) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1354 if (s[i] == ch) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1355 if (maxcount-- <= 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1356 break;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1357 SPLIT_APPEND(s, j, i);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1358 i = j = i + 1;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1359 } else
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1360 i++;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1361 }
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1362 if (j <= len) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1363 SPLIT_APPEND(s, j, len);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1364 }
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1365 return list;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1366
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1367 onError:
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1368 Py_DECREF(list);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1369 return NULL;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1370 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1371
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1372 PyDoc_STRVAR(split__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1373 "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
1374 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1375 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
1376 delimiter string. If maxsplit is given, at most maxsplit\n\
24561
b25c31e008c8 SF 582071 clarified the .split() method's docstring to note that sep=None
Raymond Hettinger <python@rcn.com>
parents: 24415
diff changeset
1377 splits are done. If sep is not specified or is None, any\n\
b25c31e008c8 SF 582071 clarified the .split() method's docstring to note that sep=None
Raymond Hettinger <python@rcn.com>
parents: 24415
diff changeset
1378 whitespace string is a separator.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1379
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1380 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1381 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
1382 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1383 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
1384 int maxsplit = -1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1385 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
1386 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
1387
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1388 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
1389 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1390 if (maxsplit < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1391 maxsplit = INT_MAX;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1392 if (subobj == Py_None)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1393 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
1394 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1395 sub = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1396 n = PyString_GET_SIZE(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1397 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1398 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1399 else if (PyUnicode_Check(subobj))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1400 return PyUnicode_Split((PyObject *)self, subobj, maxsplit);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1401 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1402 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
1403 return NULL;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1404
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1405 if (n == 0) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1406 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
1407 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1408 }
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1409 else if (n == 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1410 return split_char(s, len, sub[0], maxsplit);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1411
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1412 list = PyList_New(0);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1413 if (list == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1414 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1415
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1416 i = j = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1417 while (i+n <= len) {
12966
447efb64c72d Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents: 12924
diff changeset
1418 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
1419 if (maxsplit-- <= 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1420 break;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1421 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
1422 if (item == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1423 goto fail;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1424 err = PyList_Append(list, item);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1425 Py_DECREF(item);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1426 if (err < 0)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1427 goto fail;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1428 i = j = i + n;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1429 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1430 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1431 i++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1432 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1433 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
1434 if (item == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1435 goto fail;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1436 err = PyList_Append(list, item);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1437 Py_DECREF(item);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1438 if (err < 0)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1439 goto fail;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1440
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1441 return list;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1442
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1443 fail:
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1444 Py_DECREF(list);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1445 return NULL;
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
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1448 static PyObject *
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1449 rsplit_whitespace(const char *s, int len, int maxsplit)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1450 {
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1451 int i, j;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1452 PyObject *str;
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1453 PyObject *list = PyList_New(0);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1454
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1455 if (list == NULL)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1456 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1457
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1458 for (i = j = len - 1; i >= 0; ) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1459 while (i >= 0 && isspace(Py_CHARMASK(s[i])))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1460 i--;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1461 j = i;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1462 while (i >= 0 && !isspace(Py_CHARMASK(s[i])))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1463 i--;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1464 if (j > i) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1465 if (maxsplit-- <= 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1466 break;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1467 SPLIT_INSERT(s, i + 1, j + 1);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1468 while (i >= 0 && isspace(Py_CHARMASK(s[i])))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1469 i--;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1470 j = i;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1471 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1472 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1473 if (j >= 0) {
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1474 SPLIT_INSERT(s, 0, j + 1);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1475 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1476 return list;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1477 onError:
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1478 Py_DECREF(list);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1479 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1480 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1481
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1482 static PyObject *
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1483 rsplit_char(const char *s, int len, char ch, int maxcount)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1484 {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1485 register int i, j;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1486 PyObject *str;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1487 PyObject *list = PyList_New(0);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1488
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1489 if (list == NULL)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1490 return NULL;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1491
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1492 for (i = j = len - 1; i >= 0; ) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1493 if (s[i] == ch) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1494 if (maxcount-- <= 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1495 break;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1496 SPLIT_INSERT(s, i + 1, j + 1);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1497 j = i = i - 1;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1498 } else
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1499 i--;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1500 }
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1501 if (j >= -1) {
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1502 SPLIT_INSERT(s, 0, j + 1);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1503 }
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1504 return list;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1505
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1506 onError:
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1507 Py_DECREF(list);
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1508 return NULL;
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1509 }
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1510
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1511 PyDoc_STRVAR(rsplit__doc__,
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1512 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1513 \n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1514 Return a list of the words in the string S, using sep as the\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1515 delimiter string, starting at the end of the string and working\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1516 to the front. If maxsplit is given, at most maxsplit splits are\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1517 done. If sep is not specified or is None, any whitespace string\n\
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1518 is a separator.");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1519
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1520 static PyObject *
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1521 string_rsplit(PyStringObject *self, PyObject *args)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1522 {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1523 int len = PyString_GET_SIZE(self), n, i, j, err;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1524 int maxsplit = -1;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1525 const char *s = PyString_AS_STRING(self), *sub;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1526 PyObject *list, *item, *subobj = Py_None;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1527
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1528 if (!PyArg_ParseTuple(args, "|Oi:rsplit", &subobj, &maxsplit))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1529 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1530 if (maxsplit < 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1531 maxsplit = INT_MAX;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1532 if (subobj == Py_None)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1533 return rsplit_whitespace(s, len, maxsplit);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1534 if (PyString_Check(subobj)) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1535 sub = PyString_AS_STRING(subobj);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1536 n = PyString_GET_SIZE(subobj);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1537 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1538 #ifdef Py_USING_UNICODE
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1539 else if (PyUnicode_Check(subobj))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1540 return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1541 #endif
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1542 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1543 return NULL;
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1544
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1545 if (n == 0) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1546 PyErr_SetString(PyExc_ValueError, "empty separator");
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1547 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1548 }
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1549 else if (n == 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31250
diff changeset
1550 return rsplit_char(s, len, sub[0], maxsplit);
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1551
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1552 list = PyList_New(0);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1553 if (list == NULL)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1554 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1555
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1556 j = len;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1557 i = j - n;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1558 while (i >= 0) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1559 if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) {
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1560 if (maxsplit-- <= 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1561 break;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1562 item = PyString_FromStringAndSize(s+i+n, (int)(j-i-n));
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1563 if (item == NULL)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1564 goto fail;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1565 err = PyList_Insert(list, 0, item);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1566 Py_DECREF(item);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1567 if (err < 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1568 goto fail;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1569 j = i;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1570 i -= n;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1571 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1572 else
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1573 i--;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1574 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1575 item = PyString_FromStringAndSize(s, j);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1576 if (item == NULL)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1577 goto fail;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1578 err = PyList_Insert(list, 0, item);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1579 Py_DECREF(item);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1580 if (err < 0)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1581 goto fail;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1582
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1583 return list;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1584
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1585 fail:
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1586 Py_DECREF(list);
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1587 return NULL;
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1588 }
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
1589
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1590
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1591 PyDoc_STRVAR(join__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1592 "S.join(sequence) -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1593 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1594 Return a string which is the concatenation of the strings in the\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1595 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
1596
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1597 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1598 string_join(PyStringObject *self, PyObject *orig)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1599 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1600 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
1601 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
1602 PyObject *res = NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1603 char *p;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1604 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
1605 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
1606 int i;
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1607 PyObject *seq, *item;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1608
16014
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1609 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
1610 if (seq == NULL) {
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1611 if (PyErr_ExceptionMatches(PyExc_TypeError))
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1612 PyErr_Format(PyExc_TypeError,
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1613 "sequence expected, %.80s found",
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1614 orig->ob_type->tp_name);
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1615 return NULL;
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1616 }
16014
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1617
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13561
diff changeset
1618 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
1619 if (seqlen == 0) {
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1620 Py_DECREF(seq);
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1621 return PyString_FromString("");
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1622 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1623 if (seqlen == 1) {
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1624 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
1625 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
1626 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
1627 "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
1628 " %.80s found",
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1629 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
1630 Py_DECREF(seq);
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1631 return NULL;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1632 }
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1633 Py_INCREF(item);
13528
e848d6ed3eb9 string_join(): Some cleaning up of reference counting. In the
Barry Warsaw <barry@python.org>
parents: 13526
diff changeset
1634 Py_DECREF(seq);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1635 return item;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1636 }
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1637
16014
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1638 /* 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
1639 * 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
1640 * 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
1641 */
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1642 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
1643 const size_t old_sz = sz;
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1644 item = PySequence_Fast_GET_ITEM(seq, i);
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1645 if (!PyString_Check(item)){
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1646 #ifdef Py_USING_UNICODE
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1647 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
1648 /* 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
1649 * 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
1650 * 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
1651 * 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
1652 */
9e093bf8f6a9 Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents: 17655
diff changeset
1653 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
1654 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
1655 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
1656 return result;
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1657 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1658 #endif
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1659 PyErr_Format(PyExc_TypeError,
13544
3cf4f5aee00e small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13528
diff changeset
1660 "sequence item %i: expected string,"
3cf4f5aee00e small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13528
diff changeset
1661 " %.80s found",
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1662 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
1663 Py_DECREF(seq);
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1664 return NULL;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1665 }
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1666 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
1667 if (i != 0)
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1668 sz += seplen;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1669 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
1670 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
1671 "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
1672 Py_DECREF(seq);
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1673 return NULL;
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1674 }
16014
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1675 }
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1676
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1677 /* Allocate result space. */
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1678 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
1679 if (res == NULL) {
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1680 Py_DECREF(seq);
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1681 return NULL;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1682 }
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1683
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1684 /* Catenate everything. */
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1685 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
1686 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
1687 size_t n;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1688 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
1689 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
1690 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
1691 p += n;
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1692 if (i < seqlen - 1) {
13524
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1693 memcpy(p, sep, seplen);
16e490462e6b two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13460
diff changeset
1694 p += seplen;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1695 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1696 }
16014
754ecd21fbef Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents: 15910
diff changeset
1697
13526
3686d12362b8 fix two refcount bugs in new string_join implementation:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13524
diff changeset
1698 Py_DECREF(seq);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1699 return res;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1700 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1701
18130
8eea2efb34cd Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1702 PyObject *
8eea2efb34cd Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1703 _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
1704 {
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1705 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
1706 assert(x != NULL);
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1707 return string_join((PyStringObject *)sep, x);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1708 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18086
diff changeset
1709
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1710 static void
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1711 string_adjust_indices(int *start, int *end, int len)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1712 {
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1713 if (*end > len)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1714 *end = len;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1715 else if (*end < 0)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1716 *end += len;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1717 if (*end < 0)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1718 *end = 0;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1719 if (*start < 0)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1720 *start += len;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1721 if (*start < 0)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1722 *start = 0;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1723 }
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1724
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1725 static long
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1726 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
1727 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1728 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
1729 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
1730 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
1731 PyObject *subobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1732
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
1733 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex",
12615
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
1734 &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
1735 return -2;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1736 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1737 sub = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1738 n = PyString_GET_SIZE(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1739 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1740 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1741 else if (PyUnicode_Check(subobj))
24918
0d5d8e1cdc22 Fix some endcase bugs in unicode rfind()/rindex() and endswith().
Guido van Rossum <guido@python.org>
parents: 24901
diff changeset
1742 return PyUnicode_Find((PyObject *)self, subobj, i, last, dir);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1743 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1744 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
1745 return -2;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1746
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
1747 string_adjust_indices(&i, &last, len);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1748
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1749 if (dir > 0) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1750 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
1751 return (long)i;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1752 last -= n;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1753 for (; i <= last; ++i)
12966
447efb64c72d Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents: 12924
diff changeset
1754 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
1755 return (long)i;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1756 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1757 else {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1758 int j;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
1759
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1760 if (n == 0 && i <= last)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1761 return (long)last;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1762 for (j = last-n; j >= i; --j)
12966
447efb64c72d Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents: 12924
diff changeset
1763 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
1764 return (long)j;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1765 }
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
1766
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1767 return -1;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1768 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1769
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1770
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1771 PyDoc_STRVAR(find__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1772 "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
1773 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1774 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
1775 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
1776 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
1777 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1778 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1779
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1780 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1781 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
1782 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1783 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
1784 if (result == -2)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1785 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1786 return PyInt_FromLong(result);
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
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1789
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1790 PyDoc_STRVAR(index__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1791 "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
1792 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1793 Like S.find() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1794
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1795 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1796 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
1797 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1798 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
1799 if (result == -2)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1800 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1801 if (result == -1) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1802 PyErr_SetString(PyExc_ValueError,
27248
c4150f7f6a69 SF patch #664192 bug #661913: inconsistent error messages between string
Raymond Hettinger <python@rcn.com>
parents: 27123
diff changeset
1803 "substring not found");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1804 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1805 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1806 return PyInt_FromLong(result);
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
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1809
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1810 PyDoc_STRVAR(rfind__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1811 "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
1812 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1813 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
1814 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
1815 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
1816 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1817 Return -1 on failure.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1818
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1819 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1820 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
1821 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1822 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
1823 if (result == -2)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1824 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1825 return PyInt_FromLong(result);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1826 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1827
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1828
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1829 PyDoc_STRVAR(rindex__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1830 "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
1831 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1832 Like S.rfind() but raise ValueError when the substring is not found.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1833
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1834 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
1835 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
1836 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
1837 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
1838 if (result == -2)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1839 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1840 if (result == -1) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1841 PyErr_SetString(PyExc_ValueError,
27248
c4150f7f6a69 SF patch #664192 bug #661913: inconsistent error messages between string
Raymond Hettinger <python@rcn.com>
parents: 27123
diff changeset
1842 "substring not found");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1843 return NULL;
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 return PyInt_FromLong(result);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1846 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1847
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1848
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1849 static PyObject *
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1850 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1851 {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1852 char *s = PyString_AS_STRING(self);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1853 int len = PyString_GET_SIZE(self);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1854 char *sep = PyString_AS_STRING(sepobj);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1855 int seplen = PyString_GET_SIZE(sepobj);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1856 int i, j;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1857
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1858 i = 0;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1859 if (striptype != RIGHTSTRIP) {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1860 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1861 i++;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1862 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1863 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1864
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1865 j = len;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1866 if (striptype != LEFTSTRIP) {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1867 do {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1868 j--;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1869 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1870 j++;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1871 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1872
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1873 if (i == 0 && j == len && PyString_CheckExact(self)) {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1874 Py_INCREF(self);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1875 return (PyObject*)self;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1876 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1877 else
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1878 return PyString_FromStringAndSize(s+i, j-i);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1879 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1880
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1881
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1882 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1883 do_strip(PyStringObject *self, int striptype)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1884 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1885 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
1886 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
1887
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1888 i = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1889 if (striptype != RIGHTSTRIP) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1890 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
1891 i++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1892 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1893 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1894
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1895 j = len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1896 if (striptype != LEFTSTRIP) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1897 do {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1898 j--;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1899 } 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
1900 j++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1901 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1902
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
1903 if (i == 0 && j == len && PyString_CheckExact(self)) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1904 Py_INCREF(self);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1905 return (PyObject*)self;
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 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1908 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
1909 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1910
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1911
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1912 static PyObject *
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1913 do_argstrip(PyStringObject *self, int striptype, PyObject *args)
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1914 {
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1915 PyObject *sep = NULL;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1916
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1917 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1918 return NULL;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1919
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1920 if (sep != NULL && sep != Py_None) {
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1921 if (PyString_Check(sep))
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1922 return do_xstrip(self, striptype, sep);
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1923 #ifdef Py_USING_UNICODE
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1924 else if (PyUnicode_Check(sep)) {
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1925 PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1926 PyObject *res;
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1927 if (uniself==NULL)
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1928 return NULL;
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1929 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1930 striptype, sep);
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1931 Py_DECREF(uniself);
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1932 return res;
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1933 }
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1934 #endif
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1935 else {
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1936 PyErr_Format(PyExc_TypeError,
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1937 #ifdef Py_USING_UNICODE
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1938 "%s arg must be None, str or unicode",
23430
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1939 #else
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1940 "%s arg must be None or str",
1be6e79b1d46 Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents: 23295
diff changeset
1941 #endif
23225
e639e34ef901 Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents: 23051
diff changeset
1942 STRIPNAME(striptype));
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1943 return NULL;
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1944 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1945 return do_xstrip(self, striptype, sep);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1946 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1947
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1948 return do_strip(self, striptype);
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1949 }
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1950
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1951
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1952 PyDoc_STRVAR(strip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1953 "S.strip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1954 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1955 Return a copy of the string S with leading and trailing\n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1956 whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1957 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1958 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1959
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1960 static PyObject *
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1961 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
1962 {
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1963 if (PyTuple_GET_SIZE(args) == 0)
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1964 return do_strip(self, BOTHSTRIP); /* Common case */
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1965 else
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1966 return do_argstrip(self, BOTHSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1967 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1968
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1969
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1970 PyDoc_STRVAR(lstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1971 "S.lstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1972 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1973 Return a copy of the string S with leading whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1974 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1975 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1976
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1977 static PyObject *
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1978 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
1979 {
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1980 if (PyTuple_GET_SIZE(args) == 0)
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1981 return do_strip(self, LEFTSTRIP); /* Common case */
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1982 else
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1983 return do_argstrip(self, LEFTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1984 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1985
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1986
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
1987 PyDoc_STRVAR(rstrip__doc__,
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1988 "S.rstrip([chars]) -> string or unicode\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1989 \n\
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1990 Return a copy of the string S with trailing whitespace removed.\n\
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1991 If chars is given and not None, remove characters in chars instead.\n\
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28559
diff changeset
1992 If chars is unicode, S will be converted to unicode before stripping");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1993
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
1994 static PyObject *
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1995 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
1996 {
23015
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1997 if (PyTuple_GET_SIZE(args) == 0)
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1998 return do_strip(self, RIGHTSTRIP); /* Common case */
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
1999 else
202ae5b75cca Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents: 22977
diff changeset
2000 return do_argstrip(self, RIGHTSTRIP, args);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2001 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2002
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2003
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2004 PyDoc_STRVAR(lower__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2005 "S.lower() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2006 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2007 Return a copy of the string S converted to lowercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2008
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2009 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2010 string_lower(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2011 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2012 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
2013 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
2014 PyObject *new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2015
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2016 new = PyString_FromStringAndSize(NULL, n);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2017 if (new == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2018 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2019 s_new = PyString_AsString(new);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2020 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
2021 int c = Py_CHARMASK(*s++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2022 if (isupper(c)) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2023 *s_new = tolower(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2024 } else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2025 *s_new = c;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2026 s_new++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2027 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2028 return new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2029 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2030
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2031
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2032 PyDoc_STRVAR(upper__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2033 "S.upper() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2034 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2035 Return a copy of the string S converted to uppercase.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2036
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2037 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2038 string_upper(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2039 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2040 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
2041 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
2042 PyObject *new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2043
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2044 new = PyString_FromStringAndSize(NULL, n);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2045 if (new == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2046 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2047 s_new = PyString_AsString(new);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2048 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
2049 int c = Py_CHARMASK(*s++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2050 if (islower(c)) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2051 *s_new = toupper(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2052 } else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2053 *s_new = c;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2054 s_new++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2055 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2056 return new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2057 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2058
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2059
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2060 PyDoc_STRVAR(title__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2061 "S.title() -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2062 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2063 Return a titlecased version of S, i.e. words start with uppercase\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2064 characters, all remaining cased characters have lowercase.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2065
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2066 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2067 string_title(PyStringObject *self)
11969
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 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
2070 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
2071 int previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2072 PyObject *new;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2073
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2074 new = PyString_FromStringAndSize(NULL, n);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2075 if (new == NULL)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2076 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2077 s_new = PyString_AsString(new);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2078 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
2079 int c = Py_CHARMASK(*s++);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2080 if (islower(c)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2081 if (!previous_is_cased)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2082 c = toupper(c);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2083 previous_is_cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2084 } else if (isupper(c)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2085 if (previous_is_cased)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2086 c = tolower(c);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2087 previous_is_cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2088 } else
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2089 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2090 *s_new++ = c;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2091 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2092 return new;
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2095 PyDoc_STRVAR(capitalize__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2096 "S.capitalize() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2097 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2098 Return a copy of the string S with only its first character\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2099 capitalized.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2100
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2101 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2102 string_capitalize(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2103 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2104 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
2105 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
2106 PyObject *new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2107
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2108 new = PyString_FromStringAndSize(NULL, n);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2109 if (new == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2110 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2111 s_new = PyString_AsString(new);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2112 if (0 < n) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2113 int c = Py_CHARMASK(*s++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2114 if (islower(c))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2115 *s_new = toupper(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2116 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2117 *s_new = c;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2118 s_new++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2119 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2120 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
2121 int c = Py_CHARMASK(*s++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2122 if (isupper(c))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2123 *s_new = tolower(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2124 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2125 *s_new = c;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2126 s_new++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2127 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2128 return new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2129 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2130
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2131
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2132 PyDoc_STRVAR(count__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2133 "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
2134 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2135 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
2136 S[start:end]. Optional arguments start and end are\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2137 interpreted as in slice notation.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2138
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2139 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2140 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
2141 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2142 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
2143 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
2144 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
2145 int m, r;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2146 PyObject *subobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2147
12615
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2148 if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj,
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2149 _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
2150 return NULL;
12615
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2151
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2152 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2153 sub = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2154 n = PyString_GET_SIZE(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2155 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2156 #ifdef Py_USING_UNICODE
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2157 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
2158 int count;
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2159 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
2160 if (count == -1)
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2161 return NULL;
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2162 else
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2163 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
2164 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2165 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2166 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
2167 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2168
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2169 string_adjust_indices(&i, &last, len);
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2170
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2171 m = last + 1 - n;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2172 if (n == 0)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2173 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
2174
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2175 r = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2176 while (i < m) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2177 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
2178 r++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2179 i += n;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2180 } else {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2181 i++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2182 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2183 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2184 return PyInt_FromLong((long) r);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2185 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2186
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2187
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2188 PyDoc_STRVAR(swapcase__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2189 "S.swapcase() -> string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2190 \n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2191 Return a copy of the string S with uppercase characters\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2192 converted to lowercase and vice versa.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2193
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2194 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2195 string_swapcase(PyStringObject *self)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2196 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2197 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
2198 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
2199 PyObject *new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2200
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2201 new = PyString_FromStringAndSize(NULL, n);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2202 if (new == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2203 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2204 s_new = PyString_AsString(new);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2205 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
2206 int c = Py_CHARMASK(*s++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2207 if (islower(c)) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2208 *s_new = toupper(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2209 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2210 else if (isupper(c)) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2211 *s_new = tolower(c);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2212 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2213 else
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2214 *s_new = c;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2215 s_new++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2216 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2217 return new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2218 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2219
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2220
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2221 PyDoc_STRVAR(translate__doc__,
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2222 "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
2223 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2224 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
2225 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
2226 remaining characters have been mapped through the given\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2227 translation table, which must be a string of length 256.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2228
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2229 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2230 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
2231 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2232 register char *input, *output;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2233 register const char *table;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2234 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
2235 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
2236 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
2237 int inlen, tablen, dellen = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2238 PyObject *result;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2239 int trans_table[256];
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2240 PyObject *tableobj, *delobj = NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2241
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26625
diff changeset
2242 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2243 &tableobj, &delobj))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2244 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2245
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2246 if (PyString_Check(tableobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2247 table1 = PyString_AS_STRING(tableobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2248 tablen = PyString_GET_SIZE(tableobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2249 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2250 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2251 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
2252 /* 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
2253 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
2254 to be deleted. */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2255 if (delobj != NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2256 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2257 "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
2258 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2259 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2260 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
2261 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2262 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2263 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
2264 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2265
26625
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2266 if (tablen != 256) {
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2267 PyErr_SetString(PyExc_ValueError,
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2268 "translation table must be 256 characters long");
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2269 return NULL;
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2270 }
ba1578be5706 Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents: 26550
diff changeset
2271
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2272 if (delobj != NULL) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2273 if (PyString_Check(delobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2274 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
2275 dellen = PyString_GET_SIZE(delobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2276 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2277 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2278 else if (PyUnicode_Check(delobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2279 PyErr_SetString(PyExc_TypeError,
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2280 "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
2281 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2282 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2283 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2284 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
2285 return NULL;
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 else {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2288 del_table = NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2289 dellen = 0;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2290 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2291
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2292 table = table1;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2293 inlen = PyString_Size(input_obj);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2294 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
2295 if (result == NULL)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2296 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2297 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
2298 input = PyString_AsString(input_obj);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2299
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2300 if (dellen == 0) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2301 /* 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
2302 for (i = inlen; --i >= 0; ) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2303 c = Py_CHARMASK(*input++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2304 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
2305 changed = 1;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2306 }
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2307 if (changed || !PyString_CheckExact(input_obj))
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2308 return result;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2309 Py_DECREF(result);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2310 Py_INCREF(input_obj);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2311 return input_obj;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2312 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2313
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2314 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
2315 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
2316
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2317 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
2318 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
2319
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2320 for (i = inlen; --i >= 0; ) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2321 c = Py_CHARMASK(*input++);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2322 if (trans_table[c] != -1)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2323 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
2324 continue;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2325 changed = 1;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2326 }
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2327 if (!changed && PyString_CheckExact(input_obj)) {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2328 Py_DECREF(result);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2329 Py_INCREF(input_obj);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2330 return input_obj;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2331 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2332 /* Fix the size of the resulting string */
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
2333 if (inlen > 0)
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
2334 _PyString_Resize(&result, output - output_start);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2335 return result;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2336 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2337
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2338
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2339 /* 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
2340
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2341 /*
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2342 mymemfind
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2343
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2344 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
2345
12025
7994d9db7f4d On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents: 11998
diff changeset
2346 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
2347 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
2348 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
2349 MEM, the function returns -1.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2350 */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
2351 static int
13460
e6d17f15ae6a Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents: 13459
diff changeset
2352 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
2353 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2354 register int ii;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2355
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2356 /* 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
2357 len -= pat_len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2358
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2359 for (ii = 0; ii <= len; ii++) {
12966
447efb64c72d Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents: 12924
diff changeset
2360 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
2361 return ii;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2362 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2363 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2364 return -1;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2365 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2366
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2367 /*
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2368 mymemcnt
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2369
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2370 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
2371 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
2372 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
2373 */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
2374 static int
13460
e6d17f15ae6a Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents: 13459
diff changeset
2375 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
2376 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2377 register int offset = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2378 int nfound = 0;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2379
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2380 while (len >= 0) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2381 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
2382 if (offset == -1)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2383 break;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2384 mem += offset + pat_len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2385 len -= offset + pat_len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2386 nfound++;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2387 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2388 return nfound;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2389 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2390
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2391 /*
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2392 mymemreplace
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2393
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
2394 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
2395 replaced with SUB.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2396
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
2397 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
2398 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
2399 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
2400
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2401 on return, out_len is:
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2402 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
2403 -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
2404 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
2405
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2406 return value is:
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2407 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
2408 NULL if an error occurred.
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2409 */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2410 static char *
13460
e6d17f15ae6a Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents: 13459
diff changeset
2411 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
2412 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
2413 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
2414 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
2415 int *out_len)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2416 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2417 char *out_s;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2418 char *new_s;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2419 int nfound, offset, new_len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2420
25004
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2421 if (len == 0 || (pat_len == 0 && sub_len == 0) || pat_len > len)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2422 goto return_same;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2423
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2424 /* find length of output string */
25004
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2425 nfound = (pat_len > 0) ? mymemcnt(str, len, pat, pat_len) : len + 1;
17751
c886cd1c2184 Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents: 17750
diff changeset
2426 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
2427 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
2428 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
2429 nfound = count;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2430 if (nfound == 0)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2431 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
2432
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2433 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
2434 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
2435 /* 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
2436 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
2437 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
2438 return NULL;
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2439 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
2440 }
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2441 else {
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2442 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
2443 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
2444 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
2445 return NULL;
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2446 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
2447
25004
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2448 if (pat_len > 0) {
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2449 for (; nfound > 0; --nfound) {
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2450 /* find index of next instance of pattern */
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2451 offset = mymemfind(str, len, pat, pat_len);
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2452 if (offset == -1)
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2453 break;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2454
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2455 /* copy non matching part of input string */
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2456 memcpy(new_s, str, offset);
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2457 str += offset + pat_len;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2458 len -= offset + pat_len;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2459
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2460 /* copy substitute into the output string */
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2461 new_s += offset;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2462 memcpy(new_s, sub, sub_len);
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2463 new_s += sub_len;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2464 }
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2465 /* copy any remaining values into output string */
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2466 if (len > 0)
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2467 memcpy(new_s, str, len);
17750
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2468 }
25004
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2469 else {
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2470 for (;;++str, --len) {
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2471 memcpy(new_s, sub, sub_len);
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2472 new_s += sub_len;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2473 if (--nfound <= 0) {
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2474 memcpy(new_s, str, len);
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2475 break;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2476 }
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2477 *new_s++ = *str;
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2478 }
762dd09edb83 Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents: 24918
diff changeset
2479 }
17750
b39266f7484c Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents: 17741
diff changeset
2480 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2481 *out_len = new_len;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2482 return out_s;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2483
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2484 return_same:
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2485 *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
2486 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
2487 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2488
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2489
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2490 PyDoc_STRVAR(replace__doc__,
30751
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2491 "S.replace (old, new[, count]) -> string\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2492 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2493 Return a copy of string S with all occurrences of substring\n\
30751
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2494 old replaced by new. If the optional argument count is\n\
6fdde96a7222 Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents: 30706
diff changeset
2495 given, only the first count occurrences are replaced.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2496
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2497 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2498 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
2499 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2500 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
2501 char *new_s;
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2502 const int len = PyString_GET_SIZE(self);
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2503 int sub_len, repl_len, out_len;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2504 int count = -1;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2505 PyObject *new;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2506 PyObject *subobj, *replobj;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2507
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2508 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
2509 &subobj, &replobj, &count))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2510 return NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2511
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2512 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2513 sub = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2514 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
2515 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2516 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2517 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
2518 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
2519 subobj, replobj, count);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2520 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2521 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
2522 return NULL;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2523
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2524 if (PyString_Check(replobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2525 repl = PyString_AS_STRING(replobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2526 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
2527 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2528 #ifdef Py_USING_UNICODE
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2529 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
2530 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
2531 subobj, replobj, count);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2532 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2533 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
2534 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2535
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2536 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
2537 if (new_s == NULL) {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2538 PyErr_NoMemory();
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2539 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2540 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2541 if (out_len == -1) {
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2542 if (PyString_CheckExact(self)) {
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2543 /* we're returning another reference to self */
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2544 new = (PyObject*)self;
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2545 Py_INCREF(new);
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2546 }
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2547 else {
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2548 new = PyString_FromStringAndSize(str, len);
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2549 if (new == NULL)
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2550 return NULL;
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2551 }
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2552 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2553 else {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2554 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
2555 PyMem_FREE(new_s);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2556 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2557 return new;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2558 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2559
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2560
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2561 PyDoc_STRVAR(startswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2562 "S.startswith(prefix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2563 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2564 Return True if S starts with the specified prefix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2565 With optional start, test S beginning at that position.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2566 With optional end, stop comparing S at that position.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2567
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2568 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2569 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
2570 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2571 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
2572 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
2573 const char* prefix;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2574 int plen;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2575 int start = 0;
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2576 int end = INT_MAX;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2577 PyObject *subobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2578
12615
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2579 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2580 _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
2581 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2582 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2583 prefix = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2584 plen = PyString_GET_SIZE(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2585 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2586 #ifdef Py_USING_UNICODE
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2587 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
2588 int rc;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
2589 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
2590 subobj, start, end, -1);
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2591 if (rc == -1)
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2592 return NULL;
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2593 else
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2594 return PyBool_FromLong((long) rc);
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2595 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2596 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2597 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
2598 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2599
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2600 string_adjust_indices(&start, &end, len);
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2601
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2602 if (start+plen > len)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2603 return PyBool_FromLong(0);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2604
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2605 if (end-start >= plen)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2606 return PyBool_FromLong(!memcmp(str+start, prefix, plen));
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2607 else
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2608 return PyBool_FromLong(0);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2609 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2610
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2611
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2612 PyDoc_STRVAR(endswith__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2613 "S.endswith(suffix[, start[, end]]) -> bool\n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2614 \n\
28559
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2615 Return True if S ends with the specified suffix, False otherwise.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2616 With optional start, test S beginning at that position.\n\
aff080f2efb4 Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents: 28478
diff changeset
2617 With optional end, stop comparing S at that position.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2618
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2619 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2620 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
2621 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2622 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
2623 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
2624 const char* suffix;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2625 int slen;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2626 int start = 0;
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2627 int end = INT_MAX;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2628 PyObject *subobj;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2629
12615
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2630 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
67233df13037 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12588
diff changeset
2631 _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
2632 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2633 if (PyString_Check(subobj)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2634 suffix = PyString_AS_STRING(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2635 slen = PyString_GET_SIZE(subobj);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2636 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2637 #ifdef Py_USING_UNICODE
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2638 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
2639 int rc;
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
2640 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
2641 subobj, start, end, +1);
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2642 if (rc == -1)
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2643 return NULL;
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2644 else
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2645 return PyBool_FromLong((long) rc);
15910
f7f3be3d00b9 Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents: 15579
diff changeset
2646 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2647 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2648 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
2649 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2650
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2651 string_adjust_indices(&start, &end, len);
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2652
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2653 if (end-start < slen || start > len)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2654 return PyBool_FromLong(0);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2655
23850
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2656 if (end-slen > start)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2657 start = end - slen;
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2658 if (end-start >= slen)
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2659 return PyBool_FromLong(!memcmp(str+start, suffix, slen));
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2660 else
854583cd63c7 SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
2661 return PyBool_FromLong(0);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2662 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2663
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
2664
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2665 PyDoc_STRVAR(encode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2666 "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
2667 \n\
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2668 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
2669 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
2670 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2671 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2672 'xmlcharrefreplace' as well as any other name registered with\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2673 codecs.register_error that is able to handle UnicodeEncodeErrors.");
13377
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2674
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2675 static PyObject *
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2676 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
2677 {
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2678 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
2679 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
2680 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
2681 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
2682 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
2683 }
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2684
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2685
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2686 PyDoc_STRVAR(decode__doc__,
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2687 "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
2688 \n\
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2689 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
2690 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
2691 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
25091
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2692 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2693 as well as any other name registerd with codecs.register_error that is\n\
0337dad8403e PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents: 25017
diff changeset
2694 able to handle UnicodeDecodeErrors.");
17835
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2695
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2696 static PyObject *
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2697 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
2698 {
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2699 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
2700 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
2701 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
2702 return NULL;
9ea87c0a7c04 This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents: 17751
diff changeset
2703 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
2704 }
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2705
2017f54b0325 Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents: 13354
diff changeset
2706
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2707 PyDoc_STRVAR(expandtabs__doc__,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2708 "S.expandtabs([tabsize]) -> string\n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2709 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2710 Return a copy of S where all tab characters are expanded using spaces.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2711 If tabsize is not given, a tab size of 8 characters is assumed.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2712
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2713 static PyObject*
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2714 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
2715 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2716 const char *e, *p;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2717 char *q;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2718 int i, j;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2719 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2720 int tabsize = 8;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2721
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2722 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
2723 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2724
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
2725 /* 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
2726 i = j = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2727 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
2728 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
2729 if (*p == '\t') {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2730 if (tabsize > 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2731 j += tabsize - (j % tabsize);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2732 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2733 else {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2734 j++;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2735 if (*p == '\n' || *p == '\r') {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2736 i += j;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2737 j = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2738 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2739 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2740
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2741 /* 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
2742 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
2743 if (!u)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2744 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2745
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2746 j = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2747 q = PyString_AS_STRING(u);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2748
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2749 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
2750 if (*p == '\t') {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2751 if (tabsize > 0) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2752 i = tabsize - (j % tabsize);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2753 j += i;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2754 while (i--)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2755 *q++ = ' ';
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2756 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2757 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2758 else {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2759 j++;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2760 *q++ = *p;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2761 if (*p == '\n' || *p == '\r')
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2762 j = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2763 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2764
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2765 return u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2766 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2767
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2768 static PyObject *
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2769 pad(PyStringObject *self, int left, int right, char fill)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2770 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2771 PyObject *u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2772
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2773 if (left < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2774 left = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2775 if (right < 0)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2776 right = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2777
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2778 if (left == 0 && right == 0 && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2779 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2780 return (PyObject *)self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2781 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2782
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
2783 u = PyString_FromStringAndSize(NULL,
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2784 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
2785 if (u) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2786 if (left)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2787 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
2788 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
2789 PyString_AS_STRING(self),
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2790 PyString_GET_SIZE(self));
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2791 if (right)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2792 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
2793 fill, right);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2794 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2795
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2796 return u;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2797 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2798
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2799 PyDoc_STRVAR(ljust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2800 "S.ljust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2801 "\n"
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2802 "Return S left justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2803 "done using the specified fill character (default is a space).");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2804
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2805 static PyObject *
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2806 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
2807 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2808 int width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2809 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2810
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2811 if (!PyArg_ParseTuple(args, "i|c:ljust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2812 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2813
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2814 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2815 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2816 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2817 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2818
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2819 return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2820 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2821
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2822
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2823 PyDoc_STRVAR(rjust__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2824 "S.rjust(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2825 "\n"
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2826 "Return S right justified in a string of length width. Padding is\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2827 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2828
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2829 static PyObject *
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2830 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
2831 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2832 int width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2833 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2834
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2835 if (!PyArg_ParseTuple(args, "i|c:rjust", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2836 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2837
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2838 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2839 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2840 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2841 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2842
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2843 return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2844 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2845
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2846
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2847 PyDoc_STRVAR(center__doc__,
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2848 "S.center(width[, fillchar]) -> string\n"
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2849 "\n"
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2850 "Return S centered in a string of length width. Padding is\n"
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2851 "done using the specified fill character (default is a space)");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2852
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2853 static PyObject *
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2854 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
2855 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2856 int marg, left;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2857 int width;
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2858 char fillchar = ' ';
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2859
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2860 if (!PyArg_ParseTuple(args, "i|c:center", &width, &fillchar))
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2861 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2862
19884
df66b4dd0b0d More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents: 19846
diff changeset
2863 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2864 Py_INCREF(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2865 return (PyObject*) self;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2866 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2867
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2868 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
2869 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
2870
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30751
diff changeset
2871 return pad(self, left, marg - left, fillchar);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2872 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2873
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2874 PyDoc_STRVAR(zfill__doc__,
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2875 "S.zfill(width) -> string\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2876 "\n"
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2877 "Pad a numeric string S with zeros on the left, to fill a field\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2878 "of the specified width. The string S is never truncated.");
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2879
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2880 static PyObject *
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2881 string_zfill(PyStringObject *self, PyObject *args)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2882 {
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2883 int fill;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2884 PyObject *s;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
2885 char *p;
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2886
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2887 int width;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2888 if (!PyArg_ParseTuple(args, "i:zfill", &width))
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2889 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2890
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2891 if (PyString_GET_SIZE(self) >= width) {
23051
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2892 if (PyString_CheckExact(self)) {
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2893 Py_INCREF(self);
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2894 return (PyObject*) self;
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2895 }
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2896 else
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2897 return PyString_FromStringAndSize(
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2898 PyString_AS_STRING(self),
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2899 PyString_GET_SIZE(self)
19ea00feda33 Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 23046
diff changeset
2900 );
23045
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2901 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2902
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2903 fill = width - PyString_GET_SIZE(self);
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2904
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2905 s = pad(self, fill, 0, '0');
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2906
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2907 if (s == NULL)
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2908 return NULL;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2909
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2910 p = PyString_AS_STRING(s);
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2911 if (p[fill] == '+' || p[fill] == '-') {
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2912 /* move sign to beginning of string */
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2913 p[0] = p[fill];
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2914 p[fill] = '0';
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2915 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2916
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2917 return (PyObject*) s;
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2918 }
8b1268bc371a Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents: 23015
diff changeset
2919
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2920 PyDoc_STRVAR(isspace__doc__,
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2921 "S.isspace() -> bool\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2922 \n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2923 Return True if all characters in S are whitespace\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2924 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2925
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2926 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2927 string_isspace(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2928 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2929 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2930 = (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
2931 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
2932
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2933 /* Shortcut for single character strings */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2934 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
2935 isspace(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2936 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2937
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
2938 /* Special case for empty strings */
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
2939 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2940 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
2941
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2942 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
2943 for (; p < e; p++) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2944 if (!isspace(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2945 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2946 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2947 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2948 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2949
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
2950
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2951 PyDoc_STRVAR(isalpha__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2952 "S.isalpha() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2953 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2954 Return True if all characters in S are alphabetic\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2955 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2956
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2957 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2958 string_isalpha(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2959 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2960 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2961 = (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
2962 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
2963
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2964 /* 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
2965 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
2966 isalpha(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2967 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2968
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2969 /* 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
2970 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2971 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2972
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2973 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
2974 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
2975 if (!isalpha(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2976 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2977 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2978 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2979 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2980
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2981
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2982 PyDoc_STRVAR(isalnum__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2983 "S.isalnum() -> bool\n\
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2984 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
2985 Return True if all characters in S are alphanumeric\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
2986 and there is at least one character in S, False otherwise.");
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2987
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2988 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
2989 string_isalnum(PyStringObject *self)
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2990 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2991 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
2992 = (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
2993 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
2994
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2995 /* 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
2996 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
2997 isalnum(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
2998 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
2999
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3000 /* 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
3001 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3002 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3003
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3004 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
3005 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
3006 if (!isalnum(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3007 return PyBool_FromLong(0);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3008 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3009 return PyBool_FromLong(1);
13354
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3010 }
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3011
99f341a97fde Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents: 13256
diff changeset
3012
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3013 PyDoc_STRVAR(isdigit__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3014 "S.isdigit() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3015 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3016 Return True if all characters in S are digits\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3017 and there is at least one character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3018
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3019 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3020 string_isdigit(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3021 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3022 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3023 = (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
3024 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
3025
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3026 /* Shortcut for single character strings */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3027 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
3028 isdigit(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3029 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3030
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3031 /* Special case for empty strings */
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3032 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3033 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3034
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3035 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
3036 for (; p < e; p++) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3037 if (!isdigit(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3038 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3039 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3040 return PyBool_FromLong(1);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3041 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3042
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3043
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3044 PyDoc_STRVAR(islower__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3045 "S.islower() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3046 \n\
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3047 Return True if all cased characters in S are lowercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3048 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3049
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3050 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3051 string_islower(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3052 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3053 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3054 = (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
3055 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
3056 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3057
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3058 /* Shortcut for single character strings */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3059 if (PyString_GET_SIZE(self) == 1)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3060 return PyBool_FromLong(islower(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3061
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3062 /* Special case for empty strings */
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3063 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3064 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3065
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3066 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
3067 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3068 for (; p < e; p++) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3069 if (isupper(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3070 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3071 else if (!cased && islower(*p))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3072 cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3073 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3074 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3075 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3076
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3077
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3078 PyDoc_STRVAR(isupper__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3079 "S.isupper() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3080 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3081 Return True if all cased characters in S are uppercase and there is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3082 at least one cased character in S, False otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3083
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3084 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3085 string_isupper(PyStringObject *self)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3086 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3087 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3088 = (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
3089 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
3090 int cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3091
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3092 /* Shortcut for single character strings */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3093 if (PyString_GET_SIZE(self) == 1)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3094 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3095
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3096 /* Special case for empty strings */
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3097 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3098 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3099
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3100 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
3101 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3102 for (; p < e; p++) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3103 if (islower(*p))
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3104 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3105 else if (!cased && isupper(*p))
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3106 cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3107 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3108 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3109 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3110
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3111
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3112 PyDoc_STRVAR(istitle__doc__,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3113 "S.istitle() -> bool\n\
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3114 \n\
30706
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3115 Return True if S is a titlecased string and there is at least one\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3116 character in S, i.e. uppercase characters may only follow uncased\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3117 characters and lowercase characters only cased ones. Return False\n\
be0ca26ad574 Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents: 30216
diff changeset
3118 otherwise.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3119
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3120 static PyObject*
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3121 string_istitle(PyStringObject *self, PyObject *uncased)
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3122 {
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3123 register const unsigned char *p
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3124 = (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
3125 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
3126 int cased, previous_is_cased;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3127
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3128 /* Shortcut for single character strings */
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3129 if (PyString_GET_SIZE(self) == 1)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3130 return PyBool_FromLong(isupper(*p) != 0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3131
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3132 /* Special case for empty strings */
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3133 if (PyString_GET_SIZE(self) == 0)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3134 return PyBool_FromLong(0);
12924
2d0b99778edf Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12902
diff changeset
3135
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3136 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
3137 cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3138 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3139 for (; p < e; p++) {
12588
54274beb244d The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
3140 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
3141
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3142 if (isupper(ch)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3143 if (previous_is_cased)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3144 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3145 previous_is_cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3146 cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3147 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3148 else if (islower(ch)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3149 if (!previous_is_cased)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3150 return PyBool_FromLong(0);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3151 previous_is_cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3152 cased = 1;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3153 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3154 else
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3155 previous_is_cased = 0;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3156 }
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22689
diff changeset
3157 return PyBool_FromLong(cased);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3158 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3159
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3160
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3161 PyDoc_STRVAR(splitlines__doc__,
20415
621c7703e8b7 Remove extra "]" in splitlines() docstring.
Fred Drake <fdrake@acm.org>
parents: 20277
diff changeset
3162 "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
3163 \n\
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3164 Return a list of the lines in S, breaking at line boundaries.\n\
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3165 Line breaks are not included in the resulting list unless keepends\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3166 is given and true.");
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3167
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3168 static PyObject*
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3169 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
3170 {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3171 register int i;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3172 register int j;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3173 int len;
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3174 int keepends = 0;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3175 PyObject *list;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3176 PyObject *str;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3177 char *data;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3178
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3179 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
3180 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3181
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3182 data = PyString_AS_STRING(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3183 len = PyString_GET_SIZE(self);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3184
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3185 list = PyList_New(0);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3186 if (!list)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3187 goto onError;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3188
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3189 for (i = j = 0; i < len; ) {
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3190 int eol;
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3191
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3192 /* 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
3193 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
3194 i++;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3195
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3196 /* Skip the line break reading CRLF as one line break */
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3197 eol = i;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3198 if (i < len) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3199 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
3200 data[i+1] == '\n')
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3201 i += 2;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3202 else
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3203 i++;
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3204 if (keepends)
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3205 eol = i;
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3206 }
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
3207 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
3208 j = i;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3209 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3210 if (j < len) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3211 SPLIT_APPEND(data, j, len);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3212 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3213
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3214 return list;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3215
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3216 onError:
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3217 Py_DECREF(list);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3218 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3219 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3220
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3221 #undef SPLIT_APPEND
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3222
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3223 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3224 string_getnewargs(PyStringObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3225 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3226 return Py_BuildValue("(s#)", v->ob_sval, v->ob_size);
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3227 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3228
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3229
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3230 static PyMethodDef
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3231 string_methods[] = {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11942
diff changeset
3232 /* 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
3233 string.maketrans(). */
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3234 {"join", (PyCFunction)string_join, METH_O, join__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3235 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__},
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31042
diff changeset
3236 {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__},
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3237 {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3238 {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3239 {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3240 {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3241 {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3242 {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3243 {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3244 {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
3245 {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3246 {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3247 capitalize__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3248 {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3249 {"endswith", (PyCFunction)string_endswith, METH_VARARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3250 endswith__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3251 {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3252 {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3253 {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3254 {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3255 {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3256 {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3257 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3258 {"startswith", (PyCFunction)string_startswith, METH_VARARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3259 startswith__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3260 {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3261 {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3262 swapcase__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3263 {"translate", (PyCFunction)string_translate, METH_VARARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3264 translate__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3265 {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3266 {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3267 {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3268 {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3269 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3270 {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3271 {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3272 {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3273 expandtabs__doc__},
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3274 {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3275 splitlines__doc__},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27248
diff changeset
3276 {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS},
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3277 {NULL, NULL} /* sentinel */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3278 };
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3279
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24184
diff changeset
3280 static PyObject *
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3281 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3282
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3283 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3284 string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3285 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3286 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3287 static char *kwlist[] = {"object", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3288
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3289 if (type != &PyString_Type)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3290 return str_subtype_new(type, args, kwds);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3291 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3292 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3293 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3294 return PyString_FromString("");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3295 return PyObject_Str(x);
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3296 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3297
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3298 static PyObject *
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3299 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3300 {
19886
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3301 PyObject *tmp, *pnew;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3302 int n;
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3303
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3304 assert(PyType_IsSubtype(type, &PyString_Type));
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3305 tmp = string_new(&PyString_Type, args, kwds);
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3306 if (tmp == NULL)
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3307 return NULL;
19846
662e22f99639 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19632
diff changeset
3308 assert(PyString_CheckExact(tmp));
19886
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3309 n = PyString_GET_SIZE(tmp);
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3310 pnew = type->tp_alloc(type, n);
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3311 if (pnew != NULL) {
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3312 memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3313 ((PyStringObject *)pnew)->ob_shash =
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3314 ((PyStringObject *)tmp)->ob_shash;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
3315 ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED;
19886
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3316 }
19632
20fb7591f885 Fix a memory leak in str_subtype_new(). (All the other
Guido van Rossum <guido@python.org>
parents: 19592
diff changeset
3317 Py_DECREF(tmp);
19886
3b0da13ace48 str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents: 19884
diff changeset
3318 return pnew;
19592
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3319 }
85765e61deb0 Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents: 19489
diff changeset
3320
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3321 static PyObject *
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3322 basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3323 {
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3324 PyErr_SetString(PyExc_TypeError,
23597
ed007e47b3a1 Change name from string to basestring
Neal Norwitz <nnorwitz@gmail.com>
parents: 23542
diff changeset
3325 "The basestring type cannot be instantiated");
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3326 return NULL;
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3327 }
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3328
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3329 static PyObject *
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3330 string_mod(PyObject *v, PyObject *w)
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3331 {
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3332 if (!PyString_Check(v)) {
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3333 Py_INCREF(Py_NotImplemented);
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3334 return Py_NotImplemented;
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3335 }
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3336 return PyString_Format(v, w);
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3337 }
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3338
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3339 PyDoc_STRVAR(basestring_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3340 "Type basestring cannot be instantiated; it is the base for str and unicode.");
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3341
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3342 static PyNumberMethods string_as_number = {
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3343 0, /*nb_add*/
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3344 0, /*nb_subtract*/
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3345 0, /*nb_multiply*/
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3346 0, /*nb_divide*/
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3347 string_mod, /*nb_remainder*/
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3348 };
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3349
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3350
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3351 PyTypeObject PyBaseString_Type = {
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3352 PyObject_HEAD_INIT(&PyType_Type)
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3353 0,
23597
ed007e47b3a1 Change name from string to basestring
Neal Norwitz <nnorwitz@gmail.com>
parents: 23542
diff changeset
3354 "basestring",
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3355 0,
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3356 0,
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3357 0, /* tp_dealloc */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3358 0, /* tp_print */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3359 0, /* tp_getattr */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3360 0, /* tp_setattr */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3361 0, /* tp_compare */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3362 0, /* tp_repr */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3363 0, /* tp_as_number */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3364 0, /* tp_as_sequence */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3365 0, /* tp_as_mapping */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3366 0, /* tp_hash */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3367 0, /* tp_call */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3368 0, /* tp_str */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3369 0, /* tp_getattro */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3370 0, /* tp_setattro */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3371 0, /* tp_as_buffer */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3372 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3373 basestring_doc, /* tp_doc */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3374 0, /* tp_traverse */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3375 0, /* tp_clear */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3376 0, /* tp_richcompare */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3377 0, /* tp_weaklistoffset */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3378 0, /* tp_iter */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3379 0, /* tp_iternext */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3380 0, /* tp_methods */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3381 0, /* tp_members */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3382 0, /* tp_getset */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3383 &PyBaseObject_Type, /* tp_base */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3384 0, /* tp_dict */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3385 0, /* tp_descr_get */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3386 0, /* tp_descr_set */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3387 0, /* tp_dictoffset */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3388 0, /* tp_init */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3389 0, /* tp_alloc */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3390 basestring_new, /* tp_new */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3391 0, /* tp_free */
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3392 };
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3393
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3394 PyDoc_STRVAR(string_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3395 "str(object) -> string\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3396 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3397 Return a nice string representation of the object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23778
diff changeset
3398 If the argument is a string, the return value is the same object.");
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 10923
diff changeset
3399
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3400 PyTypeObject PyString_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3401 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3402 0,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3403 "str",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3404 sizeof(PyStringObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3405 sizeof(char),
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3406 (destructor)string_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3407 (printfunc)string_print, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3408 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3409 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3410 0, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3411 (reprfunc)string_repr, /* tp_repr */
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3412 &string_as_number, /* tp_as_number */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3413 &string_as_sequence, /* tp_as_sequence */
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23597
diff changeset
3414 &string_as_mapping, /* tp_as_mapping */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3415 (hashfunc)string_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3416 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3417 (reprfunc)string_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3418 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3419 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3420 &string_as_buffer, /* tp_as_buffer */
26273
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3421 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
57c6e84f71ce Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 26164
diff changeset
3422 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3423 string_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3424 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3425 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3426 (richcmpfunc)string_richcompare, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3427 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3428 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3429 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3430 string_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3431 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3432 0, /* tp_getset */
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23494
diff changeset
3433 &PyBaseString_Type, /* tp_base */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3434 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3435 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3436 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3437 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3438 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3439 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
3440 string_new, /* tp_new */
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
3441 PyObject_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3442 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3443
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3444 void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3445 PyString_Concat(register PyObject **pv, register PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3446 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3447 register PyObject *v;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3448 if (*pv == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3449 return;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3450 if (w == NULL || !PyString_Check(*pv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3451 Py_DECREF(*pv);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3452 *pv = NULL;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3453 return;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3454 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3455 v = string_concat((PyStringObject *) *pv, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3456 Py_DECREF(*pv);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3457 *pv = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3458 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3459
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3460 void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3461 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
3462 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3463 PyString_Concat(pv, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3464 Py_XDECREF(w);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3465 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3466
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3467
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3468 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3469 it changes the size of a string. We get away with this only if there
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3470 is only one module referencing the object. You can also think of it
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3471 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3472 more efficiently. In any case, don't use this if the string may
23295
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3473 already be known to some other part of the code...
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3474 Note that if there's not enough memory to resize the string, the original
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3475 string object at *pv is deallocated, *pv is set to NULL, an "out of
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3476 memory" exception is set, and -1 is returned. Else (on success) 0 is
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3477 returned, and the value in *pv may or may not be the same as on input.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3478 As always, an extra byte is allocated for a trailing \0 byte (newsize
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3479 does *not* include that), and a trailing \0 byte is stored.
6fa76b4e59fe Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents: 23225
diff changeset
3480 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3481
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3482 int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3483 _PyString_Resize(PyObject **pv, int newsize)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3484 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3485 register PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3486 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
3487 v = *pv;
25719
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
3488 if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0) {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3489 *pv = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3490 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3491 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
3492 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3493 }
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
3494 /* XXX UNREF/NEWREF interface should be more symmetrical */
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 23850
diff changeset
3495 _Py_DEC_REFTOTAL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3496 _Py_ForgetReference(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3497 *pv = (PyObject *)
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
3498 PyObject_REALLOC((char *)v,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3499 sizeof(PyStringObject) + newsize * sizeof(char));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3500 if (*pv == NULL) {
22977
4ba091cc70b8 Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
3501 PyObject_Del(v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3502 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
3503 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3504 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3505 _Py_NewReference(*pv);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3506 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
3507 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
3508 sv->ob_sval[newsize] = '\0';
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3509 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3510 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3511
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3512 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3513
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3514 static PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3515 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
3516 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3517 int argidx = *p_argidx;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3518 if (argidx < arglen) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3519 (*p_argidx)++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3520 if (arglen < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3521 return args;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3522 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3523 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
3524 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3525 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3526 "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
3527 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3528 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3529
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
3530 /* 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
3531 * 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
3532 * 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
3533 * 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
3534 * 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
3535 * 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
3536 */
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3537 #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
3538 #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
3539 #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
3540 #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
3541 #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
3542
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3543 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3544 formatfloat(char *buf, size_t buflen, int flags,
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3545 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
3546 {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3547 /* fmt = '%#.' + `prec` + `type`
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3548 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
3549 char fmt[20];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3550 double x;
24415
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3551 x = PyFloat_AsDouble(v);
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3552 if (x == -1.0 && PyErr_Occurred()) {
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3553 PyErr_SetString(PyExc_TypeError, "float argument required");
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3554 return -1;
24415
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3555 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3556 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3557 prec = 6;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3558 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
3559 type = 'g';
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3560 /* Worst case length calc to ensure no buffer overrun:
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3561
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3562 'g' formats:
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3563 fmt = %#.<prec>g
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3564 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
3565 for any double rep.)
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3566 len = 1 + prec + 1 + 2 + 5 = 9 + prec
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3567
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3568 'f' formats:
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3569 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3570 len = 1 + 50 + 1 + prec = 52 + prec
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3571
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3572 If prec=0 the effective precision is 1 (the leading digit is
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3573 always given), therefore increase the length by one.
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3574
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3575 */
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3576 if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) ||
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3577 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3578 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15082
diff changeset
3579 "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
3580 return -1;
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3581 }
26825
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3582 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3583 (flags&F_ALT) ? "#" : "",
e153961f961f Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents: 26818
diff changeset
3584 prec, type);
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 31350
diff changeset
3585 PyOS_ascii_formatd(buf, buflen, fmt, x);
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3586 return strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3587 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3588
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
3589 /* _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
3590 * 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
3591 * 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
3592 * 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
3593 * . *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
3594 * *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
3595 * 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
3596 * 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
3597 * "-"? ("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
3598 * "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
3599 * 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
3600 * 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
3601 * 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
3602 * 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
3603 * 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
3604 * 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
3605 * 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
3606 *
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3607 * 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
3608 * 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
3609 */
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3610 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
3611 _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
3612 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
3613 {
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3614 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
3615 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
3616 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
3617 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
3618 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
3619 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
3620 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
3621
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3622 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
3623 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
3624 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
3625 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
3626 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
3627 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
3628 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
3629 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
3630 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
3631 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
3632 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
3633 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
3634 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
3635 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
3636 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
3637 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3638 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
3639 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
3640
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3641 /* 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
3642 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
3643 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
3644 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
3645 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3646 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
3647 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
3648 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
3649 --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
3650 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
3651 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3652 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
3653 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
3654 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
3655 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
3656
17450
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
3657 /* 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
3658 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
3659 /* 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
3660 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
3661 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
3662 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
3663 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
3664 /* 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
3665 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
3666 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
3667 --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
3668 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3669 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
3670 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
3671 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
3672 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
3673 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
3674 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
3675 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
3676 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
3677 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3678 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
3679 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
3680 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
3681 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
3682 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
3683 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3684 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
3685 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
3686 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3687
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3688 /* 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
3689 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
3690 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
3691 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
3692 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
3693 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
3694 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
3695 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
3696 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3697 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
3698 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
3699 *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
3700 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
3701 *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
3702 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
3703 *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
3704 *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
3705 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
3706 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
3707 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
3708 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
3709 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3710
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3711 /* 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
3712 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
3713 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
3714 /* 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
3715 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
3716 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
3717 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
3718 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
3719 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
3720 /* 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
3721 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
3722 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
3723 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
3724 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3725 *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
3726 *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
3727 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
3728 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
3729
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3730 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3731 formatint(char *buf, size_t buflen, int flags,
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3732 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
3733 {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3734 /* 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
3735 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
3736 + 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
3737 char fmt[64]; /* plenty big enough! */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3738 char *sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3739 long x;
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3740
24415
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3741 x = PyInt_AsLong(v);
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3742 if (x == -1 && PyErr_Occurred()) {
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3743 PyErr_SetString(PyExc_TypeError, "int argument required");
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3744 return -1;
24415
f5cfa0ee02cf Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents: 24409
diff changeset
3745 }
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3746 if (x < 0 && type == 'u') {
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3747 type = 'd';
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24598
diff changeset
3748 }
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3749 if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3750 sign = "-";
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3751 else
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3752 sign = "";
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3753 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3754 prec = 1;
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3755
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3756 if ((flags & F_ALT) &&
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3757 (type == 'x' || type == 'X')) {
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3758 /* When converting under %#x or %#X, there are a number
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3759 * of issues that cause pain:
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3760 * - when 0 is being converted, the C standard leaves off
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3761 * the '0x' or '0X', which is inconsistent with other
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3762 * %#x/%#X conversions and inconsistent with Python's
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3763 * hex() function
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3764 * - there are platforms that violate the standard and
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3765 * convert 0 with the '0x' or '0X'
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3766 * (Metrowerks, Compaq Tru64)
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3767 * - there are platforms that give '0x' when converting
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3768 * under %#X, but convert 0 in accordance with the
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3769 * standard (OS/2 EMX)
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3770 *
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3771 * We can achieve the desired consistency by inserting our
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3772 * own '0x' or '0X' prefix, and substituting %x/%X in place
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3773 * of %#x/%#X.
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3774 *
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3775 * Note that this is the same approach as used in
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3776 * formatint() in unicodeobject.c
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3777 */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3778 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3779 sign, type, prec, type);
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3780 }
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3781 else {
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3782 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3783 sign, (flags&F_ALT) ? "#" : "",
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3784 prec, type);
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3785 }
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3786
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3787 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3788 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
22087
5b24a6ae2dc4 %#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 22053
diff changeset
3789 */
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3790 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3791 PyErr_SetString(PyExc_OverflowError,
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3792 "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
3793 return -1;
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3794 }
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3795 if (sign[0])
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3796 PyOS_snprintf(buf, buflen, fmt, -x);
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3797 else
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
3798 PyOS_snprintf(buf, buflen, fmt, x);
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3799 return strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3800 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3801
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3802 static int
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3803 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
3804 {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3805 /* 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
3806 if (PyString_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3807 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
3808 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3809 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3810 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3811 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
3812 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3813 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3814 buf[1] = '\0';
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
3815 return 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3816 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3817
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3818
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3819 /* 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
3820
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3821 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
3822 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
3823 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
3824 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
3825 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
3826 */
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3827 #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
3828
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3829 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
3830 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
3831 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3832 char *fmt, *res;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3833 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
3834 int args_owned = 0;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3835 PyObject *result, *orig_args;
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3836 #ifdef Py_USING_UNICODE
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3837 PyObject *v, *w;
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3838 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3839 PyObject *dict = NULL;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3840 if (format == NULL || !PyString_Check(format) || args == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3841 PyErr_BadInternalCall();
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3842 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3843 }
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3844 orig_args = args;
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
3845 fmt = PyString_AS_STRING(format);
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
3846 fmtcnt = PyString_GET_SIZE(format);
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
3847 reslen = rescnt = fmtcnt + 100;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3848 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
3849 if (result == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3850 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3851 res = PyString_AsString(result);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3852 if (PyTuple_Check(args)) {
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
3853 arglen = PyTuple_GET_SIZE(args);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3854 argidx = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3855 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3856 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3857 arglen = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3858 argidx = -2;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3859 }
26164
63a08639dae2 Fix SF # 635969, No error "not all arguments converted"
Neal Norwitz <nnorwitz@gmail.com>
parents: 25721
diff changeset
3860 if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) &&
63a08639dae2 Fix SF # 635969, No error "not all arguments converted"
Neal Norwitz <nnorwitz@gmail.com>
parents: 25721
diff changeset
3861 !PyObject_TypeCheck(args, &PyBaseString_Type))
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3862 dict = args;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3863 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3864 if (*fmt != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3865 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
3866 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
3867 reslen += rescnt;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3868 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
3869 return NULL;
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
3870 res = PyString_AS_STRING(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3871 + reslen - rescnt;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3872 --rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3873 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3874 *res++ = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3875 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3876 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3877 /* Got a format specifier */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3878 int flags = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3879 int width = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3880 int prec = -1;
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
3881 int c = '\0';
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3882 int fill;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3883 PyObject *v = NULL;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3884 PyObject *temp = NULL;
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
3885 char *pbuf;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3886 int sign;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3887 int len;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3888 char formatbuf[FORMATBUFLEN];
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
3889 /* For format{float,int,char}() */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3890 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
3891 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
3892 int argidx_start = argidx;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
3893 #endif
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3894
4588
4dc67fdd9e0b Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
3895 fmt++;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3896 if (*fmt == '(') {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3897 char *keystart;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3898 int keylen;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3899 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
3900 int pcount = 1;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3901
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3902 if (dict == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3903 PyErr_SetString(PyExc_TypeError,
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
3904 "format requires a mapping");
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3905 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3906 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3907 ++fmt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3908 --fmtcnt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3909 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
3910 /* 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
3911 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
3912 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
3913 --pcount;
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
3914 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
3915 ++pcount;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3916 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
3917 }
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
3918 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
3919 if (fmtcnt < 0 || pcount > 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3920 PyErr_SetString(PyExc_ValueError,
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3921 "incomplete format key");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3922 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3923 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3924 key = PyString_FromStringAndSize(keystart,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3925 keylen);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3926 if (key == NULL)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3927 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
3928 if (args_owned) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3929 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
3930 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
3931 }
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
3932 args = PyObject_GetItem(dict, key);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3933 Py_DECREF(key);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3934 if (args == NULL) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3935 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3936 }
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
3937 args_owned = 1;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3938 arglen = -1;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3939 argidx = -2;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
3940 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3941 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3942 switch (c = *fmt++) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3943 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
3944 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
3945 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
3946 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
3947 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
3948 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3949 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3950 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3951 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3952 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
3953 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3954 goto error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3955 if (!PyInt_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3956 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3957 "* wants int");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3958 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3959 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3960 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
3961 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
3962 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
3963 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
3964 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3965 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3966 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3967 }
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
3968 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
3969 width = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3970 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
3971 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
3972 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3973 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3974 if ((width*10) / 10 != width) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3975 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3976 PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3977 "width too big");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3978 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3979 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3980 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
3981 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3982 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3983 if (c == '.') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3984 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3985 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3986 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3987 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3988 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
3989 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3990 goto error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3991 if (!PyInt_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3992 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3993 PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3994 "* wants int");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3995 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3996 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3997 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
3998 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
3999 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4000 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4001 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4002 }
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
4003 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
4004 prec = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4005 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
4006 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
4007 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4008 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4009 if ((prec*10) / 10 != prec) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4010 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4011 PyExc_ValueError,
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4012 "prec too big");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4013 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4014 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4015 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
4016 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4017 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4018 } /* prec */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4019 if (fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4020 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
4021 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4022 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4023 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4024 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4025 if (fmtcnt < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4026 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4027 "incomplete format");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4028 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4029 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4030 if (c != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4031 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
4032 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4033 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4034 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4035 sign = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4036 fill = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4037 switch (c) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4038 case '%':
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4039 pbuf = "%";
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4040 len = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4041 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4042 case 's':
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4043 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4044 if (PyUnicode_Check(v)) {
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4045 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
4046 argidx = argidx_start;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4047 goto unicode;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4048 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4049 #endif
25682
ce49f12a1769 The string formatting code has a test to switch to Unicode when %s
Guido van Rossum <guido@python.org>
parents: 25659
diff changeset
4050 /* Fall through */
29529
f48bb94f3289 Fix whitespace.
Walter Dörwald <walter@livinglogic.de>
parents: 28575
diff changeset
4051 case 'r':
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
4052 if (c == 's')
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
4053 temp = PyObject_Str(v);
12371
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
4054 else
429a37ff9db5 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12328
diff changeset
4055 temp = PyObject_Repr(v);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
4056 if (temp == NULL)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4057 goto error;
8468
c27cd5fc46b5 Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
4058 if (!PyString_Check(temp)) {
25683
38843d35b646 Undo this part of the previous checkin:
Guido van Rossum <guido@python.org>
parents: 25682
diff changeset
4059 /* XXX Note: this should never happen,
38843d35b646 Undo this part of the previous checkin:
Guido van Rossum <guido@python.org>
parents: 25682
diff changeset
4060 since PyObject_Repr() and
38843d35b646 Undo this part of the previous checkin:
Guido van Rossum <guido@python.org>
parents: 25682
diff changeset
4061 PyObject_Str() assure this */
8468
c27cd5fc46b5 Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
4062 PyErr_SetString(PyExc_TypeError,
25683
38843d35b646 Undo this part of the previous checkin:
Guido van Rossum <guido@python.org>
parents: 25682
diff changeset
4063 "%s argument has non-string str()");
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
4064 Py_DECREF(temp);
8468
c27cd5fc46b5 Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
4065 goto error;
c27cd5fc46b5 Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
4066 }
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
4067 pbuf = PyString_AS_STRING(temp);
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
4068 len = PyString_GET_SIZE(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4069 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
4070 len = prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4071 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4072 case 'i':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4073 case 'd':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4074 case 'u':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4075 case 'o':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4076 case 'x':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4077 case 'X':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4078 if (c == 'i')
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4079 c = 'd';
15455
d0146d128897 Fox for SF bug #123859: %[duxXo] long formats inconsistent.
Tim Peters <tim.peters@gmail.com>
parents: 15435
diff changeset
4080 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
4081 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
4082 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
4083 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
4084 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
4085 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
4086 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4087 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
4088 pbuf = formatbuf;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
4089 len = formatint(pbuf,
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
4090 sizeof(formatbuf),
14713
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4091 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
4092 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
4093 goto error;
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 31021
diff changeset
4094 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
4095 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4096 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
4097 fill = '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4098 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4099 case 'e':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4100 case 'E':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4101 case 'f':
30216
7ab1a41f16da SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents: 29529
diff changeset
4102 case 'F':
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4103 case 'g':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4104 case 'G':
30216
7ab1a41f16da SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents: 29529
diff changeset
4105 if (c == 'F')
7ab1a41f16da SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents: 29529
diff changeset
4106 c = 'f';
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4107 pbuf = formatbuf;
23046
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
4108 len = formatfloat(pbuf, sizeof(formatbuf),
769e0a538e54 Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents: 23045
diff changeset
4109 flags, prec, c, v);
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
4110 if (len < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4111 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4112 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
4113 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
4114 fill = '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4115 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4116 case 'c':
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4117 #ifdef Py_USING_UNICODE
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4118 if (PyUnicode_Check(v)) {
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4119 fmt = fmt_start;
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4120 argidx = argidx_start;
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4121 goto unicode;
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4122 }
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 27521
diff changeset
4123 #endif
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4124 pbuf = formatbuf;
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4125 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
4126 if (len < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4127 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4128 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4129 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
4130 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
4131 "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
4132 "at index %i",
25233
8d751b62ecc4 Fix warnings on 64-bit platforms about casts from pointers to ints.
Guido van Rossum <guido@python.org>
parents: 25173
diff changeset
4133 c, c,
8d751b62ecc4 Fix warnings on 64-bit platforms about casts from pointers to ints.
Guido van Rossum <guido@python.org>
parents: 25173
diff changeset
4134 (int)(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
4135 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4136 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4137 if (sign) {
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4138 if (*pbuf == '-' || *pbuf == '+') {
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4139 sign = *pbuf++;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4140 len--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4141 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4142 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
4143 sign = '+';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4144 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
4145 sign = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4146 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
4147 sign = 0;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4148 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4149 if (width < len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4150 width = len;
25719
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
4151 if (rescnt - (sign != 0) < width) {
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
4152 reslen -= rescnt;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
4153 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
4154 reslen += rescnt;
25719
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
4155 if (reslen < 0) {
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
4156 Py_DECREF(result);
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
4157 return PyErr_NoMemory();
f532e9b69551 Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents: 25683
diff changeset
4158 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4159 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
4160 return NULL;
21254
19186b17d25a Little stuff.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21149
diff changeset
4161 res = PyString_AS_STRING(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4162 + reslen - rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4163 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4164 if (sign) {
1409
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
4165 if (fill != ' ')
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
4166 *res++ = sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4167 rescnt--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4168 if (width > len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4169 width--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4170 }
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
4171 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
4172 assert(pbuf[0] == '0');
17450
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4173 assert(pbuf[1] == c);
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4174 if (fill != ' ') {
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4175 *res++ = *pbuf++;
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4176 *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
4177 }
17450
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4178 rescnt -= 2;
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4179 width -= 2;
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4180 if (width < 0)
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4181 width = 0;
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4182 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
4183 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4184 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
4185 do {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4186 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4187 *res++ = fill;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4188 } while (--width > len);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4189 }
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
4190 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
4191 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
4192 *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
4193 if ((flags & F_ALT) &&
17450
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4194 (c == 'x' || c == 'X')) {
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4195 assert(pbuf[0] == '0');
f74f5aafdc7e Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents: 17425
diff changeset
4196 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
4197 *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
4198 *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
4199 }
58b832b8745e Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents: 14696
diff changeset
4200 }
13196
18920eaa1226 Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents: 12966
diff changeset
4201 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
4202 res += len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4203 rescnt -= len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4204 while (--width >= len) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4205 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4206 *res++ = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4207 }
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
4208 if (dict && (argidx < arglen) && c != '%') {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4209 PyErr_SetString(PyExc_TypeError,
23494
76f376bea5e5 Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents: 23430
diff changeset
4210 "not all arguments converted during string formatting");
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
4211 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
4212 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4213 Py_XDECREF(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4214 } /* '%' */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4215 } /* 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
4216 if (argidx < arglen && !dict) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4217 PyErr_SetString(PyExc_TypeError,
23494
76f376bea5e5 Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents: 23430
diff changeset
4218 "not all arguments converted during string formatting");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4219 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4220 }
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6286
diff changeset
4221 if (args_owned) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4222 Py_DECREF(args);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6286
diff changeset
4223 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4224 _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
4225 return result;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4226
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4227 #ifdef Py_USING_UNICODE
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4228 unicode:
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4229 if (args_owned) {
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4230 Py_DECREF(args);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4231 args_owned = 0;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4232 }
17655
0470babdae49 Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents: 17645
diff changeset
4233 /* Fiddle args right (remove the first argidx arguments) */
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4234 if (PyTuple_Check(orig_args) && argidx > 0) {
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4235 PyObject *v;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4236 int n = PyTuple_GET_SIZE(orig_args) - argidx;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4237 v = PyTuple_New(n);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4238 if (v == NULL)
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4239 goto error;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4240 while (--n >= 0) {
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4241 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4242 Py_INCREF(w);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4243 PyTuple_SET_ITEM(v, n, w);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4244 }
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4245 args = v;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4246 } else {
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4247 Py_INCREF(orig_args);
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4248 args = orig_args;
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4249 }
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
4250 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
4251 /* 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
4252 function format the rest of the input. */
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4253 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
4254 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
4255 goto error;
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4256 fmtcnt = PyString_GET_SIZE(format) - \
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4257 (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
4258 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
4259 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
4260 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
4261 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
4262 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
4263 if (v == NULL)
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4264 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
4265 /* 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
4266 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
4267 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
4268 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
4269 Py_DECREF(v);
12328
4f5671a31347 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12025
diff changeset
4270 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
4271 return w;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
4272 #endif /* Py_USING_UNICODE */
17640
9934225fe297 A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
4273
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4274 error:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4275 Py_DECREF(result);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6286
diff changeset
4276 if (args_owned) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4277 Py_DECREF(args);
7981
09963a2a24db Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6286
diff changeset
4278 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4279 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
4280 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4281
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4282 void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4283 PyString_InternInPlace(PyObject **p)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4284 {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4285 register PyStringObject *s = (PyStringObject *)(*p);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4286 PyObject *t;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4287 if (s == NULL || !PyString_Check(s))
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4288 Py_FatalError("PyString_InternInPlace: strings only please!");
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4289 if (PyString_CHECK_INTERNED(s))
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4290 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4291 if (interned == NULL) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4292 interned = PyDict_New();
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4293 if (interned == NULL) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4294 PyErr_Clear(); /* Don't leave an exception */
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4295 return;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4296 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4297 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4298 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4299 Py_INCREF(t);
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4300 Py_DECREF(*p);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4301 *p = t;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4302 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4303 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4304 /* Ensure that only true string objects appear in the intern dict */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4305 if (!PyString_CheckExact(s)) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4306 t = PyString_FromStringAndSize(PyString_AS_STRING(s),
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4307 PyString_GET_SIZE(s));
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4308 if (t == NULL) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4309 PyErr_Clear();
19887
b28fc8c36e3d If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents: 19886
diff changeset
4310 return;
b28fc8c36e3d If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents: 19886
diff changeset
4311 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4312 } else {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4313 t = (PyObject*) s;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4314 Py_INCREF(t);
19887
b28fc8c36e3d If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents: 19886
diff changeset
4315 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4316
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4317 if (PyDict_SetItem(interned, t, t) == 0) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4318 /* The two references in interned are not counted by
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4319 refcnt. The string deallocator will take care of this */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4320 ((PyObject *)t)->ob_refcnt-=2;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4321 PyString_CHECK_INTERNED(t) = SSTATE_INTERNED_MORTAL;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4322 Py_DECREF(*p);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4323 *p = t;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4324 return;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4325 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4326 Py_DECREF(t);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4327 PyErr_Clear();
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4328 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4329
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4330 void
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4331 PyString_InternImmortal(PyObject **p)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4332 {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4333 PyString_InternInPlace(p);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4334 if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4335 PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4336 Py_INCREF(*p);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4337 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4338 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4339
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4340
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4341 PyObject *
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4342 PyString_InternFromString(const char *cp)
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4343 {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4344 PyObject *s = PyString_FromString(cp);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4345 if (s == NULL)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4346 return NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4347 PyString_InternInPlace(&s);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4348 return s;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4349 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
4350
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4351 void
13459
0f95c93f200b ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13377
diff changeset
4352 PyString_Fini(void)
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4353 {
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4354 int i;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4355 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
4356 Py_XDECREF(characters[i]);
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4357 characters[i] = NULL;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4358 }
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4359 Py_XDECREF(nullstring);
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4360 nullstring = NULL;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
4361 }
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4362
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4363 void _Py_ReleaseInternedStrings(void)
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4364 {
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4365 PyObject *keys;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4366 PyStringObject *s;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4367 int i, n;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4368
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4369 if (interned == NULL || !PyDict_Check(interned))
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4370 return;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4371 keys = PyDict_Keys(interned);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4372 if (keys == NULL || !PyList_Check(keys)) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4373 PyErr_Clear();
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4374 return;
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4375 }
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4376
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4377 /* Since _Py_ReleaseInternedStrings() is intended to help a leak
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4378 detector, interned strings are not forcibly deallocated; rather, we
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4379 give them their stolen references back, and then clear and DECREF
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4380 the interned dict. */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4381
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4382 fprintf(stderr, "releasing interned strings\n");
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4383 n = PyList_GET_SIZE(keys);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4384 for (i = 0; i < n; i++) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4385 s = (PyStringObject *) PyList_GET_ITEM(keys, i);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4386 switch (s->ob_sstate) {
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4387 case SSTATE_NOT_INTERNED:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4388 /* XXX Shouldn't happen */
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4389 break;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4390 case SSTATE_INTERNED_IMMORTAL:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4391 s->ob_refcnt += 1;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4392 break;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4393 case SSTATE_INTERNED_MORTAL:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4394 s->ob_refcnt += 2;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4395 break;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4396 default:
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4397 Py_FatalError("Inconsistent interned string state.");
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4398 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4399 s->ob_sstate = SSTATE_NOT_INTERNED;
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4400 }
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4401 Py_DECREF(keys);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4402 PyDict_Clear(interned);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4403 Py_DECREF(interned);
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24898
diff changeset
4404 interned = NULL;
16751
ca1be7d00413 _Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents: 16226
diff changeset
4405 }