Mercurial > cpython-lru_cache-weakref
annotate Modules/stropmodule.c @ 39940:dfd809dcf680 2.5
Bug #1446043: correctly raise a LookupError if an encoding name given
to encodings.search_function() contains a dot.
(backport from rev. 52075)
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 30 Sep 2006 11:22:35 +0000 |
| parents | 3f9739de6603 |
| children | 8cbe0bd93c28 4fe192d48960 |
| rev | line source |
|---|---|
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* strop module */ |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
| 36557 | 3 #define PY_SSIZE_T_CLEAN |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
4 #include "Python.h" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
5 #include <ctype.h> |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
6 |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
7 PyDoc_STRVAR(strop_module__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
8 "Common string manipulations, optimized for speed.\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
9 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
10 "Always use \"import string\" rather than referencing\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
11 "this module directly."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
12 |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
13 /* XXX This file assumes that the <ctype.h> is*() functions |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
14 XXX are defined for all 8-bit characters! */ |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
15 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
16 #define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \ |
|
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
17 "strop functions are obsolete; use string methods")) \ |
|
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
18 return NULL |
|
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
19 |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
20 /* The lstrip(), rstrip() and strip() functions are implemented |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
21 in do_strip(), which uses an additional parameter to indicate what |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
22 type of strip should occur. */ |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
23 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
24 #define LEFTSTRIP 0 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
25 #define RIGHTSTRIP 1 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
26 #define BOTHSTRIP 2 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
27 |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
| 4617 | 29 static PyObject * |
| 36557 | 30 split_whitespace(char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 { |
| 36557 | 32 Py_ssize_t i = 0, j; |
| 33 int err; | |
| 34 Py_ssize_t countsplit = 0; | |
| 4889 | 35 PyObject* item; |
| 36 PyObject *list = PyList_New(0); | |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 if (list == NULL) |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 while (i < len) { |
| 2310 | 42 while (i < len && isspace(Py_CHARMASK(s[i]))) { |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 i = i+1; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 j = i; |
|
2335
e11e6b358531
fix stupid bug in strip and split
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
46 while (i < len && !isspace(Py_CHARMASK(s[i]))) { |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 i = i+1; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 if (j < i) { |
| 36557 | 50 item = PyString_FromStringAndSize(s+j, i-j); |
| 4889 | 51 if (item == NULL) |
| 52 goto finally; | |
| 53 | |
| 4617 | 54 err = PyList_Append(list, item); |
| 55 Py_DECREF(item); | |
| 4889 | 56 if (err < 0) |
| 57 goto finally; | |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
58 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
59 countsplit++; |
|
6696
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
60 while (i < len && isspace(Py_CHARMASK(s[i]))) { |
|
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
61 i = i+1; |
|
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
62 } |
|
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
63 if (maxsplit && (countsplit >= maxsplit) && i < len) { |
| 4617 | 64 item = PyString_FromStringAndSize( |
| 36557 | 65 s+i, len - i); |
| 4889 | 66 if (item == NULL) |
| 67 goto finally; | |
| 68 | |
| 4617 | 69 err = PyList_Append(list, item); |
| 70 Py_DECREF(item); | |
| 4889 | 71 if (err < 0) |
| 72 goto finally; | |
| 73 | |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
74 i = len; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
75 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 return list; |
| 4889 | 79 finally: |
| 80 Py_DECREF(list); | |
| 81 return NULL; | |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
85 PyDoc_STRVAR(splitfields__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
86 "split(s [,sep [,maxsplit]]) -> list of strings\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
87 "splitfields(s [,sep [,maxsplit]]) -> list of strings\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
88 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
89 "Return a list of the words in the string s, using sep as the\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
90 "delimiter string. If maxsplit is nonzero, splits into at most\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
91 "maxsplit words. If sep is not specified, any whitespace string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
92 "is a separator. Maxsplit defaults to 0.\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
93 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
94 "(split and splitfields are synonymous)"); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
95 |
| 4617 | 96 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
97 strop_splitfields(PyObject *self, PyObject *args) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 { |
| 36557 | 99 Py_ssize_t len, n, i, j, err; |
| 100 Py_ssize_t splitcount, maxsplit; | |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 char *s, *sub; |
| 4617 | 102 PyObject *list, *item; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
104 WARN; |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
105 sub = NULL; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
106 n = 0; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
107 splitcount = 0; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
108 maxsplit = 0; |
| 36557 | 109 if (!PyArg_ParseTuple(args, "t#|z#n:split", &s, &len, &sub, &n, &maxsplit)) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 return NULL; |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
111 if (sub == NULL) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
112 return split_whitespace(s, len, maxsplit); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 if (n == 0) { |
| 4617 | 114 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
| 4617 | 118 list = PyList_New(0); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 if (list == NULL) |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 i = j = 0; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 while (i+n <= len) { |
|
4352
6ec0caeaf823
Replace all uses of strncmp (in split, find, rfind) with memcmp, so
Guido van Rossum <guido@python.org>
parents:
4296
diff
changeset
|
124 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) { |
| 36557 | 125 item = PyString_FromStringAndSize(s+j, i-j); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
126 if (item == NULL) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
127 goto fail; |
| 4617 | 128 err = PyList_Append(list, item); |
| 129 Py_DECREF(item); | |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
130 if (err < 0) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
131 goto fail; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 i = j = i + n; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
133 splitcount++; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
134 if (maxsplit && (splitcount >= maxsplit)) |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
135 break; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 else |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 i++; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 } |
| 36557 | 140 item = PyString_FromStringAndSize(s+j, len-j); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
141 if (item == NULL) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
142 goto fail; |
| 4617 | 143 err = PyList_Append(list, item); |
| 144 Py_DECREF(item); | |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
145 if (err < 0) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
146 goto fail; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 return list; |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
149 |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
150 fail: |
| 4617 | 151 Py_DECREF(list); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
152 return NULL; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
156 PyDoc_STRVAR(joinfields__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
157 "join(list [,sep]) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
158 "joinfields(list [,sep]) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
159 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
160 "Return a string composed of the words in list, with\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
161 "intervening occurrences of sep. Sep defaults to a single\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
162 "space.\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
163 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
164 "(join and joinfields are synonymous)"); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
165 |
| 4617 | 166 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
167 strop_joinfields(PyObject *self, PyObject *args) |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
168 { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
169 PyObject *seq; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
170 char *sep = NULL; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
171 Py_ssize_t seqlen, seplen = 0; |
|
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
172 Py_ssize_t i, reslen = 0, slen = 0, sz = 100; |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
173 PyObject *res = NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
174 char* p = NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
175 ssizeargfunc getitemfunc; |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
176 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
177 WARN; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
178 if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen)) |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
179 return NULL; |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
180 if (sep == NULL) { |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
181 sep = " "; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
182 seplen = 1; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
183 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
184 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13494
diff
changeset
|
185 seqlen = PySequence_Size(seq); |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
186 if (seqlen < 0 && PyErr_Occurred()) |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
187 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
188 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
189 if (seqlen == 1) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
190 /* Optimization if there's only one item */ |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
191 PyObject *item = PySequence_GetItem(seq, 0); |
|
7214
6222d3371fb8
Forgot to return NULL in joinfields() when a type error was detected
Guido van Rossum <guido@python.org>
parents:
7001
diff
changeset
|
192 if (item && !PyString_Check(item)) { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
193 PyErr_SetString(PyExc_TypeError, |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
194 "first argument must be sequence of strings"); |
|
9402
9ccaecd3dea3
Add a missing DECREF in an error exit. Submitted by Jonathan Giddy.
Guido van Rossum <guido@python.org>
parents:
9307
diff
changeset
|
195 Py_DECREF(item); |
|
7214
6222d3371fb8
Forgot to return NULL in joinfields() when a type error was detected
Guido van Rossum <guido@python.org>
parents:
7001
diff
changeset
|
196 return NULL; |
|
6222d3371fb8
Forgot to return NULL in joinfields() when a type error was detected
Guido van Rossum <guido@python.org>
parents:
7001
diff
changeset
|
197 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
198 return item; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
199 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
200 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
201 if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
202 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
203 p = PyString_AsString(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
204 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
205 /* optimize for lists, since it's the most common case. all others |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
206 * (tuples and arbitrary sequences) just use the sequence abstract |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
207 * interface. |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
208 */ |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
209 if (PyList_Check(seq)) { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
210 for (i = 0; i < seqlen; i++) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
211 PyObject *item = PyList_GET_ITEM(seq, i); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
212 if (!PyString_Check(item)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
213 PyErr_SetString(PyExc_TypeError, |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
214 "first argument must be sequence of strings"); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
215 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
216 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
217 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
218 slen = PyString_GET_SIZE(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
219 while (reslen + slen + seplen >= sz) { |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
22771
diff
changeset
|
220 if (_PyString_Resize(&res, sz * 2) < 0) |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
221 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
222 sz *= 2; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
223 p = PyString_AsString(res) + reslen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
224 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
225 if (i > 0) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
226 memcpy(p, sep, seplen); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
227 p += seplen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
228 reslen += seplen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
229 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
230 memcpy(p, PyString_AS_STRING(item), slen); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
231 p += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
232 reslen += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
233 } |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
22771
diff
changeset
|
234 _PyString_Resize(&res, reslen); |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
235 return res; |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
236 } |
|
8405
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
237 |
|
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
238 if (seq->ob_type->tp_as_sequence == NULL || |
|
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
239 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL) |
|
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
240 { |
| 4617 | 241 PyErr_SetString(PyExc_TypeError, |
| 4889 | 242 "first argument must be a sequence"); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
243 return NULL; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
244 } |
|
8405
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
245 /* This is now type safe */ |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
246 for (i = 0; i < seqlen; i++) { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
247 PyObject *item = getitemfunc(seq, i); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
248 if (!item || !PyString_Check(item)) { |
| 4617 | 249 PyErr_SetString(PyExc_TypeError, |
| 4889 | 250 "first argument must be sequence of strings"); |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
251 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
252 Py_XDECREF(item); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
253 return NULL; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
254 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
255 slen = PyString_GET_SIZE(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
256 while (reslen + slen + seplen >= sz) { |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
22771
diff
changeset
|
257 if (_PyString_Resize(&res, sz * 2) < 0) { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
258 Py_DECREF(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
259 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
260 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
261 sz *= 2; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
262 p = PyString_AsString(res) + reslen; |
| 4889 | 263 } |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
264 if (i > 0) { |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
265 memcpy(p, sep, seplen); |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
266 p += seplen; |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
267 reslen += seplen; |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
268 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
269 memcpy(p, PyString_AS_STRING(item), slen); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
270 p += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
271 reslen += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
272 Py_DECREF(item); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
273 } |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
22771
diff
changeset
|
274 _PyString_Resize(&res, reslen); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
275 return res; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
276 } |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
277 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
278 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
279 PyDoc_STRVAR(find__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
280 "find(s, sub [,start [,end]]) -> in\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
281 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
282 "Return the lowest index in s where substring sub is found,\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
283 "such that sub is contained within s[start,end]. Optional\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
284 "arguments start and end are interpreted as in slice notation.\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
285 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
286 "Return -1 on failure."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
287 |
| 4617 | 288 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
289 strop_find(PyObject *self, PyObject *args) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 { |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 char *s, *sub; |
| 36557 | 292 Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX; |
| 4889 | 293 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
294 WARN; |
| 36557 | 295 if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last)) |
| 4889 | 296 return NULL; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
298 if (last > len) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
299 last = len; |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
300 if (last < 0) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
301 last += len; |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
302 if (last < 0) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
303 last = 0; |
| 4889 | 304 if (i < 0) |
| 305 i += len; | |
| 306 if (i < 0) | |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 i = 0; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 |
|
7733
30e331496930
Check for boundary errors in [r]find -- find("x", "", 2) should return -1.
Guido van Rossum <guido@python.org>
parents:
7214
diff
changeset
|
309 if (n == 0 && i <= last) |
| 4617 | 310 return PyInt_FromLong((long)i); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
312 last -= n; |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
313 for (; i <= last; ++i) |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
314 if (s[i] == sub[0] && |
|
4352
6ec0caeaf823
Replace all uses of strncmp (in split, find, rfind) with memcmp, so
Guido van Rossum <guido@python.org>
parents:
4296
diff
changeset
|
315 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
| 4617 | 316 return PyInt_FromLong((long)i); |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
317 |
| 4617 | 318 return PyInt_FromLong(-1L); |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
319 } |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
320 |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
321 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
322 PyDoc_STRVAR(rfind__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
323 "rfind(s, sub [,start [,end]]) -> int\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
324 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
325 "Return the highest index in s where substring sub is found,\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
326 "such that sub is contained within s[start,end]. Optional\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
327 "arguments start and end are interpreted as in slice notation.\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
328 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
329 "Return -1 on failure."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
330 |
| 4617 | 331 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
332 strop_rfind(PyObject *self, PyObject *args) |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
333 { |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
334 char *s, *sub; |
| 36557 | 335 Py_ssize_t len, n, j; |
|
37671
3f9739de6603
Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37666
diff
changeset
|
336 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
337 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
338 WARN; |
| 36557 | 339 if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last)) |
| 4889 | 340 return NULL; |
| 341 | |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
342 if (last > len) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
343 last = len; |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
344 if (last < 0) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
345 last += len; |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
346 if (last < 0) |
|
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
347 last = 0; |
| 4889 | 348 if (i < 0) |
| 349 i += len; | |
| 350 if (i < 0) | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
351 i = 0; |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
352 |
|
7733
30e331496930
Check for boundary errors in [r]find -- find("x", "", 2) should return -1.
Guido van Rossum <guido@python.org>
parents:
7214
diff
changeset
|
353 if (n == 0 && i <= last) |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
354 return PyInt_FromLong((long)last); |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
355 |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
356 for (j = last-n; j >= i; --j) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
357 if (s[j] == sub[0] && |
|
4352
6ec0caeaf823
Replace all uses of strncmp (in split, find, rfind) with memcmp, so
Guido van Rossum <guido@python.org>
parents:
4296
diff
changeset
|
358 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0)) |
| 4617 | 359 return PyInt_FromLong((long)j); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 |
| 4617 | 361 return PyInt_FromLong(-1L); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
364 |
| 4617 | 365 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
366 do_strip(PyObject *args, int striptype) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
367 { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
368 char *s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
369 Py_ssize_t len, i, j; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
370 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
371 |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
372 if (PyString_AsStringAndSize(args, &s, &len)) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
373 return NULL; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
374 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
375 i = 0; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
376 if (striptype != RIGHTSTRIP) { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
377 while (i < len && isspace(Py_CHARMASK(s[i]))) { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
378 i++; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
379 } |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
380 } |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
381 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
382 j = len; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
383 if (striptype != LEFTSTRIP) { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
384 do { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
385 j--; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
386 } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
387 j++; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
388 } |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
389 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
390 if (i == 0 && j == len) { |
| 4617 | 391 Py_INCREF(args); |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
392 return args; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
393 } |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
394 else |
| 4617 | 395 return PyString_FromStringAndSize(s+i, j-i); |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
396 } |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
397 |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
399 PyDoc_STRVAR(strip__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
400 "strip(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
401 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
402 "Return a copy of the string s with leading and trailing\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
403 "whitespace removed."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
404 |
| 4617 | 405 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
406 strop_strip(PyObject *self, PyObject *args) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 { |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
408 WARN; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
409 return do_strip(args, BOTHSTRIP); |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
410 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
412 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
413 PyDoc_STRVAR(lstrip__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
414 "lstrip(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
415 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
416 "Return a copy of the string s with leading whitespace removed."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
417 |
| 4617 | 418 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
419 strop_lstrip(PyObject *self, PyObject *args) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
420 { |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
421 WARN; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
422 return do_strip(args, LEFTSTRIP); |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
423 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
425 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
426 PyDoc_STRVAR(rstrip__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
427 "rstrip(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
428 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
429 "Return a copy of the string s with trailing whitespace removed."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
430 |
| 4617 | 431 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
432 strop_rstrip(PyObject *self, PyObject *args) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
433 { |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
434 WARN; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
435 return do_strip(args, RIGHTSTRIP); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
439 PyDoc_STRVAR(lower__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
440 "lower(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
441 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
442 "Return a copy of the string s converted to lowercase."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
443 |
| 4617 | 444 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
445 strop_lower(PyObject *self, PyObject *args) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
446 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
447 char *s, *s_new; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
448 Py_ssize_t i, n; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
449 PyObject *newstr; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
450 int changed; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
451 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
452 WARN; |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
453 if (PyString_AsStringAndSize(args, &s, &n)) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
454 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
455 newstr = PyString_FromStringAndSize(NULL, n); |
|
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
456 if (newstr == NULL) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
457 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
458 s_new = PyString_AsString(newstr); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
459 changed = 0; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
460 for (i = 0; i < n; i++) { |
| 2310 | 461 int c = Py_CHARMASK(*s++); |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
462 if (isupper(c)) { |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
463 changed = 1; |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
464 *s_new = tolower(c); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
465 } else |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
466 *s_new = c; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
467 s_new++; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
468 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
469 if (!changed) { |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
470 Py_DECREF(newstr); |
| 4617 | 471 Py_INCREF(args); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
472 return args; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
473 } |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
474 return newstr; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
475 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
476 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
477 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
478 PyDoc_STRVAR(upper__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
479 "upper(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
480 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
481 "Return a copy of the string s converted to uppercase."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
482 |
| 4617 | 483 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
484 strop_upper(PyObject *self, PyObject *args) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
485 { |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
486 char *s, *s_new; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
487 Py_ssize_t i, n; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
488 PyObject *newstr; |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
489 int changed; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
490 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
491 WARN; |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
492 if (PyString_AsStringAndSize(args, &s, &n)) |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
493 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
494 newstr = PyString_FromStringAndSize(NULL, n); |
|
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
495 if (newstr == NULL) |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
496 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
497 s_new = PyString_AsString(newstr); |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
498 changed = 0; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
499 for (i = 0; i < n; i++) { |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
500 int c = Py_CHARMASK(*s++); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
501 if (islower(c)) { |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
502 changed = 1; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
503 *s_new = toupper(c); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
504 } else |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
505 *s_new = c; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
506 s_new++; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
507 } |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
508 if (!changed) { |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
509 Py_DECREF(newstr); |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
510 Py_INCREF(args); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
511 return args; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
512 } |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
513 return newstr; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
514 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
515 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
516 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
517 PyDoc_STRVAR(capitalize__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
518 "capitalize(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
519 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
520 "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:
23295
diff
changeset
|
521 "capitalized."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
522 |
| 4617 | 523 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
524 strop_capitalize(PyObject *self, PyObject *args) |
| 3676 | 525 { |
| 526 char *s, *s_new; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
527 Py_ssize_t i, n; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
528 PyObject *newstr; |
| 3676 | 529 int changed; |
| 530 | |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
531 WARN; |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
532 if (PyString_AsStringAndSize(args, &s, &n)) |
| 3676 | 533 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
534 newstr = PyString_FromStringAndSize(NULL, n); |
|
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
535 if (newstr == NULL) |
| 3676 | 536 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
537 s_new = PyString_AsString(newstr); |
| 3676 | 538 changed = 0; |
|
3679
b8980735d03b
Test for zero-length argument in capitalize().
Guido van Rossum <guido@python.org>
parents:
3676
diff
changeset
|
539 if (0 < n) { |
| 3676 | 540 int c = Py_CHARMASK(*s++); |
| 541 if (islower(c)) { | |
| 542 changed = 1; | |
| 543 *s_new = toupper(c); | |
| 544 } else | |
| 545 *s_new = c; | |
| 546 s_new++; | |
| 547 } | |
| 548 for (i = 1; i < n; i++) { | |
| 549 int c = Py_CHARMASK(*s++); | |
| 550 if (isupper(c)) { | |
| 551 changed = 1; | |
| 552 *s_new = tolower(c); | |
| 553 } else | |
| 554 *s_new = c; | |
| 555 s_new++; | |
| 556 } | |
| 557 if (!changed) { | |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
558 Py_DECREF(newstr); |
| 4617 | 559 Py_INCREF(args); |
| 3676 | 560 return args; |
| 561 } | |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
562 return newstr; |
| 3676 | 563 } |
| 564 | |
| 565 | |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
566 PyDoc_STRVAR(expandtabs__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
567 "expandtabs(string, [tabsize]) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
568 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
569 "Expand tabs in a string, i.e. replace them by one or more spaces,\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
570 "depending on the current column and the given tab size (default 8).\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
571 "The column number is reset to zero after each newline occurring in the\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
572 "string. This doesn't understand other non-printing characters."); |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
573 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
574 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
575 strop_expandtabs(PyObject *self, PyObject *args) |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
576 { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
577 /* Original by Fredrik Lundh */ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
578 char* e; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
579 char* p; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
580 char* q; |
| 36557 | 581 Py_ssize_t i, j; |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
582 PyObject* out; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
583 char* string; |
| 36557 | 584 Py_ssize_t stringlen; |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
585 int tabsize = 8; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
586 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
587 WARN; |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
588 /* Get arguments */ |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
589 if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize)) |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
590 return NULL; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
591 if (tabsize < 1) { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
592 PyErr_SetString(PyExc_ValueError, |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
593 "tabsize must be at least 1"); |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
594 return NULL; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
595 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
596 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
597 /* First pass: determine size of output string */ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
598 i = j = 0; /* j: current column; i: total of previous lines */ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
599 e = string + stringlen; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
600 for (p = string; p < e; p++) { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
601 if (*p == '\t') |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
602 j += tabsize - (j%tabsize); |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
603 else { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
604 j++; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
605 if (*p == '\n') { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
606 i += j; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
607 j = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
608 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
609 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
610 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
611 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
612 /* Second pass: create output string and fill it */ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
613 out = PyString_FromStringAndSize(NULL, i+j); |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
614 if (out == NULL) |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
615 return NULL; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
616 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
617 i = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
618 q = PyString_AS_STRING(out); |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
619 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
620 for (p = string; p < e; p++) { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
621 if (*p == '\t') { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
622 j = tabsize - (i%tabsize); |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
623 i += j; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
624 while (j-- > 0) |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
625 *q++ = ' '; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
626 } else { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
627 *q++ = *p; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
628 i++; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
629 if (*p == '\n') |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
630 i = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
631 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
632 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
633 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
634 return out; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
635 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
636 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
637 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
638 PyDoc_STRVAR(count__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
639 "count(s, sub[, start[, end]]) -> int\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
640 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
641 "Return the number of occurrences of substring sub in string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
642 "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:
23295
diff
changeset
|
643 "interpreted as in slice notation."); |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
644 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
645 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
646 strop_count(PyObject *self, PyObject *args) |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
647 { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
648 char *s, *sub; |
| 36557 | 649 Py_ssize_t len, n; |
|
37671
3f9739de6603
Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37666
diff
changeset
|
650 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; |
| 36557 | 651 Py_ssize_t m, r; |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
652 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
653 WARN; |
| 36557 | 654 if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last)) |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
655 return NULL; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
656 if (last > len) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
657 last = len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
658 if (last < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
659 last += len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
660 if (last < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
661 last = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
662 if (i < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
663 i += len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
664 if (i < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
665 i = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
666 m = last + 1 - n; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
667 if (n == 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
668 return PyInt_FromLong((long) (m-i)); |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
669 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
670 r = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
671 while (i < m) { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
672 if (!memcmp(s+i, sub, n)) { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
673 r++; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
674 i += n; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
675 } else { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
676 i++; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
677 } |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
678 } |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
679 return PyInt_FromLong((long) r); |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
680 } |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
681 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
682 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
683 PyDoc_STRVAR(swapcase__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
684 "swapcase(s) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
685 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
686 "Return a copy of the string s with upper case characters\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
687 "converted to lowercase and vice versa."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
688 |
| 4617 | 689 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
690 strop_swapcase(PyObject *self, PyObject *args) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
691 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
692 char *s, *s_new; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
693 Py_ssize_t i, n; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
694 PyObject *newstr; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
695 int changed; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
696 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
697 WARN; |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
698 if (PyString_AsStringAndSize(args, &s, &n)) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
699 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
700 newstr = PyString_FromStringAndSize(NULL, n); |
|
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
701 if (newstr == NULL) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
702 return NULL; |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
703 s_new = PyString_AsString(newstr); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
704 changed = 0; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
705 for (i = 0; i < n; i++) { |
| 2310 | 706 int c = Py_CHARMASK(*s++); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
707 if (islower(c)) { |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
708 changed = 1; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
709 *s_new = toupper(c); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
710 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
711 else if (isupper(c)) { |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
712 changed = 1; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
713 *s_new = tolower(c); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
714 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
715 else |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
716 *s_new = c; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
717 s_new++; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
718 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
719 if (!changed) { |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
720 Py_DECREF(newstr); |
| 4617 | 721 Py_INCREF(args); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
722 return args; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
723 } |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
724 return newstr; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
725 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
726 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
727 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
728 PyDoc_STRVAR(atoi__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
729 "atoi(s [,base]) -> int\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
730 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
731 "Return the integer represented by the string s in the given\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
732 "base, which defaults to 10. The string s must consist of one\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
733 "or more digits, possibly preceded by a sign. If base is 0, it\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
734 "is chosen from the leading characters of s, 0 for octal, 0x or\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
735 "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
736 "accepted."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
737 |
| 4617 | 738 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
739 strop_atoi(PyObject *self, PyObject *args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
740 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
741 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
742 int base = 10; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
743 long x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
744 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
745 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
746 WARN; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
747 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base)) |
| 4889 | 748 return NULL; |
| 749 | |
| 750 if ((base != 0 && base < 2) || base > 36) { | |
| 751 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); | |
| 752 return NULL; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
753 } |
| 4889 | 754 |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
755 while (*s && isspace(Py_CHARMASK(*s))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
756 s++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
757 errno = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
758 if (base == 0 && s[0] == '0') |
| 4617 | 759 x = (long) PyOS_strtoul(s, &end, base); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
760 else |
| 4617 | 761 x = PyOS_strtol(s, &end, base); |
|
36090
8cb686dd6e64
Fix SF bug #1072182, problems with signed characters.
Neal Norwitz <nnorwitz@gmail.com>
parents:
32305
diff
changeset
|
762 if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
|
8809
c5d72bcda932
Better error messages when raising ValueError for int literals. (The
Guido van Rossum <guido@python.org>
parents:
8757
diff
changeset
|
763 goto bad; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
764 while (*end && isspace(Py_CHARMASK(*end))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
765 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
766 if (*end != '\0') { |
|
8809
c5d72bcda932
Better error messages when raising ValueError for int literals. (The
Guido van Rossum <guido@python.org>
parents:
8757
diff
changeset
|
767 bad: |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
768 PyOS_snprintf(buffer, sizeof(buffer), |
|
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
769 "invalid literal for atoi(): %.200s", s); |
| 4617 | 770 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
771 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
772 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
773 else if (errno != 0) { |
|
21094
c2150313d9d1
More sprintf -> PyOS_snprintf.
Tim Peters <tim.peters@gmail.com>
parents:
21073
diff
changeset
|
774 PyOS_snprintf(buffer, sizeof(buffer), |
|
c2150313d9d1
More sprintf -> PyOS_snprintf.
Tim Peters <tim.peters@gmail.com>
parents:
21073
diff
changeset
|
775 "atoi() literal too large: %.200s", s); |
| 4617 | 776 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
777 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
778 } |
| 4617 | 779 return PyInt_FromLong(x); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
780 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
781 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
782 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
783 PyDoc_STRVAR(atol__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
784 "atol(s [,base]) -> long\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
785 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
786 "Return the long integer represented by the string s in the\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
787 "given base, which defaults to 10. The string s must consist\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
788 "of one or more digits, possibly preceded by a sign. If base\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
789 "is 0, it is chosen from the leading characters of s, 0 for\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
790 "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
791 "0x or 0X is accepted. A trailing L or l is not accepted,\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
792 "unless base is 0."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
793 |
| 4617 | 794 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
795 strop_atol(PyObject *self, PyObject *args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
796 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
797 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
798 int base = 10; |
| 4617 | 799 PyObject *x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
800 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
801 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
802 WARN; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
803 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base)) |
| 4889 | 804 return NULL; |
| 805 | |
| 806 if ((base != 0 && base < 2) || base > 36) { | |
| 807 PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); | |
| 808 return NULL; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
809 } |
| 4889 | 810 |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
811 while (*s && isspace(Py_CHARMASK(*s))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
812 s++; |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
813 if (s[0] == '\0') { |
| 4617 | 814 PyErr_SetString(PyExc_ValueError, "empty string for atol()"); |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
815 return NULL; |
|
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
816 } |
| 4617 | 817 x = PyLong_FromString(s, &end, base); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
818 if (x == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
819 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
820 if (base == 0 && (*end == 'l' || *end == 'L')) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
821 end++; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
822 while (*end && isspace(Py_CHARMASK(*end))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
823 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
824 if (*end != '\0') { |
|
21094
c2150313d9d1
More sprintf -> PyOS_snprintf.
Tim Peters <tim.peters@gmail.com>
parents:
21073
diff
changeset
|
825 PyOS_snprintf(buffer, sizeof(buffer), |
|
c2150313d9d1
More sprintf -> PyOS_snprintf.
Tim Peters <tim.peters@gmail.com>
parents:
21073
diff
changeset
|
826 "invalid literal for atol(): %.200s", s); |
| 4617 | 827 PyErr_SetString(PyExc_ValueError, buffer); |
| 828 Py_DECREF(x); | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
829 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
830 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
831 return x; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
832 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
833 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
834 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
835 PyDoc_STRVAR(atof__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
836 "atof(s) -> float\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
837 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
838 "Return the floating point number represented by the string s."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
839 |
| 4617 | 840 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
841 strop_atof(PyObject *self, PyObject *args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
842 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
843 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
844 double x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
845 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
846 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
847 WARN; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
848 if (!PyArg_ParseTuple(args, "s:atof", &s)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
849 return NULL; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
850 while (*s && isspace(Py_CHARMASK(*s))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
851 s++; |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
852 if (s[0] == '\0') { |
| 4617 | 853 PyErr_SetString(PyExc_ValueError, "empty string for atof()"); |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
854 return NULL; |
|
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
855 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
856 errno = 0; |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4901
diff
changeset
|
857 PyFPE_START_PROTECT("strop_atof", return 0) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
24471
diff
changeset
|
858 x = PyOS_ascii_strtod(s, &end); |
|
5188
3ec3d8807f9e
Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
859 PyFPE_END_PROTECT(x) |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
860 while (*end && isspace(Py_CHARMASK(*end))) |
|
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
861 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
862 if (*end != '\0') { |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
863 PyOS_snprintf(buffer, sizeof(buffer), |
|
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
864 "invalid literal for atof(): %.200s", s); |
| 4617 | 865 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
866 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
867 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
868 else if (errno != 0) { |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
869 PyOS_snprintf(buffer, sizeof(buffer), |
|
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
17834
diff
changeset
|
870 "atof() literal too large: %.200s", s); |
| 4617 | 871 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
872 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
873 } |
| 4617 | 874 return PyFloat_FromDouble(x); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
875 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
876 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
877 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
878 PyDoc_STRVAR(maketrans__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
879 "maketrans(frm, to) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
880 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
881 "Return a translation table (a string of 256 bytes long)\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
882 "suitable for use in string.translate. The strings frm and to\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
883 "must be of the same length."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
884 |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
885 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
886 strop_maketrans(PyObject *self, PyObject *args) |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
887 { |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
888 unsigned char *c, *from=NULL, *to=NULL; |
| 36557 | 889 Py_ssize_t i, fromlen=0, tolen=0; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
890 PyObject *result; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
891 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
892 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen)) |
| 4889 | 893 return NULL; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
894 |
| 4889 | 895 if (fromlen != tolen) { |
| 4617 | 896 PyErr_SetString(PyExc_ValueError, |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
897 "maketrans arguments must have same length"); |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
898 return NULL; |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
899 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
900 |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
901 result = PyString_FromStringAndSize((char *)NULL, 256); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
902 if (result == NULL) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
903 return NULL; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
904 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); |
| 4889 | 905 for (i = 0; i < 256; i++) |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
906 c[i]=(unsigned char)i; |
| 4889 | 907 for (i = 0; i < fromlen; i++) |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
908 c[from[i]]=to[i]; |
| 4889 | 909 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
910 return result; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
911 } |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
912 |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
913 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
914 PyDoc_STRVAR(translate__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
915 "translate(s,table [,deletechars]) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
916 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
917 "Return a copy of the string s, where all characters occurring\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
918 "in the optional argument deletechars are removed, and the\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
919 "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:
23295
diff
changeset
|
920 "translation table, which must be a string of length 256."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
921 |
| 4617 | 922 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
923 strop_translate(PyObject *self, PyObject *args) |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
924 { |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
925 register char *input, *table, *output; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
926 Py_ssize_t i; |
|
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
927 int c, changed = 0; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
928 PyObject *input_obj; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
929 char *table1, *output_start, *del_table=NULL; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
930 Py_ssize_t inlen, tablen, dellen = 0; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
931 PyObject *result; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
932 int trans_table[256]; |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
933 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
934 WARN; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
935 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj, |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
936 &table1, &tablen, &del_table, &dellen)) |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
937 return NULL; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
938 if (tablen != 256) { |
| 4617 | 939 PyErr_SetString(PyExc_ValueError, |
| 4889 | 940 "translation table must be 256 characters long"); |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
941 return NULL; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
942 } |
| 4889 | 943 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
944 table = table1; |
|
37086
3b7b295cca90
Use macro versions instead of function versions when we already know the type.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36557
diff
changeset
|
945 inlen = PyString_GET_SIZE(input_obj); |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
946 result = PyString_FromStringAndSize((char *)NULL, inlen); |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
947 if (result == NULL) |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
948 return NULL; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
949 output_start = output = PyString_AsString(result); |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
950 input = PyString_AsString(input_obj); |
| 4889 | 951 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
952 if (dellen == 0) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
953 /* If no deletions are required, use faster code */ |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
954 for (i = inlen; --i >= 0; ) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
955 c = Py_CHARMASK(*input++); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
956 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
957 changed = 1; |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
958 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
959 if (changed) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
960 return result; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
961 Py_DECREF(result); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
962 Py_INCREF(input_obj); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
963 return input_obj; |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
964 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
965 |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
966 for (i = 0; i < 256; i++) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
967 trans_table[i] = Py_CHARMASK(table[i]); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
968 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
969 for (i = 0; i < dellen; i++) |
| 5380 | 970 trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
971 |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
972 for (i = inlen; --i >= 0; ) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
973 c = Py_CHARMASK(*input++); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
974 if (trans_table[c] != -1) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
975 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
976 continue; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
977 changed = 1; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
978 } |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
979 if (!changed) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
980 Py_DECREF(result); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
981 Py_INCREF(input_obj); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
982 return input_obj; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
983 } |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
984 /* 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:
22771
diff
changeset
|
985 if (inlen > 0) |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
22771
diff
changeset
|
986 _PyString_Resize(&result, output - output_start); |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
987 return result; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
988 } |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
989 |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
990 |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
991 /* What follows is used for implementing replace(). Perry Stoll. */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
992 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
993 /* |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
994 mymemfind |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
995 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
996 strstr replacement for arbitrary blocks of memory. |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
997 |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11882
diff
changeset
|
998 Locates the first occurrence in the memory pointed to by MEM of the |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
999 contents of memory pointed to by PAT. Returns the index into MEM if |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1000 found, or -1 if not found. If len of PAT is greater than length of |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1001 MEM, the function returns -1. |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1002 */ |
| 36557 | 1003 static Py_ssize_t |
| 1004 mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) | |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1005 { |
| 36557 | 1006 register Py_ssize_t ii; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1007 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1008 /* pattern can not occur in the last pat_len-1 chars */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1009 len -= pat_len; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1010 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1011 for (ii = 0; ii <= len; ii++) { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1012 if (mem[ii] == pat[0] && |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1013 (pat_len == 1 || |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1014 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1015 return ii; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1016 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1017 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1018 return -1; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1019 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1020 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1021 /* |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1022 mymemcnt |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1023 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1024 Return the number of distinct times PAT is found in MEM. |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1025 meaning mem=1111 and pat==11 returns 2. |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1026 mem=11111 and pat==11 also return 2. |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1027 */ |
| 36557 | 1028 static Py_ssize_t |
| 1029 mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) | |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1030 { |
| 36557 | 1031 register Py_ssize_t offset = 0; |
| 1032 Py_ssize_t nfound = 0; | |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1033 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1034 while (len >= 0) { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1035 offset = mymemfind(mem, len, pat, pat_len); |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1036 if (offset == -1) |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1037 break; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1038 mem += offset + pat_len; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1039 len -= offset + pat_len; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1040 nfound++; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1041 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1042 return nfound; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1043 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1044 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1045 /* |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1046 mymemreplace |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1047 |
|
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
|
1048 Return a string in which all occurrences of PAT in memory STR are |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1049 replaced with SUB. |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1050 |
|
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
|
1051 If length of PAT is less than length of STR or there are no occurrences |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1052 of PAT in STR, then the original string is returned. Otherwise, a new |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1053 string is allocated here and returned. |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1054 |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1055 on return, out_len is: |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1056 the length of output string, or |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1057 -1 if the input string is returned, or |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1058 unchanged if an error occurs (no memory). |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1059 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1060 return value is: |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1061 the new string allocated locally, or |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1062 NULL if an error occurred. |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1063 */ |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1064 static char * |
| 36557 | 1065 mymemreplace(const char *str, Py_ssize_t len, /* input string */ |
| 1066 const char *pat, Py_ssize_t pat_len, /* pattern string to find */ | |
| 1067 const char *sub, Py_ssize_t sub_len, /* substitution string */ | |
| 1068 Py_ssize_t count, /* number of replacements */ | |
| 1069 Py_ssize_t *out_len) | |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1070 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1071 char *out_s; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1072 char *new_s; |
| 36557 | 1073 Py_ssize_t nfound, offset, new_len; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1074 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1075 if (len == 0 || pat_len > len) |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1076 goto return_same; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1077 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1078 /* find length of output string */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1079 nfound = mymemcnt(str, len, pat, pat_len); |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1080 if (count < 0) |
|
37671
3f9739de6603
Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37666
diff
changeset
|
1081 count = PY_SSIZE_T_MAX; |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1082 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
|
1083 nfound = count; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1084 if (nfound == 0) |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1085 goto return_same; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1086 |
|
17749
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1087 new_len = len + nfound*(sub_len - pat_len); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1088 if (new_len == 0) { |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17749
diff
changeset
|
1089 /* 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:
17749
diff
changeset
|
1090 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
|
1091 if (out_s == NULL) |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17749
diff
changeset
|
1092 return NULL; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17749
diff
changeset
|
1093 out_s[0] = '\0'; |
|
17749
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1094 } |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1095 else { |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1096 assert(new_len > 0); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1097 new_s = (char *)PyMem_MALLOC(new_len); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1098 if (new_s == NULL) |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1099 return NULL; |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1100 out_s = new_s; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1101 |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1102 for (; count > 0 && len > 0; --count) { |
|
17749
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1103 /* find index of next instance of pattern */ |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1104 offset = mymemfind(str, len, pat, pat_len); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1105 if (offset == -1) |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1106 break; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1107 |
|
17749
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1108 /* copy non matching part of input string */ |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1109 memcpy(new_s, str, offset); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1110 str += offset + pat_len; |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1111 len -= offset + pat_len; |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1112 |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1113 /* copy substitute into the output string */ |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1114 new_s += offset; |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1115 memcpy(new_s, sub, sub_len); |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1116 new_s += sub_len; |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1117 } |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1118 /* copy any remaining values into output string */ |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1119 if (len > 0) |
|
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1120 memcpy(new_s, str, len); |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1121 } |
|
17749
f465cae301ee
SF bug #422088: [OSF1 alpha] string.replace().
Tim Peters <tim.peters@gmail.com>
parents:
17748
diff
changeset
|
1122 *out_len = new_len; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1123 return out_s; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1124 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1125 return_same: |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1126 *out_len = -1; |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17749
diff
changeset
|
1127 return (char *)str; /* cast away const */ |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1128 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1129 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1130 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
1131 PyDoc_STRVAR(replace__doc__, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1132 "replace (str, old, new[, maxsplit]) -> string\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1133 "\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1134 "Return a copy of string str with all occurrences of substring\n" |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1135 "old replaced by new. If the optional argument maxsplit is\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23295
diff
changeset
|
1136 "given, only the first maxsplit occurrences are replaced."); |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1137 |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1138 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1139 strop_replace(PyObject *self, PyObject *args) |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1140 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1141 char *str, *pat,*sub,*new_s; |
| 36557 | 1142 Py_ssize_t len,pat_len,sub_len,out_len; |
| 1143 Py_ssize_t count = -1; | |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
1144 PyObject *newstr; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1145 |
|
17834
ed1e8b6e26ec
Add warnings to the strop module, for to those functions that really
Guido van Rossum <guido@python.org>
parents:
17753
diff
changeset
|
1146 WARN; |
| 36557 | 1147 if (!PyArg_ParseTuple(args, "t#t#t#|n:replace", |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1148 &str, &len, &pat, &pat_len, &sub, &sub_len, |
|
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1149 &count)) |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1150 return NULL; |
|
8333
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1151 if (pat_len <= 0) { |
|
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1152 PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
|
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1153 return NULL; |
|
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1154 } |
|
17753
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1155 /* CAUTION: strop treats a replace count of 0 as infinity, unlke |
|
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1156 * current (2.1) string.py and string methods. Preserve this for |
|
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1157 * ... well, hard to say for what <wink>. |
|
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1158 */ |
|
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1159 if (count == 0) |
|
c47cc9a9864c
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
Tim Peters <tim.peters@gmail.com>
parents:
17752
diff
changeset
|
1160 count = -1; |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1161 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len); |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1162 if (new_s == NULL) { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1163 PyErr_NoMemory(); |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1164 return NULL; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1165 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1166 if (out_len == -1) { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1167 /* we're returning another reference to the input string */ |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
1168 newstr = PyTuple_GetItem(args, 0); |
|
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
1169 Py_XINCREF(newstr); |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1170 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1171 else { |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
1172 newstr = PyString_FromStringAndSize(new_s, out_len); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
1173 PyMem_FREE(new_s); |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1174 } |
|
37666
5a9fcd676b3f
C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37086
diff
changeset
|
1175 return newstr; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1176 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1177 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1178 |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1179 /* List of functions defined in the module */ |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1180 |
| 4889 | 1181 static PyMethodDef |
| 1182 strop_methods[] = { | |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1183 {"atof", strop_atof, METH_VARARGS, atof__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1184 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1185 {"atol", strop_atol, METH_VARARGS, atol__doc__}, |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1186 {"capitalize", strop_capitalize, METH_O, capitalize__doc__}, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1187 {"count", strop_count, METH_VARARGS, count__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1188 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1189 {"find", strop_find, METH_VARARGS, find__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1190 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1191 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__}, |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1192 {"lstrip", strop_lstrip, METH_O, lstrip__doc__}, |
|
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1193 {"lower", strop_lower, METH_O, lower__doc__}, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1194 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1195 {"replace", strop_replace, METH_VARARGS, replace__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1196 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__}, |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1197 {"rstrip", strop_rstrip, METH_O, rstrip__doc__}, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1198 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__}, |
|
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1199 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__}, |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1200 {"strip", strop_strip, METH_O, strip__doc__}, |
|
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1201 {"swapcase", strop_swapcase, METH_O, swapcase__doc__}, |
|
17748
261196bd16d7
Mechanical changes for easier edits.
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
1202 {"translate", strop_translate, METH_VARARGS, translate__doc__}, |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
22729
diff
changeset
|
1203 {"upper", strop_upper, METH_O, upper__doc__}, |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1204 {NULL, NULL} /* sentinel */ |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1205 }; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1206 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1207 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
1208 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1209 initstrop(void) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1210 { |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1211 PyObject *m, *s; |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1212 char buf[256]; |
|
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1213 int c, n; |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1214 m = Py_InitModule4("strop", strop_methods, strop_module__doc__, |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1215 (PyObject*)NULL, PYTHON_API_VERSION); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36090
diff
changeset
|
1216 if (m == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36090
diff
changeset
|
1217 return; |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1218 |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1219 /* Create 'whitespace' object */ |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1220 n = 0; |
| 2310 | 1221 for (c = 0; c < 256; c++) { |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1222 if (isspace(c)) |
|
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1223 buf[n++] = c; |
|
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1224 } |
| 4617 | 1225 s = PyString_FromStringAndSize(buf, n); |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1226 if (s) |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1227 PyModule_AddObject(m, "whitespace", s); |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1228 |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1229 /* Create 'lowercase' object */ |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1230 n = 0; |
| 2310 | 1231 for (c = 0; c < 256; c++) { |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1232 if (islower(c)) |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1233 buf[n++] = c; |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1234 } |
| 4617 | 1235 s = PyString_FromStringAndSize(buf, n); |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1236 if (s) |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1237 PyModule_AddObject(m, "lowercase", s); |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1238 |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1239 /* Create 'uppercase' object */ |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1240 n = 0; |
| 2310 | 1241 for (c = 0; c < 256; c++) { |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1242 if (isupper(c)) |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1243 buf[n++] = c; |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1244 } |
| 4617 | 1245 s = PyString_FromStringAndSize(buf, n); |
|
22729
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1246 if (s) |
|
cdff3c7c1a76
Use the PyModule_Add*() APIs instead of manipulating the module dict
Fred Drake <fdrake@acm.org>
parents:
22421
diff
changeset
|
1247 PyModule_AddObject(m, "uppercase", s); |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1248 } |
