annotate Modules/stropmodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 2d4c6cba01bf
children 017467de7933
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 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1888
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1888
diff changeset
3 The Netherlands.
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
8 documentation for any purpose and without fee is hereby granted,
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
10 both that copyright notice and this permission notice appear in
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
15 permission.
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4352
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* strop module */
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
34 #include "Python.h"
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
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
36 #ifdef HAVE_LIMITS_H
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
37 #include <limits.h>
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
38 #else
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
39 #define INT_MAX 2147483647
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
40 #endif
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
41
1321
a80677798f84 stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
42 #include <ctype.h>
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
43 /* 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
44 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
45
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
46 /* The lstrip(), rstrip() and strip() functions are implemented
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
47 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
48 type of strip should occur. */
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
49
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
50 #define LEFTSTRIP 0
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
51 #define RIGHTSTRIP 1
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
52 #define BOTHSTRIP 2
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
53
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
55 static PyObject *
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
56 split_whitespace(s, len, maxsplit)
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
57 char *s;
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
58 int len;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
59 int maxsplit;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 {
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
61 int i = 0, j, err;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
62 int countsplit = 0;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
63 PyObject* item;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
64 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
65
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if (list == NULL)
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return NULL;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 while (i < len) {
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
70 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
71 i = i+1;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 j = i;
2335
e11e6b358531 fix stupid bug in strip and split
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
74 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
75 i = i+1;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if (j < i) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
78 item = PyString_FromStringAndSize(s+j, (int)(i-j));
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
79 if (item == NULL)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
80 goto finally;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
81
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
82 err = PyList_Append(list, item);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
83 Py_DECREF(item);
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
84 if (err < 0)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
85 goto finally;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
86
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
87 countsplit++;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
88 if (maxsplit && (countsplit >= maxsplit)) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
89 item = PyString_FromStringAndSize(
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
90 s+i, (int)(len - i));
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
91 if (item == NULL)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
92 goto finally;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
93
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
94 err = PyList_Append(list, item);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
95 Py_DECREF(item);
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
96 if (err < 0)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
97 goto finally;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
98
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
99 i = len;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
100 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
101
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return list;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
105 finally:
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
106 Py_DECREF(list);
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
107 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
111 static PyObject *
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 strop_splitfields(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
113 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
114 PyObject *args;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 {
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
116 int len, n, i, j, err;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
117 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
118 char *s, *sub;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
119 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
120
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
121 sub = NULL;
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
122 n = 0;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
123 splitcount = 0;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
124 maxsplit = 0;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
125 if (!PyArg_ParseTuple(args, "s#|z#i", &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
126 return NULL;
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
127 if (sub == NULL)
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
128 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
129 if (n == 0) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
130 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
131 return NULL;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
134 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
135 if (list == NULL)
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 return NULL;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 i = j = 0;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 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
140 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
141 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
142 if (item == NULL)
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
143 goto fail;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
144 err = PyList_Append(list, item);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
145 Py_DECREF(item);
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
146 if (err < 0)
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
147 goto fail;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 i = j = i + n;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
149 splitcount++;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
150 if (maxsplit && (splitcount >= maxsplit))
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
151 break;
1012
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 else
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 i++;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
156 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
157 if (item == NULL)
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
158 goto fail;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
159 err = PyList_Append(list, item);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
160 Py_DECREF(item);
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
161 if (err < 0)
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
162 goto fail;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 return list;
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
165
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
166 fail:
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
167 Py_DECREF(list);
1394
98f51f61cb4b Fix memory leaks in join & joinfields
Guido van Rossum <guido@python.org>
parents: 1367
diff changeset
168 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
172 static PyObject *
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
173 strop_joinfields(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
174 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
175 PyObject *args;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
176 {
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
177 PyObject *seq;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
178 char *sep = NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
179 int seqlen, seplen = 0;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
180 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
181 PyObject *res = NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
182 char* p = NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
183 intargfunc getitemfunc;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
184
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
185 if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen))
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
186 return NULL;
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
187 if (sep == NULL) {
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
188 sep = " ";
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
189 seplen = 1;
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
190 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
191
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
192 seqlen = PySequence_Length(seq);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
193 if (seqlen < 0 && PyErr_Occurred())
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
194 return NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
195
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
196 if (seqlen == 1) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
197 /* 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
198 PyObject *item = PySequence_GetItem(seq, 0);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
199 if (item && !PyString_Check(item))
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
200 PyErr_SetString(PyExc_TypeError,
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
201 "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
202 return item;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
203 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
204
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
205 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
206 return NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
207 p = PyString_AsString(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
208
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
209 /* 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
210 * (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
211 * interface.
4894
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
212 */
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
213 if (PyList_Check(seq)) {
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
214 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
215 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
216 if (!PyString_Check(item)) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
217 PyErr_SetString(PyExc_TypeError,
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
218 "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
219 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
220 return NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
221 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
222 slen = PyString_GET_SIZE(item);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
223 while (reslen + slen + seplen >= sz) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
224 if (_PyString_Resize(&res, sz * 2)) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
225 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
226 return NULL;
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 sz *= 2;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
229 p = PyString_AsString(res) + reslen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
230 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
231 if (i > 0) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
232 memcpy(p, sep, seplen);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
233 p += seplen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
234 reslen += seplen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
235 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
236 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
237 p += slen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
238 reslen += slen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
239 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
240 if (_PyString_Resize(&res, reslen)) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
241 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
242 res = NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
243 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
244 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
245 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
246 else if (!PySequence_Check(seq)) {
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 a sequence");
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
249 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
250 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
251 /* type safe */
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
252 getitemfunc = seq->ob_type->tp_as_sequence->sq_item;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
253 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
254 PyObject *item = getitemfunc(seq, i);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
255 if (!item || !PyString_Check(item)) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
256 PyErr_SetString(PyExc_TypeError,
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
257 "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
258 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
259 Py_XDECREF(item);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
260 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
261 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
262 slen = PyString_GET_SIZE(item);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
263 while (reslen + slen + seplen >= sz) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
264 if (_PyString_Resize(&res, sz * 2)) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
265 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
266 Py_DECREF(item);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
267 return NULL;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
268 }
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
269 sz *= 2;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
270 p = PyString_AsString(res) + reslen;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
271 }
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
272 if (i > 0) {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
273 memcpy(p, sep, seplen);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
274 p += seplen;
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
275 reslen += seplen;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
276 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
277 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
278 p += slen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
279 reslen += slen;
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
280 Py_DECREF(item);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
281 }
4901
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
282 if (_PyString_Resize(&res, reslen)) {
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
283 Py_DECREF(res);
770104c8f84a New strop_joinfields implementation, highly optimized for Lists. All
Barry Warsaw <barry@python.org>
parents: 4895
diff changeset
284 res = NULL;
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
285 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
286 return res;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
287 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1048
diff changeset
288
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
289 static PyObject *
1787
cb84dd51c2aa * Modules/stropmodule.c: implement find/rfind instead of
Guido van Rossum <guido@python.org>
parents: 1760
diff changeset
290 strop_find(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
291 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
292 PyObject *args;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 {
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 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
295 int len, n, i = 0, last = INT_MAX;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
296
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
297 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
298 return NULL;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299
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
300 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
301 last = len;
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
302 if (last < 0)
3ec3d8807f9e Add optional 4th argument to [r]find and [r]index (end of slice).
Guido van Rossum <guido@python.org>
parents: 5120
diff changeset
303 last += 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
304 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
305 last = 0;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
306 if (i < 0)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
307 i += len;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
308 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
309 i = 0;
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 if (n == 0)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
312 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
313
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
314 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
315 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
316 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
317 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
318 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
319
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
320 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
321 }
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
322
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
323
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
324 static PyObject *
1787
cb84dd51c2aa * Modules/stropmodule.c: implement find/rfind instead of
Guido van Rossum <guido@python.org>
parents: 1760
diff changeset
325 strop_rfind(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
326 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
327 PyObject *args;
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
328 {
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
329 char *s, *sub;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
330 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
331 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
332
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
333 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
334 return NULL;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
335
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
336 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
337 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
338 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
339 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
340 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
341 last = 0;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
342 if (i < 0)
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
343 i += len;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
344 if (i < 0)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
345 i = 0;
1367
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
346
ee4a2f893254 Added rindex(). index() and rindex() interpret negative start index
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
347 if (n == 0)
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
348 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
349
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
350 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
351 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
352 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
353 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
354
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
355 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
356 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
358 static PyObject *
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
359 do_strip(args, striptype)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
360 PyObject *args;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
361 int striptype;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
362 {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
363 char *s;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
364 int len, i, j;
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
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
367 if (!PyArg_Parse(args, "s#", &s, &len))
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
368 return NULL;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
369
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
370 i = 0;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
371 if (striptype != RIGHTSTRIP) {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
372 while (i < len && isspace(Py_CHARMASK(s[i]))) {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
373 i++;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
374 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
375 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
376
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 j = len;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
379 if (striptype != LEFTSTRIP) {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
380 do {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
381 j--;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
382 } while (j >= i && isspace(Py_CHARMASK(s[j])));
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 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
385
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
386 if (i == 0 && j == len) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
387 Py_INCREF(args);
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
388 return args;
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
389 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
390 else
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
391 return PyString_FromStringAndSize(s+i, j-i);
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
392 }
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
393
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
395 static PyObject *
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 strop_strip(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
397 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
398 PyObject *args;
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 {
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
400 return do_strip(args, BOTHSTRIP);
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
401 }
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
403 static PyObject *
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
404 strop_lstrip(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
405 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
406 PyObject *args;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
407 {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
408 return do_strip(args, LEFTSTRIP);
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
409 }
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
411 static PyObject *
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
412 strop_rstrip(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
413 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
414 PyObject *args;
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
415 {
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
416 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
417 }
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
420 static PyObject *
4894
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
421 strop_lower(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
422 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
423 PyObject *args;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
424 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
425 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
426 int i, n;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
427 PyObject *new;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
428 int changed;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
429
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
430 if (!PyArg_Parse(args, "s#", &s, &n))
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
431 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
432 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
433 if (new == NULL)
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
434 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
435 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
436 changed = 0;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
437 for (i = 0; i < n; i++) {
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
438 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
439 if (isupper(c)) {
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
440 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
441 *s_new = tolower(c);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
442 } else
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
443 *s_new = c;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
444 s_new++;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
445 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
446 if (!changed) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
447 Py_DECREF(new);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
448 Py_INCREF(args);
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
449 return args;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
450 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
451 return new;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
452 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
453
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
454
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
455 static PyObject *
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
456 strop_upper(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
457 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
458 PyObject *args;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
459 {
4894
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
460 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
461 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
462 PyObject *new;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
463 int changed;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
464
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
465 if (!PyArg_Parse(args, "s#", &s, &n))
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
466 return NULL;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
467 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
468 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
469 return NULL;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
470 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
471 changed = 0;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
472 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
473 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
474 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
475 changed = 1;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
476 *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
477 } else
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
478 *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
479 s_new++;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
480 }
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
481 if (!changed) {
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
482 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
483 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
484 return args;
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
485 }
f31f47ff1fdd strop_upper(), strop_lower(): shared code version caused to much of a
Barry Warsaw <barry@python.org>
parents: 4889
diff changeset
486 return new;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
487 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
488
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
489
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
490 static PyObject *
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
491 strop_capitalize(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
492 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
493 PyObject *args;
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
494 {
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
495 char *s, *s_new;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
496 int i, n;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
497 PyObject *new;
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
498 int changed;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
499
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
500 if (!PyArg_Parse(args, "s#", &s, &n))
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
501 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
502 new = PyString_FromStringAndSize(NULL, n);
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
503 if (new == NULL)
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
504 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
505 s_new = PyString_AsString(new);
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
506 changed = 0;
3679
b8980735d03b Test for zero-length argument in capitalize().
Guido van Rossum <guido@python.org>
parents: 3676
diff changeset
507 if (0 < n) {
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
508 int c = Py_CHARMASK(*s++);
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
509 if (islower(c)) {
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
510 changed = 1;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
511 *s_new = toupper(c);
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
512 } else
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
513 *s_new = c;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
514 s_new++;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
515 }
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
516 for (i = 1; i < n; i++) {
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
517 int c = Py_CHARMASK(*s++);
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
518 if (isupper(c)) {
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
519 changed = 1;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
520 *s_new = tolower(c);
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
521 } else
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
522 *s_new = c;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
523 s_new++;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
524 }
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
525 if (!changed) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
526 Py_DECREF(new);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
527 Py_INCREF(args);
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
528 return args;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
529 }
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
530 return new;
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
531 }
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
532
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
533
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
534 static PyObject *
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
535 strop_swapcase(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
536 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
537 PyObject *args;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
538 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
539 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
540 int i, n;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
541 PyObject *new;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
542 int changed;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
543
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
544 if (!PyArg_Parse(args, "s#", &s, &n))
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
545 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
546 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
547 if (new == NULL)
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
548 return NULL;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
549 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
550 changed = 0;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
551 for (i = 0; i < n; i++) {
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
552 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
553 if (islower(c)) {
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
554 changed = 1;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
555 *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
556 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
557 else if (isupper(c)) {
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
558 changed = 1;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
559 *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
560 }
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
561 else
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
562 *s_new = c;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1337
diff changeset
563 s_new++;
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
564 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
565 if (!changed) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
566 Py_DECREF(new);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
567 Py_INCREF(args);
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
568 return args;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
569 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
570 return new;
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
571 }
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
572
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
573
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
574 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
575 strop_atoi(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
576 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
577 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
578 {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
579 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
580 extern unsigned long
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
581 PyOS_strtoul Py_PROTO((const char *, char **, int));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
582 char *s, *end;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
583 int base = 10;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
584 long x;
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
585 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
586
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
587 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
588 return NULL;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
589
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
590 if ((base != 0 && base < 2) || base > 36) {
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
591 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
592 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
593 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
594
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
595 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
596 s++;
4094
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
597 if (s[0] == '\0') {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
598 PyErr_SetString(PyExc_ValueError, "empty string for atoi()");
4094
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
599 return NULL;
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
600 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
601 errno = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
602 if (base == 0 && s[0] == '0')
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
603 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
604 else
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
605 x = PyOS_strtol(s, &end, base);
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
606 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
607 end++;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
608 if (*end != '\0') {
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
609 sprintf(buffer, "invalid literal for atoi(): %.200s", s);
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
610 PyErr_SetString(PyExc_ValueError, buffer);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
611 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
612 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
613 else if (errno != 0) {
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
614 sprintf(buffer, "atoi() literal too large: %.200s", s);
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
615 PyErr_SetString(PyExc_ValueError, buffer);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
616 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
617 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
618 return PyInt_FromLong(x);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
619 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
620
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
621
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
622 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
623 strop_atol(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
624 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
625 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
626 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
627 char *s, *end;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
628 int base = 10;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
629 PyObject *x;
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
630 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
631
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
632 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
633 return NULL;
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
634
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
635 if ((base != 0 && base < 2) || base > 36) {
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
636 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
637 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
638 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
639
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
640 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
641 s++;
4094
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
642 if (s[0] == '\0') {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
643 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
644 return NULL;
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
645 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
646 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
647 if (x == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
648 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
649 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
650 end++;
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
651 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
652 end++;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
653 if (*end != '\0') {
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
654 sprintf(buffer, "invalid literal for atol(): %.200s", s);
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
655 PyErr_SetString(PyExc_ValueError, buffer);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
656 Py_DECREF(x);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
657 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
658 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
659 return x;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
660 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
661
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
662
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
663 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
664 strop_atof(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
665 PyObject *self; /* Not used */
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
666 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
667 {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
668 extern double strtod Py_PROTO((const char *, char **));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
669 char *s, *end;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
670 double x;
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
671 char buffer[256]; /* For errors */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
672
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
673 if (!PyArg_Parse(args, "s", &s))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
674 return NULL;
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
675 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
676 s++;
4094
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
677 if (s[0] == '\0') {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
678 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
679 return NULL;
8438184e448e Raise ValueError on empty string passed into atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4035
diff changeset
680 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
681 errno = 0;
5120
a48f3678f9e1 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4901
diff changeset
682 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
683 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
684 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
685 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
686 end++;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
687 if (*end != '\0') {
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
688 sprintf(buffer, "invalid literal for atof(): %.200s", s);
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
689 PyErr_SetString(PyExc_ValueError, buffer);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
690 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
691 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
692 else if (errno != 0) {
4296
75d75732a9e4 Add text of argument to errors for atoi(), atol(), atof().
Guido van Rossum <guido@python.org>
parents: 4094
diff changeset
693 sprintf(buffer, "atof() literal too large: %.200s", s);
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
694 PyErr_SetString(PyExc_ValueError, buffer);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
695 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
696 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
697 return PyFloat_FromDouble(x);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
698 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
699
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
700
3799
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
701 static PyObject *
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
702 strop_maketrans(self, args)
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
703 PyObject *self; /* Not used */
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
704 PyObject *args;
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
705 {
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
706 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
707 int i, fromlen=0, tolen=0;
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
708 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
709
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
710 if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen))
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
711 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
712
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
713 if (fromlen != tolen) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
714 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
715 "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
716 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
717 }
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
718
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
719 result = PyString_FromStringAndSize((char *)NULL, 256);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
720 if (result == NULL)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
721 return NULL;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
722 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
723 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
724 c[i]=(unsigned char)i;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
725 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
726 c[from[i]]=to[i];
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
727
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
728 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
729 }
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
730
2f27501db3c3 Added 3rd optional argument to translate(), a string of characters to delete.
Guido van Rossum <guido@python.org>
parents: 3679
diff changeset
731
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
732 static PyObject *
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
733 strop_translate(self, args)
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
734 PyObject *self;
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
735 PyObject *args;
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
736 {
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
737 register char *input, *table, *output;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
738 register int i, c, changed = 0;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
739 PyObject *input_obj;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
740 char *table1, *output_start, *del_table=NULL;
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
741 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
742 PyObject *result;
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
743 int trans_table[256];
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
744
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
745 if (!PyArg_ParseTuple(args, "Ss#|s#", &input_obj,
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
746 &table1, &tablen, &del_table, &dellen))
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
747 return NULL;
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
748 if (tablen != 256) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
749 PyErr_SetString(PyExc_ValueError,
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
750 "translation table must be 256 characters long");
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
751 return NULL;
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
752 }
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
753
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
754 table = table1;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
755 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
756 result = PyString_FromStringAndSize((char *)NULL, inlen);
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
757 if (result == NULL)
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
758 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
759 output_start = output = PyString_AsString(result);
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
760 input = PyString_AsString(input_obj);
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
761
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
762 if (dellen == 0) {
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
763 /* If no deletions are required, use faster code */
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
764 for (i = inlen; --i >= 0; ) {
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
765 c = Py_CHARMASK(*input++);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
766 if (Py_CHARMASK((*output++ = table[c])) != c)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
767 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
768 }
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
769 if (changed)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
770 return result;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
771 Py_DECREF(result);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
772 Py_INCREF(input_obj);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
773 return input_obj;
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
774 }
4895
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
775
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
776 for (i = 0; i < 256; i++)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
777 trans_table[i] = Py_CHARMASK(table[i]);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
778
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
779 for (i = 0; i < dellen; i++)
5380
2d4c6cba01bf Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 5249
diff changeset
780 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
781
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
782 for (i = inlen; --i >= 0; ) {
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
783 c = Py_CHARMASK(*input++);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
784 if (trans_table[c] != -1)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
785 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
786 continue;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
787 changed = 1;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
788 }
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
789 if (!changed) {
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
790 Py_DECREF(result);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
791 Py_INCREF(input_obj);
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
792 return input_obj;
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
793 }
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
794 /* Fix the size of the resulting string */
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
795 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
5568320da9b5 Rewrote translate() as follows:
Guido van Rossum <guido@python.org>
parents: 4894
diff changeset
796 return NULL;
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
797 return result;
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
798 }
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
799
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
800
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
801 /* 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
802
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
803 /*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
804 mymemfind
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
805
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
806 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
807
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
808 Locates the first occurance in the memory pointed to by MEM of the
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
809 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
810 found, or -1 if not found. If len of PAT is greater than length of
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
811 MEM, the function returns -1.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
812 */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
813 static int 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
814 char *mem;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
815 int len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
816 char *pat;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
817 int pat_len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
818 {
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
819 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
820
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
821 /* 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
822 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
823
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
824 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
825 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
826 (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
827 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
828 return ii;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
829 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
830 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
831 return -1;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
832 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
833
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
834 /*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
835 mymemcnt
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
836
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
837 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
838 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
839 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
840 */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
841 static int mymemcnt(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
842 char *mem;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
843 int len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
844 char *pat;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
845 int pat_len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
846 {
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
847 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
848 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
849
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
850 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
851 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
852 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
853 break;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
854 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
855 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
856 nfound++;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
857 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
858 return nfound;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
859 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
860
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
861 /*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
862 mymemreplace
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
863
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
864 Return a string in which all occurences of PAT in memory STR are
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
865 replaced with SUB.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
866
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
867 If length of PAT is less than length of STR or there are no occurences
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
868 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
869 string is allocated here and returned.
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
870
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
871 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
872 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
873 -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
874 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
875
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
876 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
877 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
878 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
879 */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
880 static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, out_len)
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
881 char *str;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
882 int len; /* input string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
883 char *pat;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
884 int pat_len; /* pattern string to find */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
885 char *sub;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
886 int sub_len; /* substitution string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
887 int *out_len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
888
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
889 {
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
890 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
891 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
892 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
893
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
894 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
895 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
896
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
897 /* 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
898 nfound = mymemcnt(str, len, pat, pat_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
899 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
900 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
901 new_len = len + nfound*(sub_len - pat_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
902
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
903 new_s = (char *)malloc(new_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
904 if (new_s == NULL) return NULL;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
905
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
906 *out_len = new_len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
907 out_s = new_s;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
908
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
909 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
910 /* find index of next instance of pattern */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
911 offset = mymemfind(str, len, pat, pat_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
912 /* if not found, break out of loop */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
913 if (offset == -1) break;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
914
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
915 /* copy non matching part of input string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
916 memcpy(new_s, str, offset); /* copy part of str before pat */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
917 str += offset + pat_len; /* move str past pattern */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
918 len -= offset + pat_len; /* reduce length of str remaining */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
919
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
920 /* copy substitute into the output string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
921 new_s += offset; /* move new_s to dest for sub string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
922 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
923 new_s += sub_len; /* offset new_s past sub string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
924 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
925 /* copy any remaining values into output string */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
926 if (len > 0)
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
927 memcpy(new_s, str, len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
928 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
929
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
930 return_same:
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
931 *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
932 return str;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
933 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
934
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
935
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
936 static PyObject*
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
937 strop_replace(self, args)
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
938 PyObject *self; /* Not used */
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
939 PyObject *args;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
940 {
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
941 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
942 int len,pat_len,sub_len,out_len;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
943 PyObject *new;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
944
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
945 if (!PyArg_ParseTuple(args, "s#s#s#",
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
946 &str, &len, &pat, &pat_len, &sub, &sub_len))
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
947 return NULL;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
948 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,&out_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
949 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
950 PyErr_NoMemory();
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
951 return NULL;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
952 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
953 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
954 /* 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
955 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
956 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
957 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
958 else {
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
959 new = PyString_FromStringAndSize(new_s, out_len);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
960 free(new_s);
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
961 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
962 return new;
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
963 }
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
964
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
965
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 /* 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
967
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
968 static PyMethodDef
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
969 strop_methods[] = {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1394
diff changeset
970 {"atof", strop_atof},
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
971 {"atoi", strop_atoi, 1},
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
972 {"atol", strop_atol, 1},
3676
74764b3ec1bd added capitalize()
Guido van Rossum <guido@python.org>
parents: 3087
diff changeset
973 {"capitalize", strop_capitalize},
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
974 {"find", strop_find, 1},
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
975 {"join", strop_joinfields, 1},
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
976 {"joinfields", strop_joinfields, 1},
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
977 {"lstrip", strop_lstrip},
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
978 {"lower", strop_lower},
5249
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
979 {"maketrans", strop_maketrans, 1},
13005872d053 Added replace() implementation by Perry Stoll (debugged and reformatted by me).
Guido van Rossum <guido@python.org>
parents: 5188
diff changeset
980 {"replace", strop_replace, 1},
4889
9c6b5a65ce63 Several changes:
Barry Warsaw <barry@python.org>
parents: 4617
diff changeset
981 {"rfind", strop_rfind, 1},
3978
b93c4cfc2cfe Added lstrip() and rstrip().
Guido van Rossum <guido@python.org>
parents: 3799
diff changeset
982 {"rstrip", strop_rstrip},
2707
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
983 {"split", strop_splitfields, 1},
1306e761a623 unified join(fields), split(fields)
Guido van Rossum <guido@python.org>
parents: 2335
diff changeset
984 {"splitfields", strop_splitfields, 1},
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 {"strip", strop_strip},
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
986 {"swapcase", strop_swapcase},
3087
31684c5bb172 added strop.translate(s, table)
Guido van Rossum <guido@python.org>
parents: 2707
diff changeset
987 {"translate", strop_translate, 1},
1048
c1ebfde341a3 Makefile uses $> more often; cgen supports filename argument; added
Guido van Rossum <guido@python.org>
parents: 1012
diff changeset
988 {"upper", strop_upper},
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 {NULL, NULL} /* sentinel */
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 };
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
991
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
992
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 void
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 initstrop()
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
996 PyObject *m, *d, *s;
1321
a80677798f84 stropmodule.c: use C isspace(c) to test for whitespace; add
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
997 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
998 int c, n;
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
999 m = Py_InitModule("strop", strop_methods);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1000 d = PyModule_GetDict(m);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1001
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1002 /* 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
1003 n = 0;
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
1004 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
1005 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
1006 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
1007 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1008 s = PyString_FromStringAndSize(buf, n);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1009 if (s) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1010 PyDict_SetItemString(d, "whitespace", s);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1011 Py_DECREF(s);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1012 }
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1013 /* Create 'lowercase' object */
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1014 n = 0;
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
1015 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
1016 if (islower(c))
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1017 buf[n++] = c;
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1018 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1019 s = PyString_FromStringAndSize(buf, n);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1020 if (s) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1021 PyDict_SetItemString(d, "lowercase", s);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1022 Py_DECREF(s);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1023 }
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1024
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1025 /* Create 'uppercase' object */
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1026 n = 0;
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
1027 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
1028 if (isupper(c))
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1029 buf[n++] = c;
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1030 }
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1031 s = PyString_FromStringAndSize(buf, n);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1032 if (s) {
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1033 PyDict_SetItemString(d, "uppercase", s);
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1034 Py_DECREF(s);
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1035 }
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1321
diff changeset
1036
4617
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1037 if (PyErr_Occurred())
87999dc4d798 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
1038 Py_FatalError("can't initialize module strop");
1012
806689b93558 Re-implement some operations from string.py in C, for speed.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 }