Mercurial > cpython-lru_cache-weakref
annotate Modules/stropmodule.c @ 15842:f6cd36498a50 legacy-trunk
Document extensions to .pth files.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Sat, 13 Jan 2001 09:54:41 +0000 |
| parents | 25ddd3dc7a7a |
| children | 9dfd22e8e9b7 261196bd16d7 |
| 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 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 /* strop module */ |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
4 static char strop_module__doc__[] = |
|
6967
744e2b68f3b6
Minor editing corrections.
Guido van Rossum <guido@python.org>
parents:
6966
diff
changeset
|
5 "Common string manipulations, optimized for speed.\n\ |
|
7000
f2af3c645710
Oops, the last checkin left a blank line in a string literal.
Guido van Rossum <guido@python.org>
parents:
6967
diff
changeset
|
6 \n\ |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
7 Always use \"import string\" rather than referencing\n\ |
|
6967
744e2b68f3b6
Minor editing corrections.
Guido van Rossum <guido@python.org>
parents:
6966
diff
changeset
|
8 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
|
9 |
| 4617 | 10 #include "Python.h" |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
12 #include <ctype.h> |
|
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 |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
16 /* The lstrip(), rstrip() and strip() functions are implemented |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
17 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
|
18 type of strip should occur. */ |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
19 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
20 #define LEFTSTRIP 0 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
21 #define RIGHTSTRIP 1 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
22 #define BOTHSTRIP 2 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
23 |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
| 4617 | 25 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
26 split_whitespace(char *s, int len, int maxsplit) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 { |
| 4889 | 28 int i = 0, j, err; |
| 29 int countsplit = 0; | |
| 30 PyObject* item; | |
| 31 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
|
32 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 if (list == NULL) |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 while (i < len) { |
| 2310 | 37 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
|
38 i = i+1; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 j = i; |
|
2335
e11e6b358531
fix stupid bug in strip and split
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
41 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
|
42 i = i+1; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 if (j < i) { |
| 4617 | 45 item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
| 4889 | 46 if (item == NULL) |
| 47 goto finally; | |
| 48 | |
| 4617 | 49 err = PyList_Append(list, item); |
| 50 Py_DECREF(item); | |
| 4889 | 51 if (err < 0) |
| 52 goto finally; | |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
53 |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
54 countsplit++; |
|
6696
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
55 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
|
56 i = i+1; |
|
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
57 } |
|
e92154022b48
split_whitespace(): Make sure delimiter is stripped from the beginning
Barry Warsaw <barry@python.org>
parents:
6684
diff
changeset
|
58 if (maxsplit && (countsplit >= maxsplit) && i < len) { |
| 4617 | 59 item = PyString_FromStringAndSize( |
| 60 s+i, (int)(len - i)); | |
| 4889 | 61 if (item == NULL) |
| 62 goto finally; | |
| 63 | |
| 4617 | 64 err = PyList_Append(list, item); |
| 65 Py_DECREF(item); | |
| 4889 | 66 if (err < 0) |
| 67 goto finally; | |
| 68 | |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
69 i = len; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
70 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 return list; |
| 4889 | 74 finally: |
| 75 Py_DECREF(list); | |
| 76 return NULL; | |
|
1012
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 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
80 static char splitfields__doc__[] = |
|
11439
316be08c9ecd
split() docstring: Made signature and description for the first
Fred Drake <fdrake@acm.org>
parents:
10268
diff
changeset
|
81 "split(s [,sep [,maxsplit]]) -> list of strings\n\ |
|
316be08c9ecd
split() docstring: Made signature and description for the first
Fred Drake <fdrake@acm.org>
parents:
10268
diff
changeset
|
82 splitfields(s [,sep [,maxsplit]]) -> list of strings\n\ |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
83 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
84 Return a list of the words in the string s, using sep as the\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
85 delimiter string. If maxsplit is nonzero, splits into at most\n\ |
|
11439
316be08c9ecd
split() docstring: Made signature and description for the first
Fred Drake <fdrake@acm.org>
parents:
10268
diff
changeset
|
86 maxsplit words. If sep is not specified, any whitespace string\n\ |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
87 is a separator. Maxsplit defaults to 0.\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
88 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
89 (split and splitfields are synonymous)"; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
90 |
| 4617 | 91 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
92 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
|
93 { |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
94 int len, n, i, j, err; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
95 int splitcount, maxsplit; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 char *s, *sub; |
| 4617 | 97 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
|
98 |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
99 sub = NULL; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
100 n = 0; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
101 splitcount = 0; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
102 maxsplit = 0; |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
103 if (!PyArg_ParseTuple(args, "t#|z#i: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
|
104 return NULL; |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
105 if (sub == NULL) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
106 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
|
107 if (n == 0) { |
| 4617 | 108 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
|
109 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
| 4617 | 112 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
|
113 if (list == NULL) |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 return NULL; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 i = j = 0; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 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
|
118 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) { |
| 4617 | 119 item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
120 if (item == NULL) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
121 goto fail; |
| 4617 | 122 err = PyList_Append(list, item); |
| 123 Py_DECREF(item); | |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
124 if (err < 0) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
125 goto fail; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 i = j = i + n; |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
127 splitcount++; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
128 if (maxsplit && (splitcount >= maxsplit)) |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
129 break; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 else |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 i++; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 } |
| 4617 | 134 item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
135 if (item == NULL) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
136 goto fail; |
| 4617 | 137 err = PyList_Append(list, item); |
| 138 Py_DECREF(item); | |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
139 if (err < 0) |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
140 goto fail; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 return list; |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
143 |
|
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
144 fail: |
| 4617 | 145 Py_DECREF(list); |
|
1394
98f51f61cb4b
Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents:
1367
diff
changeset
|
146 return NULL; |
|
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 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
150 static char joinfields__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
151 "join(list [,sep]) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
152 joinfields(list [,sep]) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
153 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
154 Return a string composed of the words in list, with\n\ |
|
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
|
155 intervening occurrences of sep. Sep defaults to a single\n\ |
|
6967
744e2b68f3b6
Minor editing corrections.
Guido van Rossum <guido@python.org>
parents:
6966
diff
changeset
|
156 space.\n\ |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
157 \n\ |
|
6967
744e2b68f3b6
Minor editing corrections.
Guido van Rossum <guido@python.org>
parents:
6966
diff
changeset
|
158 (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
|
159 |
| 4617 | 160 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
161 strop_joinfields(PyObject *self, PyObject *args) |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
162 { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
163 PyObject *seq; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
164 char *sep = NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
165 int seqlen, seplen = 0; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
166 int i, reslen = 0, slen = 0, sz = 100; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
167 PyObject *res = NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
168 char* p = NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
169 intargfunc getitemfunc; |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
170 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
171 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
|
172 return NULL; |
|
2707
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
173 if (sep == NULL) { |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
174 sep = " "; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
175 seplen = 1; |
|
1306e761a623
unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents:
2335
diff
changeset
|
176 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
177 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13494
diff
changeset
|
178 seqlen = PySequence_Size(seq); |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
179 if (seqlen < 0 && PyErr_Occurred()) |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
180 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
181 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
182 if (seqlen == 1) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
183 /* 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
|
184 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
|
185 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
|
186 PyErr_SetString(PyExc_TypeError, |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
187 "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
|
188 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
|
189 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
|
190 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
191 return item; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
192 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
193 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
194 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
|
195 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
196 p = PyString_AsString(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
197 |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
198 /* 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
|
199 * (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
|
200 * interface. |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
201 */ |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
202 if (PyList_Check(seq)) { |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
203 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
|
204 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
|
205 if (!PyString_Check(item)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
206 PyErr_SetString(PyExc_TypeError, |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
207 "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
|
208 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
209 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
210 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
211 slen = PyString_GET_SIZE(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
212 while (reslen + slen + seplen >= sz) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
213 if (_PyString_Resize(&res, sz * 2)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
214 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
215 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
216 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
217 sz *= 2; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
218 p = PyString_AsString(res) + reslen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
219 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
220 if (i > 0) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
221 memcpy(p, sep, seplen); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
222 p += seplen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
223 reslen += seplen; |
|
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 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
|
226 p += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
227 reslen += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
228 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
229 if (_PyString_Resize(&res, reslen)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
230 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
231 res = NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
232 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
233 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
|
234 } |
|
8405
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
235 |
|
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
236 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
|
237 (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
|
238 { |
| 4617 | 239 PyErr_SetString(PyExc_TypeError, |
| 4889 | 240 "first argument must be a sequence"); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
241 return NULL; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
242 } |
|
8405
3c40af2d1de9
Make sure that no use of a function pointer gotten from a
Guido van Rossum <guido@python.org>
parents:
8333
diff
changeset
|
243 /* This is now type safe */ |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
244 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
|
245 PyObject *item = getitemfunc(seq, i); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
246 if (!item || !PyString_Check(item)) { |
| 4617 | 247 PyErr_SetString(PyExc_TypeError, |
| 4889 | 248 "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
|
249 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
250 Py_XDECREF(item); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
251 return NULL; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
252 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
253 slen = PyString_GET_SIZE(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
254 while (reslen + slen + seplen >= sz) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
255 if (_PyString_Resize(&res, sz * 2)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
256 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
257 Py_DECREF(item); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
258 return NULL; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
259 } |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
260 sz *= 2; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
261 p = PyString_AsString(res) + reslen; |
| 4889 | 262 } |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
263 if (i > 0) { |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
264 memcpy(p, sep, seplen); |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
265 p += seplen; |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
266 reslen += seplen; |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
267 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
268 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
|
269 p += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
270 reslen += slen; |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
271 Py_DECREF(item); |
|
1094
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
272 } |
|
4901
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
273 if (_PyString_Resize(&res, reslen)) { |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
274 Py_DECREF(res); |
|
770104c8f84a
New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents:
4895
diff
changeset
|
275 res = NULL; |
|
1094
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 return res; |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
278 } |
|
d2af6bdcb6d0
* config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents:
1048
diff
changeset
|
279 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
280 |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
281 static char find__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
282 "find(s, sub [,start [,end]]) -> in\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
283 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
284 Return the lowest index in s where substring sub is found,\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
285 such that sub is contained within s[start,end]. Optional\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
286 arguments start and end are interpreted as in slice notation.\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
287 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
288 Return -1 on failure."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
289 |
| 4617 | 290 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
291 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
|
292 { |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 char *s, *sub; |
|
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
|
294 int len, n, i = 0, last = INT_MAX; |
| 4889 | 295 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
296 if (!PyArg_ParseTuple(args, "t#t#|ii:find", &s, &len, &sub, &n, &i, &last)) |
| 4889 | 297 return NULL; |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 |
|
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
|
299 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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 last = 0; |
| 4889 | 305 if (i < 0) |
| 306 i += len; | |
| 307 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
|
308 i = 0; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 |
|
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
|
310 if (n == 0 && i <= last) |
| 4617 | 311 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
|
312 |
|
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
|
313 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
|
314 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
|
315 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
|
316 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0)) |
| 4617 | 317 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
|
318 |
| 4617 | 319 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
|
320 } |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
321 |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
322 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
323 static char rfind__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
324 "rfind(s, sub [,start [,end]]) -> int\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
325 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
326 Return the highest index in s where substring sub is found,\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
327 such that sub is contained within s[start,end]. Optional\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
328 arguments start and end are interpreted as in slice notation.\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
329 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
330 Return -1 on failure."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
331 |
| 4617 | 332 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
333 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
|
334 { |
|
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
335 char *s, *sub; |
| 4889 | 336 int len, n, j; |
|
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
|
337 int i = 0, last = INT_MAX; |
|
1367
ee4a2f893254
Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
338 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
339 if (!PyArg_ParseTuple(args, "t#t#|ii: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; |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
369 int len, i, j; |
|
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 |
|
9307
928ea5a409e5
Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents:
9289
diff
changeset
|
372 if (!PyArg_Parse(args, "t#", &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 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
399 static char strip__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
400 "strip(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
401 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
402 Return a copy of the string s with leading and trailing\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
403 whitespace removed."; |
|
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 { |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
408 return do_strip(args, BOTHSTRIP); |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
409 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
411 |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
412 static char lstrip__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
413 "lstrip(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
414 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
415 Return a copy of the string s with leading whitespace removed."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
416 |
| 4617 | 417 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
418 strop_lstrip(PyObject *self, PyObject *args) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
419 { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
420 return do_strip(args, LEFTSTRIP); |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
421 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
423 |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
424 static char rstrip__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
425 "rstrip(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
426 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
427 Return a copy of the string s with trailing whitespace removed."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
428 |
| 4617 | 429 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
430 strop_rstrip(PyObject *self, PyObject *args) |
|
3978
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
431 { |
|
b93c4cfc2cfe
Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents:
3799
diff
changeset
|
432 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
|
433 } |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
436 static char lower__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
437 "lower(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
438 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
439 Return a copy of the string s converted to lowercase."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
440 |
| 4617 | 441 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
442 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
|
443 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
444 char *s, *s_new; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
445 int i, n; |
| 4617 | 446 PyObject *new; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
447 int changed; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
448 |
|
9307
928ea5a409e5
Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents:
9289
diff
changeset
|
449 if (!PyArg_Parse(args, "t#", &s, &n)) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
450 return NULL; |
| 4617 | 451 new = PyString_FromStringAndSize(NULL, n); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
452 if (new == NULL) |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
453 return NULL; |
| 4617 | 454 s_new = PyString_AsString(new); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
455 changed = 0; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
456 for (i = 0; i < n; i++) { |
| 2310 | 457 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
|
458 if (isupper(c)) { |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
459 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
|
460 *s_new = tolower(c); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
461 } else |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
462 *s_new = c; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
463 s_new++; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
464 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
465 if (!changed) { |
| 4617 | 466 Py_DECREF(new); |
| 467 Py_INCREF(args); | |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
468 return args; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
469 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
470 return new; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
471 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
472 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
473 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
474 static char upper__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
475 "upper(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
476 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
477 Return a copy of the string s converted to uppercase."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
478 |
| 4617 | 479 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
480 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
|
481 { |
|
4894
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
482 char *s, *s_new; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
483 int i, n; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
484 PyObject *new; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
485 int changed; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
486 |
|
9307
928ea5a409e5
Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents:
9289
diff
changeset
|
487 if (!PyArg_Parse(args, "t#", &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
|
488 return NULL; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
489 new = PyString_FromStringAndSize(NULL, n); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
490 if (new == NULL) |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
491 return NULL; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
492 s_new = PyString_AsString(new); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
493 changed = 0; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
494 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
|
495 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
|
496 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
|
497 changed = 1; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
498 *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
|
499 } else |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
500 *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
|
501 s_new++; |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
502 } |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
503 if (!changed) { |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
504 Py_DECREF(new); |
|
f31f47ff1fdd
strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents:
4889
diff
changeset
|
505 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
|
506 return args; |
|
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 return new; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
509 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
510 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
511 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
512 static char capitalize__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
513 "capitalize(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
514 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
515 Return a copy of the string s with only its first character\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
516 capitalized."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
517 |
| 4617 | 518 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
519 strop_capitalize(PyObject *self, PyObject *args) |
| 3676 | 520 { |
| 521 char *s, *s_new; | |
| 522 int i, n; | |
| 4617 | 523 PyObject *new; |
| 3676 | 524 int changed; |
| 525 | |
|
9307
928ea5a409e5
Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents:
9289
diff
changeset
|
526 if (!PyArg_Parse(args, "t#", &s, &n)) |
| 3676 | 527 return NULL; |
| 4617 | 528 new = PyString_FromStringAndSize(NULL, n); |
| 3676 | 529 if (new == NULL) |
| 530 return NULL; | |
| 4617 | 531 s_new = PyString_AsString(new); |
| 3676 | 532 changed = 0; |
|
3679
b8980735d03b
Test for zero-length argument in capitalize().
Guido van Rossum <guido@python.org>
parents:
3676
diff
changeset
|
533 if (0 < n) { |
| 3676 | 534 int c = Py_CHARMASK(*s++); |
| 535 if (islower(c)) { | |
| 536 changed = 1; | |
| 537 *s_new = toupper(c); | |
| 538 } else | |
| 539 *s_new = c; | |
| 540 s_new++; | |
| 541 } | |
| 542 for (i = 1; i < n; i++) { | |
| 543 int c = Py_CHARMASK(*s++); | |
| 544 if (isupper(c)) { | |
| 545 changed = 1; | |
| 546 *s_new = tolower(c); | |
| 547 } else | |
| 548 *s_new = c; | |
| 549 s_new++; | |
| 550 } | |
| 551 if (!changed) { | |
| 4617 | 552 Py_DECREF(new); |
| 553 Py_INCREF(args); | |
| 3676 | 554 return args; |
| 555 } | |
| 556 return new; | |
| 557 } | |
| 558 | |
| 559 | |
|
9953
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
560 static char expandtabs__doc__[] = |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
561 "expandtabs(string, [tabsize]) -> string\n\ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
562 \n\ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
563 Expand tabs in a string, i.e. replace them by one or more spaces,\n\ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
564 depending on the current column and the given tab size (default 8).\n\ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
565 The column number is reset to zero after each newline occurring in the\n\ |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
566 string. This doesn't understand other non-printing characters."; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
567 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
568 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
569 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
|
570 { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
571 /* 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
|
572 char* e; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
573 char* p; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
574 char* q; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
575 int i, j; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
576 PyObject* out; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
577 char* string; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
578 int stringlen; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
579 int tabsize = 8; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
580 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
581 /* 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
|
582 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
|
583 return NULL; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
584 if (tabsize < 1) { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
585 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
|
586 "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
|
587 return NULL; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
588 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
589 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
590 /* 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
|
591 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
|
592 e = string + stringlen; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
593 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
|
594 if (*p == '\t') |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
595 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
|
596 else { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
597 j++; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
598 if (*p == '\n') { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
599 i += j; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
600 j = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
601 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
602 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
603 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
604 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
605 /* 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
|
606 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
|
607 if (out == NULL) |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
608 return NULL; |
|
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 i = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
611 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
|
612 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
613 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
|
614 if (*p == '\t') { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
615 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
|
616 i += j; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
617 while (j-- > 0) |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
618 *q++ = ' '; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
619 } else { |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
620 *q++ = *p; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
621 i++; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
622 if (*p == '\n') |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
623 i = 0; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
624 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
625 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
626 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
627 return out; |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
628 } |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
629 |
|
ca2745271111
A gift from Fredrik Lundh: fast C implementation of expandtabs().
Guido van Rossum <guido@python.org>
parents:
9626
diff
changeset
|
630 |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
631 static char count__doc__[] = |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
632 "count(s, sub[, start[, end]]) -> int\n\ |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
633 \n\ |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
634 Return the number of occurrences of substring sub in string\n\ |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
635 s[start:end]. Optional arguments start and end are\n\ |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
636 interpreted as in slice notation."; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
637 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
638 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
639 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
|
640 { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
641 char *s, *sub; |
| 9289 | 642 int len, n; |
|
9258
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
643 int i = 0, last = INT_MAX; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
644 int m, r; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
645 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
646 if (!PyArg_ParseTuple(args, "t#t#|ii: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
|
647 return NULL; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
648 if (last > len) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
649 last = len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
650 if (last < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
651 last += len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
652 if (last < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
653 last = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
654 if (i < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
655 i += len; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
656 if (i < 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
657 i = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
658 m = last + 1 - n; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
659 if (n == 0) |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
660 return PyInt_FromLong((long) (m-i)); |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
661 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
662 r = 0; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
663 while (i < m) { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
664 if (!memcmp(s+i, sub, n)) { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
665 r++; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
666 i += n; |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
667 } else { |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
668 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 } |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
671 return PyInt_FromLong((long) r); |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
672 } |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
673 |
|
2577c0cc7482
Andrew Dalke's implementation of string.count().
Guido van Rossum <guido@python.org>
parents:
8809
diff
changeset
|
674 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
675 static char swapcase__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
676 "swapcase(s) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
677 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
678 Return a copy of the string s with upper case characters\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
679 converted to lowercase and vice versa."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
680 |
| 4617 | 681 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
682 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
|
683 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
684 char *s, *s_new; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
685 int i, n; |
| 4617 | 686 PyObject *new; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
687 int changed; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
688 |
|
9307
928ea5a409e5
Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents:
9289
diff
changeset
|
689 if (!PyArg_Parse(args, "t#", &s, &n)) |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
690 return NULL; |
| 4617 | 691 new = PyString_FromStringAndSize(NULL, n); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
692 if (new == NULL) |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
693 return NULL; |
| 4617 | 694 s_new = PyString_AsString(new); |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
695 changed = 0; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
696 for (i = 0; i < n; i++) { |
| 2310 | 697 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
|
698 if (islower(c)) { |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
699 changed = 1; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
700 *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
|
701 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
702 else if (isupper(c)) { |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
703 changed = 1; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
704 *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
|
705 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
706 else |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
707 *s_new = c; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1337
diff
changeset
|
708 s_new++; |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
709 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
710 if (!changed) { |
| 4617 | 711 Py_DECREF(new); |
| 712 Py_INCREF(args); | |
|
1048
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
713 return args; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
714 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
715 return new; |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
716 } |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
717 |
|
c1ebfde341a3
Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents:
1012
diff
changeset
|
718 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
719 static char atoi__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
720 "atoi(s [,base]) -> int\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
721 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
722 Return the integer represented by the string s in the given\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
723 base, which defaults to 10. The string s must consist of one\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
724 or more digits, possibly preceded by a sign. If base is 0, it\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
725 is chosen from the leading characters of s, 0 for octal, 0x or\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
726 0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
727 accepted."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
728 |
| 4617 | 729 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
730 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
|
731 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
732 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
733 int base = 10; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
734 long x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
735 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
736 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
737 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base)) |
| 4889 | 738 return NULL; |
| 739 | |
| 740 if ((base != 0 && base < 2) || base > 36) { | |
| 741 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()"); | |
| 742 return NULL; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
743 } |
| 4889 | 744 |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
745 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
|
746 s++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
747 errno = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
748 if (base == 0 && s[0] == '0') |
| 4617 | 749 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
|
750 else |
| 4617 | 751 x = PyOS_strtol(s, &end, base); |
|
10268
35df4dc2d30d
In atoi(), don't use isxdigit() to test whether the last character
Guido van Rossum <guido@python.org>
parents:
9955
diff
changeset
|
752 if (end == s || !isalnum(end[-1])) |
|
8809
c5d72bcda932
Better error messages when raising ValueError for int literals. (The
Guido van Rossum <guido@python.org>
parents:
8757
diff
changeset
|
753 goto bad; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
754 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
|
755 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
756 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
|
757 bad: |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
758 sprintf(buffer, "invalid literal for atoi(): %.200s", s); |
| 4617 | 759 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
760 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
761 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
762 else if (errno != 0) { |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
763 sprintf(buffer, "atoi() literal too large: %.200s", s); |
| 4617 | 764 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
765 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
766 } |
| 4617 | 767 return PyInt_FromLong(x); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
768 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
769 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
770 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
771 static char atol__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
772 "atol(s [,base]) -> long\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
773 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
774 Return the long integer represented by the string s in the\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
775 given base, which defaults to 10. The string s must consist\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
776 of one or more digits, possibly preceded by a sign. If base\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
777 is 0, it is chosen from the leading characters of s, 0 for\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
778 octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
779 0x or 0X is accepted. A trailing L or l is not accepted,\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
780 unless base is 0."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
781 |
| 4617 | 782 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
783 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
|
784 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
785 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
786 int base = 10; |
| 4617 | 787 PyObject *x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
788 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
789 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
790 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base)) |
| 4889 | 791 return NULL; |
| 792 | |
| 793 if ((base != 0 && base < 2) || base > 36) { | |
| 794 PyErr_SetString(PyExc_ValueError, "invalid base for atol()"); | |
| 795 return NULL; | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
796 } |
| 4889 | 797 |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
798 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
|
799 s++; |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
800 if (s[0] == '\0') { |
| 4617 | 801 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
|
802 return NULL; |
|
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
803 } |
| 4617 | 804 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
|
805 if (x == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
806 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
807 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
|
808 end++; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
809 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
|
810 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
811 if (*end != '\0') { |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
812 sprintf(buffer, "invalid literal for atol(): %.200s", s); |
| 4617 | 813 PyErr_SetString(PyExc_ValueError, buffer); |
| 814 Py_DECREF(x); | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
815 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
816 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
817 return x; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
818 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
819 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
820 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
821 static char atof__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
822 "atof(s) -> float\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
823 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
824 Return the floating point number represented by the string s."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
825 |
| 4617 | 826 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
827 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
|
828 { |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
829 extern double strtod(const char *, char **); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
830 char *s, *end; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
831 double x; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
832 char buffer[256]; /* For errors */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
833 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
834 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
|
835 return NULL; |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
836 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
|
837 s++; |
|
4094
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
838 if (s[0] == '\0') { |
| 4617 | 839 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
|
840 return NULL; |
|
8438184e448e
Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4035
diff
changeset
|
841 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
842 errno = 0; |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4901
diff
changeset
|
843 PyFPE_START_PROTECT("strop_atof", return 0) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
844 x = 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
|
845 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
|
846 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
|
847 end++; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
848 if (*end != '\0') { |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
849 sprintf(buffer, "invalid literal for atof(): %.200s", s); |
| 4617 | 850 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
851 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
852 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
853 else if (errno != 0) { |
|
4296
75d75732a9e4
Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents:
4094
diff
changeset
|
854 sprintf(buffer, "atof() literal too large: %.200s", s); |
| 4617 | 855 PyErr_SetString(PyExc_ValueError, buffer); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
856 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
857 } |
| 4617 | 858 return PyFloat_FromDouble(x); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
859 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
860 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1394
diff
changeset
|
861 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
862 static char maketrans__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
863 "maketrans(frm, to) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
864 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
865 Return a translation table (a string of 256 bytes long)\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
866 suitable for use in string.translate. The strings frm and to\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
867 must be of the same length."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
868 |
|
3799
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
869 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
870 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
|
871 { |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
872 unsigned char *c, *from=NULL, *to=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
|
873 int i, fromlen=0, tolen=0; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
874 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
|
875 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
876 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen)) |
| 4889 | 877 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
|
878 |
| 4889 | 879 if (fromlen != tolen) { |
| 4617 | 880 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
|
881 "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
|
882 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
|
883 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
884 |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
885 result = PyString_FromStringAndSize((char *)NULL, 256); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
886 if (result == NULL) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
887 return NULL; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
888 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result); |
| 4889 | 889 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
|
890 c[i]=(unsigned char)i; |
| 4889 | 891 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
|
892 c[from[i]]=to[i]; |
| 4889 | 893 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
894 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
|
895 } |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
896 |
|
2f27501db3c3
Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents:
3679
diff
changeset
|
897 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
898 static char translate__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
899 "translate(s,table [,deletechars]) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
900 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
901 Return a copy of the string s, where all characters occurring\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
902 in the optional argument deletechars are removed, and the\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
903 remaining characters have been mapped through the given\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
904 translation table, which must be a string of length 256."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
905 |
| 4617 | 906 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
907 strop_translate(PyObject *self, PyObject *args) |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
908 { |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
909 register char *input, *table, *output; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
910 register int i, c, changed = 0; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
911 PyObject *input_obj; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
912 char *table1, *output_start, *del_table=NULL; |
| 4889 | 913 int 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
|
914 PyObject *result; |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
915 int trans_table[256]; |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
916 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
917 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
|
918 &table1, &tablen, &del_table, &dellen)) |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
919 return NULL; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
920 if (tablen != 256) { |
| 4617 | 921 PyErr_SetString(PyExc_ValueError, |
| 4889 | 922 "translation table must be 256 characters long"); |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
923 return NULL; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
924 } |
| 4889 | 925 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
926 table = table1; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
927 inlen = PyString_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
|
928 result = PyString_FromStringAndSize((char *)NULL, inlen); |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
929 if (result == NULL) |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
930 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
|
931 output_start = output = PyString_AsString(result); |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
932 input = PyString_AsString(input_obj); |
| 4889 | 933 |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
934 if (dellen == 0) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
935 /* If no deletions are required, use faster code */ |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
936 for (i = inlen; --i >= 0; ) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
937 c = Py_CHARMASK(*input++); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
938 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
939 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
|
940 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
941 if (changed) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
942 return result; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
943 Py_DECREF(result); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
944 Py_INCREF(input_obj); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
945 return input_obj; |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
946 } |
|
4895
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
947 |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
948 for (i = 0; i < 256; i++) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
949 trans_table[i] = Py_CHARMASK(table[i]); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
950 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
951 for (i = 0; i < dellen; i++) |
| 5380 | 952 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
|
953 |
|
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 (trans_table[c] != -1) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
957 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
958 continue; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
959 changed = 1; |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
960 } |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
961 if (!changed) { |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
962 Py_DECREF(result); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
963 Py_INCREF(input_obj); |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
964 return input_obj; |
|
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 /* Fix the size of the resulting string */ |
|
5568320da9b5
Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents:
4894
diff
changeset
|
967 if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
968 return NULL; |
|
3087
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
969 return result; |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
970 } |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
971 |
|
31684c5bb172
added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents:
2707
diff
changeset
|
972 |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
973 /* 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
|
974 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
975 /* |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
976 mymemfind |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
977 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
978 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
|
979 |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11882
diff
changeset
|
980 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
|
981 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
|
982 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
|
983 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
|
984 */ |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
985 static int mymemfind(char *mem, int len, char *pat, int 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
|
986 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
987 register int ii; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
988 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
989 /* 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
|
990 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
|
991 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
992 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
|
993 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
|
994 (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
|
995 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
|
996 return ii; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
997 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
998 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
999 return -1; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1000 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1001 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1002 /* |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1003 mymemcnt |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1004 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1005 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
|
1006 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
|
1007 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
|
1008 */ |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1009 static int mymemcnt(char *mem, int len, char *pat, int 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
|
1010 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1011 register int offset = 0; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1012 int nfound = 0; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1013 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1014 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
|
1015 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
|
1016 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
|
1017 break; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1018 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
|
1019 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
|
1020 nfound++; |
|
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 return nfound; |
|
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 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1025 /* |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1026 mymemreplace |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1027 |
|
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
|
1028 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
|
1029 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
|
1030 |
|
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
|
1031 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
|
1032 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
|
1033 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
|
1034 |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1035 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
|
1036 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
|
1037 -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
|
1038 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
|
1039 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1040 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
|
1041 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
|
1042 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
|
1043 */ |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1044 static char *mymemreplace(char *str, int len, char *pat, int pat_len, char *sub, int sub_len, int count, int *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
|
1045 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1046 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
|
1047 char *new_s; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1048 int nfound, offset, new_len; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1049 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1050 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
|
1051 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
|
1052 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1053 /* 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
|
1054 nfound = mymemcnt(str, len, pat, pat_len); |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1055 if (count > 0) |
|
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1056 nfound = nfound > count ? count : nfound; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1057 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
|
1058 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
|
1059 new_len = len + nfound*(sub_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
|
1060 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12025
diff
changeset
|
1061 new_s = (char *)PyMem_MALLOC(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
|
1062 if (new_s == NULL) return NULL; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1063 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1064 *out_len = new_len; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1065 out_s = new_s; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1066 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1067 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
|
1068 /* find index of next instance of pattern */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1069 offset = mymemfind(str, 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
|
1070 /* if not found, break out of loop */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1071 if (offset == -1) break; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1072 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1073 /* copy non matching part of input string */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1074 memcpy(new_s, str, offset); /* copy part of str before pat */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1075 str += offset + pat_len; /* move str past pattern */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1076 len -= offset + pat_len; /* reduce length of str remaining */ |
|
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 /* copy substitute into the 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 new_s += offset; /* move new_s to dest for sub string */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1080 memcpy(new_s, sub, sub_len); /* copy substring into new_s */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1081 new_s += sub_len; /* offset new_s past sub string */ |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1082 |
|
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1083 /* break when we've done count replacements */ |
|
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1084 if (--count == 0) break; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1085 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1086 /* copy any remaining values into output string */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1087 if (len > 0) |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1088 memcpy(new_s, str, len); |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1089 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
|
1090 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1091 return_same: |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1092 *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
|
1093 return str; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1094 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1095 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1096 |
|
6966
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1097 static char replace__doc__[] = |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1098 "replace (str, old, new[, maxsplit]) -> string\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1099 \n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1100 Return a copy of string str with all occurrences of substring\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1101 old replaced by new. If the optional argument maxsplit is\n\ |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1102 given, only the first maxsplit occurrences are replaced."; |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1103 |
|
eed63049df95
Added doc string, provided by Charles Waldman (with some reformatting
Guido van Rossum <guido@python.org>
parents:
6696
diff
changeset
|
1104 static PyObject * |
|
13494
4aa12e55bf95
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1105 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
|
1106 { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1107 char *str, *pat,*sub,*new_s; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1108 int len,pat_len,sub_len,out_len; |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1109 int count = 0; |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1110 PyObject *new; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1111 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11439
diff
changeset
|
1112 if (!PyArg_ParseTuple(args, "t#t#t#|i:replace", |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1113 &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
|
1114 &count)) |
|
5249
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1115 return NULL; |
|
8333
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1116 if (pat_len <= 0) { |
|
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1117 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
|
1118 return NULL; |
|
a4ae792b9b68
strop_replace(): balk if the pattern string is empty.
Guido van Rossum <guido@python.org>
parents:
7733
diff
changeset
|
1119 } |
|
6684
017467de7933
mymemreplace(), strop_replace(): Add support for optional 4th argument
Barry Warsaw <barry@python.org>
parents:
5380
diff
changeset
|
1120 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
|
1121 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
|
1122 PyErr_NoMemory(); |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1123 return NULL; |
|
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 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
|
1126 /* we're returning another reference to the input string */ |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1127 new = PyTuple_GetItem(args, 0); |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1128 Py_XINCREF(new); |
|
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 else { |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1131 new = 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
|
1132 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
|
1133 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1134 return new; |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1135 } |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1136 |
|
13005872d053
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents:
5188
diff
changeset
|
1137 |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1138 /* 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
|
1139 |
| 4889 | 1140 static PyMethodDef |
| 1141 strop_methods[] = { | |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1142 {"atof", strop_atof, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1143 METH_VARARGS, atof__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1144 {"atoi", strop_atoi, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1145 METH_VARARGS, atoi__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1146 {"atol", strop_atol, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1147 METH_VARARGS, atol__doc__}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1148 {"capitalize", strop_capitalize, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1149 METH_OLDARGS, capitalize__doc__}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1150 {"count", strop_count, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1151 METH_VARARGS, count__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1152 {"expandtabs", strop_expandtabs, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1153 METH_VARARGS, expandtabs__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1154 {"find", strop_find, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1155 METH_VARARGS, find__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1156 {"join", strop_joinfields, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1157 METH_VARARGS, joinfields__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1158 {"joinfields", strop_joinfields, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1159 METH_VARARGS, joinfields__doc__}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1160 {"lstrip", strop_lstrip, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1161 METH_OLDARGS, lstrip__doc__}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1162 {"lower", strop_lower, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1163 METH_OLDARGS, lower__doc__}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1164 {"maketrans", strop_maketrans, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1165 METH_VARARGS, maketrans__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1166 {"replace", strop_replace, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1167 METH_VARARGS, replace__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1168 {"rfind", strop_rfind, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1169 METH_VARARGS, rfind__doc__}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1170 {"rstrip", strop_rstrip, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1171 METH_OLDARGS, rstrip__doc__}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1172 {"split", strop_splitfields, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1173 METH_VARARGS, splitfields__doc__}, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1174 {"splitfields", strop_splitfields, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1175 METH_VARARGS, splitfields__doc__}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1176 {"strip", strop_strip, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1177 METH_OLDARGS, strip__doc__}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1178 {"swapcase", strop_swapcase, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1179 METH_OLDARGS, swapcase__doc__}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1180 {"translate", strop_translate, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
1181 METH_VARARGS, translate__doc__}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1182 {"upper", strop_upper, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
1183 METH_OLDARGS, upper__doc__}, |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1184 {NULL, NULL} /* sentinel */ |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1185 }; |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1186 |
|
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1187 |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
9402
diff
changeset
|
1188 DL_EXPORT(void) |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1189 initstrop(void) |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1190 { |
| 4617 | 1191 PyObject *m, *d, *s; |
|
1321
a80677798f84
stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1192 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
|
1193 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
|
1194 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
|
1195 (PyObject*)NULL, PYTHON_API_VERSION); |
| 4617 | 1196 d = PyModule_GetDict(m); |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1197 |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1198 /* 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
|
1199 n = 0; |
| 2310 | 1200 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
|
1201 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
|
1202 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
|
1203 } |
| 4617 | 1204 s = PyString_FromStringAndSize(buf, n); |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1205 if (s) { |
| 4617 | 1206 PyDict_SetItemString(d, "whitespace", s); |
| 1207 Py_DECREF(s); | |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1208 } |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1209 /* Create 'lowercase' object */ |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1210 n = 0; |
| 2310 | 1211 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
|
1212 if (islower(c)) |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1213 buf[n++] = c; |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1214 } |
| 4617 | 1215 s = PyString_FromStringAndSize(buf, n); |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1216 if (s) { |
| 4617 | 1217 PyDict_SetItemString(d, "lowercase", s); |
| 1218 Py_DECREF(s); | |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1219 } |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1220 |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1221 /* Create 'uppercase' object */ |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1222 n = 0; |
| 2310 | 1223 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
|
1224 if (isupper(c)) |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1225 buf[n++] = c; |
|
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1226 } |
| 4617 | 1227 s = PyString_FromStringAndSize(buf, n); |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1228 if (s) { |
| 4617 | 1229 PyDict_SetItemString(d, "uppercase", s); |
| 1230 Py_DECREF(s); | |
|
1322
f3c907efe5d7
* config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents:
1321
diff
changeset
|
1231 } |
|
1012
806689b93558
Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1232 } |
