annotate Modules/stropmodule.c @ 27998:e27f58a50737 legacy-trunk

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