blob: 4344bc00a3ad74300aa4a17c581832dc6c08be87 [file] [log] [blame]
Guido van Rossume270b431992-09-03 20:21:07 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossume270b431992-09-03 20:21:07 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossume270b431992-09-03 20:21:07 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossume270b431992-09-03 20:21:07 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossume270b431992-09-03 20:21:07 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossume270b431992-09-03 20:21:07 +000029
30******************************************************************/
31
32/* strop module */
33
Guido van Rossum983c9301997-12-29 19:52:29 +000034static char strop_module__doc__[] =
Guido van Rossum23e21e71997-12-29 19:57:36 +000035"Common string manipulations, optimized for speed.\n\
Guido van Rossum786205e1997-12-30 05:10:14 +000036\n\
Guido van Rossum983c9301997-12-29 19:52:29 +000037Always use \"import string\" rather than referencing\n\
Guido van Rossum23e21e71997-12-29 19:57:36 +000038this module directly.";
Guido van Rossum983c9301997-12-29 19:52:29 +000039
Barry Warsawf5256011996-12-09 18:35:56 +000040#include "Python.h"
Guido van Rossume270b431992-09-03 20:21:07 +000041
Guido van Rossum7b7c5781997-03-14 04:13:56 +000042#ifdef HAVE_LIMITS_H
43#include <limits.h>
44#else
45#define INT_MAX 2147483647
46#endif
47
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000048#include <ctype.h>
Guido van Rossume22e6441993-07-09 10:51:31 +000049/* XXX This file assumes that the <ctype.h> is*() functions
50 XXX are defined for all 8-bit characters! */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000051
Guido van Rossum7999a5c1996-08-08 19:16:15 +000052/* The lstrip(), rstrip() and strip() functions are implemented
53 in do_strip(), which uses an additional parameter to indicate what
54 type of strip should occur. */
55
56#define LEFTSTRIP 0
57#define RIGHTSTRIP 1
58#define BOTHSTRIP 2
59
Guido van Rossume270b431992-09-03 20:21:07 +000060
Barry Warsawf5256011996-12-09 18:35:56 +000061static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +000062split_whitespace(s, len, maxsplit)
Guido van Rossume270b431992-09-03 20:21:07 +000063 char *s;
Guido van Rossum009e79b1995-05-03 17:40:23 +000064 int len;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000065 int maxsplit;
Guido van Rossum009e79b1995-05-03 17:40:23 +000066{
Barry Warsawe8fc29c1997-01-03 22:45:34 +000067 int i = 0, j, err;
68 int countsplit = 0;
69 PyObject* item;
70 PyObject *list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +000071
Guido van Rossume270b431992-09-03 20:21:07 +000072 if (list == NULL)
73 return NULL;
74
Guido van Rossume270b431992-09-03 20:21:07 +000075 while (i < len) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +000076 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000077 i = i+1;
78 }
79 j = i;
Guido van Rossumee1813d1995-02-14 00:58:59 +000080 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000081 i = i+1;
82 }
83 if (j < i) {
Barry Warsawf5256011996-12-09 18:35:56 +000084 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Barry Warsawe8fc29c1997-01-03 22:45:34 +000085 if (item == NULL)
86 goto finally;
87
Barry Warsawf5256011996-12-09 18:35:56 +000088 err = PyList_Append(list, item);
89 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000090 if (err < 0)
91 goto finally;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000092
93 countsplit++;
Barry Warsaw93be92d1997-12-02 00:29:30 +000094 while (i < len && isspace(Py_CHARMASK(s[i]))) {
95 i = i+1;
96 }
97 if (maxsplit && (countsplit >= maxsplit) && i < len) {
Barry Warsawf5256011996-12-09 18:35:56 +000098 item = PyString_FromStringAndSize(
99 s+i, (int)(len - i));
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000100 if (item == NULL)
101 goto finally;
102
Barry Warsawf5256011996-12-09 18:35:56 +0000103 err = PyList_Append(list, item);
104 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000105 if (err < 0)
106 goto finally;
107
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000108 i = len;
109 }
Guido van Rossume270b431992-09-03 20:21:07 +0000110 }
111 }
Guido van Rossume270b431992-09-03 20:21:07 +0000112 return list;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000113 finally:
114 Py_DECREF(list);
115 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000116}
117
118
Guido van Rossum983c9301997-12-29 19:52:29 +0000119static char splitfields__doc__[] =
120"split(str [,sep [,maxsplit]]) -> list of strings\n\
121splitfields(str [,sep [,maxsplit]]) -> list of strings\n\
122\n\
123Return a list of the words in the string s, using sep as the\n\
124delimiter string. If maxsplit is nonzero, splits into at most\n\
125maxsplit words If sep is not specified, any whitespace string\n\
126is a separator. Maxsplit defaults to 0.\n\
127\n\
128(split and splitfields are synonymous)";
129
Barry Warsawf5256011996-12-09 18:35:56 +0000130static PyObject *
Guido van Rossume270b431992-09-03 20:21:07 +0000131strop_splitfields(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000132 PyObject *self; /* Not used */
133 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000134{
Guido van Rossum572d2d91993-11-05 10:14:49 +0000135 int len, n, i, j, err;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000136 int splitcount, maxsplit;
Guido van Rossume270b431992-09-03 20:21:07 +0000137 char *s, *sub;
Barry Warsawf5256011996-12-09 18:35:56 +0000138 PyObject *list, *item;
Guido van Rossume270b431992-09-03 20:21:07 +0000139
Guido van Rossum009e79b1995-05-03 17:40:23 +0000140 sub = NULL;
141 n = 0;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000142 splitcount = 0;
143 maxsplit = 0;
Barry Warsawf5256011996-12-09 18:35:56 +0000144 if (!PyArg_ParseTuple(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
Guido van Rossume270b431992-09-03 20:21:07 +0000145 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000146 if (sub == NULL)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000147 return split_whitespace(s, len, maxsplit);
Guido van Rossume270b431992-09-03 20:21:07 +0000148 if (n == 0) {
Barry Warsawf5256011996-12-09 18:35:56 +0000149 PyErr_SetString(PyExc_ValueError, "empty separator");
Guido van Rossume270b431992-09-03 20:21:07 +0000150 return NULL;
151 }
152
Barry Warsawf5256011996-12-09 18:35:56 +0000153 list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +0000154 if (list == NULL)
155 return NULL;
156
157 i = j = 0;
158 while (i+n <= len) {
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000159 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000160 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000161 if (item == NULL)
162 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000163 err = PyList_Append(list, item);
164 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000165 if (err < 0)
166 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000167 i = j = i + n;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000168 splitcount++;
169 if (maxsplit && (splitcount >= maxsplit))
170 break;
Guido van Rossume270b431992-09-03 20:21:07 +0000171 }
172 else
173 i++;
174 }
Barry Warsawf5256011996-12-09 18:35:56 +0000175 item = PyString_FromStringAndSize(s+j, (int)(len-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000176 if (item == NULL)
177 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000178 err = PyList_Append(list, item);
179 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000180 if (err < 0)
181 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000182
183 return list;
Guido van Rossum572d2d91993-11-05 10:14:49 +0000184
185 fail:
Barry Warsawf5256011996-12-09 18:35:56 +0000186 Py_DECREF(list);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000187 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000188}
189
190
Guido van Rossum983c9301997-12-29 19:52:29 +0000191static char joinfields__doc__[] =
192"join(list [,sep]) -> string\n\
193joinfields(list [,sep]) -> string\n\
194\n\
195Return a string composed of the words in list, with\n\
Guido van Rossum23e21e71997-12-29 19:57:36 +0000196intervening occurences of sep. Sep defaults to a single\n\
197space.\n\
Guido van Rossum983c9301997-12-29 19:52:29 +0000198\n\
Guido van Rossum23e21e71997-12-29 19:57:36 +0000199(join and joinfields are synonymous)";
Guido van Rossum983c9301997-12-29 19:52:29 +0000200
Barry Warsawf5256011996-12-09 18:35:56 +0000201static PyObject *
Guido van Rossumc89705d1992-11-26 08:54:07 +0000202strop_joinfields(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000203 PyObject *self; /* Not used */
204 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000205{
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000206 PyObject *seq;
207 char *sep = NULL;
208 int seqlen, seplen = 0;
209 int i, reslen = 0, slen = 0, sz = 100;
210 PyObject *res = NULL;
211 char* p = NULL;
212 intargfunc getitemfunc;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000213
Barry Warsawf5256011996-12-09 18:35:56 +0000214 if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000215 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000216 if (sep == NULL) {
217 sep = " ";
218 seplen = 1;
219 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000220
221 seqlen = PySequence_Length(seq);
222 if (seqlen < 0 && PyErr_Occurred())
223 return NULL;
224
225 if (seqlen == 1) {
226 /* Optimization if there's only one item */
227 PyObject *item = PySequence_GetItem(seq, 0);
228 if (item && !PyString_Check(item))
229 PyErr_SetString(PyExc_TypeError,
230 "first argument must be sequence of strings");
231 return item;
232 }
233
234 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
235 return NULL;
236 p = PyString_AsString(res);
237
238 /* optimize for lists, since it's the most common case. all others
239 * (tuples and arbitrary sequences) just use the sequence abstract
240 * interface.
Barry Warsaw04d2d151997-01-03 23:46:51 +0000241 */
242 if (PyList_Check(seq)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000243 for (i = 0; i < seqlen; i++) {
244 PyObject *item = PyList_GET_ITEM(seq, i);
245 if (!PyString_Check(item)) {
246 PyErr_SetString(PyExc_TypeError,
247 "first argument must be sequence of strings");
248 Py_DECREF(res);
249 return NULL;
250 }
251 slen = PyString_GET_SIZE(item);
252 while (reslen + slen + seplen >= sz) {
253 if (_PyString_Resize(&res, sz * 2)) {
254 Py_DECREF(res);
255 return NULL;
256 }
257 sz *= 2;
258 p = PyString_AsString(res) + reslen;
259 }
260 if (i > 0) {
261 memcpy(p, sep, seplen);
262 p += seplen;
263 reslen += seplen;
264 }
265 memcpy(p, PyString_AS_STRING(item), slen);
266 p += slen;
267 reslen += slen;
268 }
269 if (_PyString_Resize(&res, reslen)) {
270 Py_DECREF(res);
271 res = NULL;
272 }
273 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000274 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000275 else if (!PySequence_Check(seq)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000276 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000277 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000278 return NULL;
279 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000280 /* type safe */
281 getitemfunc = seq->ob_type->tp_as_sequence->sq_item;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000282 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000283 PyObject *item = getitemfunc(seq, i);
284 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000285 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000286 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000287 Py_DECREF(res);
288 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000289 return NULL;
290 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000291 slen = PyString_GET_SIZE(item);
292 while (reslen + slen + seplen >= sz) {
293 if (_PyString_Resize(&res, sz * 2)) {
294 Py_DECREF(res);
295 Py_DECREF(item);
296 return NULL;
297 }
298 sz *= 2;
299 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000300 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000301 if (i > 0) {
302 memcpy(p, sep, seplen);
303 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000304 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000305 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000306 memcpy(p, PyString_AS_STRING(item), slen);
307 p += slen;
308 reslen += slen;
309 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000310 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000311 if (_PyString_Resize(&res, reslen)) {
312 Py_DECREF(res);
313 res = NULL;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000314 }
315 return res;
316}
317
Guido van Rossum983c9301997-12-29 19:52:29 +0000318
319static char find__doc__[] =
320"find(s, sub [,start [,end]]) -> in\n\
321\n\
322Return the lowest index in s where substring sub is found,\n\
323such that sub is contained within s[start,end]. Optional\n\
324arguments start and end are interpreted as in slice notation.\n\
325\n\
326Return -1 on failure.";
327
Barry Warsawf5256011996-12-09 18:35:56 +0000328static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000329strop_find(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000330 PyObject *self; /* Not used */
331 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000332{
333 char *s, *sub;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000334 int len, n, i = 0, last = INT_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000335
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000336 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000337 return NULL;
338
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000339 if (last > len)
340 last = len;
341 if (last < 0)
342 last += len;
343 if (last < 0)
344 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000345 if (i < 0)
346 i += len;
347 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000348 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000349
350 if (n == 0)
Barry Warsawf5256011996-12-09 18:35:56 +0000351 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000352
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000353 last -= n;
354 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000355 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000356 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000357 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000358
Barry Warsawf5256011996-12-09 18:35:56 +0000359 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000360}
361
362
Guido van Rossum983c9301997-12-29 19:52:29 +0000363static char rfind__doc__[] =
364"rfind(s, sub [,start [,end]]) -> int\n\
365\n\
366Return the highest index in s where substring sub is found,\n\
367such that sub is contained within s[start,end]. Optional\n\
368arguments start and end are interpreted as in slice notation.\n\
369\n\
370Return -1 on failure.";
371
Barry Warsawf5256011996-12-09 18:35:56 +0000372static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000373strop_rfind(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000374 PyObject *self; /* Not used */
375 PyObject *args;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000376{
377 char *s, *sub;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000378 int len, n, j;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000379 int i = 0, last = INT_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000380
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000381 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000382 return NULL;
383
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000384 if (last > len)
385 last = len;
386 if (last < 0)
387 last += len;
388 if (last < 0)
389 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000390 if (i < 0)
391 i += len;
392 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000393 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000394
395 if (n == 0)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000396 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000397
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000398 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000399 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000400 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000401 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000402
Barry Warsawf5256011996-12-09 18:35:56 +0000403 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000404}
405
Guido van Rossum983c9301997-12-29 19:52:29 +0000406
Barry Warsawf5256011996-12-09 18:35:56 +0000407static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000408do_strip(args, striptype)
Barry Warsawf5256011996-12-09 18:35:56 +0000409 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000410 int striptype;
411{
412 char *s;
413 int len, i, j;
414
415
Barry Warsawf5256011996-12-09 18:35:56 +0000416 if (!PyArg_Parse(args, "s#", &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000417 return NULL;
418
419 i = 0;
420 if (striptype != RIGHTSTRIP) {
421 while (i < len && isspace(Py_CHARMASK(s[i]))) {
422 i++;
423 }
424 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000425
426 j = len;
427 if (striptype != LEFTSTRIP) {
428 do {
429 j--;
430 } while (j >= i && isspace(Py_CHARMASK(s[j])));
431 j++;
432 }
433
434 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000435 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000436 return args;
437 }
438 else
Barry Warsawf5256011996-12-09 18:35:56 +0000439 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000440}
441
Guido van Rossume270b431992-09-03 20:21:07 +0000442
Guido van Rossum983c9301997-12-29 19:52:29 +0000443static char strip__doc__[] =
444"strip(s) -> string\n\
445\n\
446Return a copy of the string s with leading and trailing\n\
447whitespace removed.";
448
Barry Warsawf5256011996-12-09 18:35:56 +0000449static PyObject *
Guido van Rossume270b431992-09-03 20:21:07 +0000450strop_strip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000451 PyObject *self; /* Not used */
452 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000453{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000454 return do_strip(args, BOTHSTRIP);
455}
Guido van Rossume270b431992-09-03 20:21:07 +0000456
Guido van Rossum983c9301997-12-29 19:52:29 +0000457
458static char lstrip__doc__[] =
459"lstrip(s) -> string\n\
460\n\
461Return a copy of the string s with leading whitespace removed.";
462
Barry Warsawf5256011996-12-09 18:35:56 +0000463static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000464strop_lstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000465 PyObject *self; /* Not used */
466 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000467{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000468 return do_strip(args, LEFTSTRIP);
469}
Guido van Rossume270b431992-09-03 20:21:07 +0000470
Guido van Rossum983c9301997-12-29 19:52:29 +0000471
472static char rstrip__doc__[] =
473"rstrip(s) -> string\n\
474\n\
475Return a copy of the string s with trailing whitespace removed.";
476
Barry Warsawf5256011996-12-09 18:35:56 +0000477static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000478strop_rstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000479 PyObject *self; /* Not used */
480 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000481{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000482 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000483}
484
485
Guido van Rossum983c9301997-12-29 19:52:29 +0000486static char lower__doc__[] =
487"lower(s) -> string\n\
488\n\
489Return a copy of the string s converted to lowercase.";
490
Barry Warsawf5256011996-12-09 18:35:56 +0000491static PyObject *
Barry Warsaw04d2d151997-01-03 23:46:51 +0000492strop_lower(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000493 PyObject *self; /* Not used */
494 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000495{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000496 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000497 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000498 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000499 int changed;
500
Barry Warsawf5256011996-12-09 18:35:56 +0000501 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000502 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000503 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000504 if (new == NULL)
505 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000506 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000507 changed = 0;
508 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000509 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000510 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000511 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000512 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000513 } else
514 *s_new = c;
515 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000516 }
517 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000518 Py_DECREF(new);
519 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000520 return args;
521 }
522 return new;
523}
524
525
Guido van Rossum983c9301997-12-29 19:52:29 +0000526static char upper__doc__[] =
527"upper(s) -> string\n\
528\n\
529Return a copy of the string s converted to uppercase.";
530
Barry Warsawf5256011996-12-09 18:35:56 +0000531static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000532strop_upper(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000533 PyObject *self; /* Not used */
534 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000535{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000536 char *s, *s_new;
537 int i, n;
538 PyObject *new;
539 int changed;
540
541 if (!PyArg_Parse(args, "s#", &s, &n))
542 return NULL;
543 new = PyString_FromStringAndSize(NULL, n);
544 if (new == NULL)
545 return NULL;
546 s_new = PyString_AsString(new);
547 changed = 0;
548 for (i = 0; i < n; i++) {
549 int c = Py_CHARMASK(*s++);
550 if (islower(c)) {
551 changed = 1;
552 *s_new = toupper(c);
553 } else
554 *s_new = c;
555 s_new++;
556 }
557 if (!changed) {
558 Py_DECREF(new);
559 Py_INCREF(args);
560 return args;
561 }
562 return new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000563}
564
565
Guido van Rossum983c9301997-12-29 19:52:29 +0000566static char capitalize__doc__[] =
567"capitalize(s) -> string\n\
568\n\
569Return a copy of the string s with only its first character\n\
570capitalized.";
571
Barry Warsawf5256011996-12-09 18:35:56 +0000572static PyObject *
Guido van Rossum27457531996-06-12 04:24:52 +0000573strop_capitalize(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000574 PyObject *self; /* Not used */
575 PyObject *args;
Guido van Rossum27457531996-06-12 04:24:52 +0000576{
577 char *s, *s_new;
578 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000579 PyObject *new;
Guido van Rossum27457531996-06-12 04:24:52 +0000580 int changed;
581
Barry Warsawf5256011996-12-09 18:35:56 +0000582 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000583 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000584 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum27457531996-06-12 04:24:52 +0000585 if (new == NULL)
586 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000587 s_new = PyString_AsString(new);
Guido van Rossum27457531996-06-12 04:24:52 +0000588 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000589 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000590 int c = Py_CHARMASK(*s++);
591 if (islower(c)) {
592 changed = 1;
593 *s_new = toupper(c);
594 } else
595 *s_new = c;
596 s_new++;
597 }
598 for (i = 1; i < n; i++) {
599 int c = Py_CHARMASK(*s++);
600 if (isupper(c)) {
601 changed = 1;
602 *s_new = tolower(c);
603 } else
604 *s_new = c;
605 s_new++;
606 }
607 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000608 Py_DECREF(new);
609 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000610 return args;
611 }
612 return new;
613}
614
615
Guido van Rossum983c9301997-12-29 19:52:29 +0000616static char swapcase__doc__[] =
617"swapcase(s) -> string\n\
618\n\
619Return a copy of the string s with upper case characters\n\
620converted to lowercase and vice versa.";
621
Barry Warsawf5256011996-12-09 18:35:56 +0000622static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000623strop_swapcase(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000624 PyObject *self; /* Not used */
625 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000626{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000627 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000628 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000629 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000630 int changed;
631
Barry Warsawf5256011996-12-09 18:35:56 +0000632 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000633 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000634 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000635 if (new == NULL)
636 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000637 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000638 changed = 0;
639 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000640 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000641 if (islower(c)) {
642 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000643 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000644 }
645 else if (isupper(c)) {
646 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000647 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000648 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000649 else
650 *s_new = c;
651 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000652 }
653 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000654 Py_DECREF(new);
655 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000656 return args;
657 }
658 return new;
659}
660
661
Guido van Rossum983c9301997-12-29 19:52:29 +0000662static char atoi__doc__[] =
663"atoi(s [,base]) -> int\n\
664\n\
665Return the integer represented by the string s in the given\n\
666base, which defaults to 10. The string s must consist of one\n\
667or more digits, possibly preceded by a sign. If base is 0, it\n\
668is chosen from the leading characters of s, 0 for octal, 0x or\n\
6690X for hexadecimal. If base is 16, a preceding 0x or 0X is\n\
670accepted.";
671
Barry Warsawf5256011996-12-09 18:35:56 +0000672static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000673strop_atoi(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000674 PyObject *self; /* Not used */
675 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000676{
Barry Warsawf5256011996-12-09 18:35:56 +0000677 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
678 extern unsigned long
679 PyOS_strtoul Py_PROTO((const char *, char **, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000680 char *s, *end;
681 int base = 10;
682 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000683 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000684
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000685 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000686 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000687
688 if ((base != 0 && base < 2) || base > 36) {
689 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
690 return NULL;
691 }
692
Guido van Rossumc35f9331996-09-11 23:30:42 +0000693 while (*s && isspace(Py_CHARMASK(*s)))
694 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000695 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000696 PyErr_SetString(PyExc_ValueError, "empty string for atoi()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000697 return NULL;
698 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699 errno = 0;
700 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000701 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702 else
Barry Warsawf5256011996-12-09 18:35:56 +0000703 x = PyOS_strtol(s, &end, base);
Guido van Rossumc35f9331996-09-11 23:30:42 +0000704 while (*end && isspace(Py_CHARMASK(*end)))
705 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000707 sprintf(buffer, "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000708 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000709 return NULL;
710 }
711 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000712 sprintf(buffer, "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000713 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000714 return NULL;
715 }
Barry Warsawf5256011996-12-09 18:35:56 +0000716 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000717}
718
719
Guido van Rossum983c9301997-12-29 19:52:29 +0000720static char atol__doc__[] =
721"atol(s [,base]) -> long\n\
722\n\
723Return the long integer represented by the string s in the\n\
724given base, which defaults to 10. The string s must consist\n\
725of one or more digits, possibly preceded by a sign. If base\n\
726is 0, it is chosen from the leading characters of s, 0 for\n\
727octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n\
7280x or 0X is accepted. A trailing L or l is not accepted,\n\
729unless base is 0.";
730
Barry Warsawf5256011996-12-09 18:35:56 +0000731static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000732strop_atol(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000733 PyObject *self; /* Not used */
734 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735{
736 char *s, *end;
737 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000738 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000739 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000741 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000743
744 if ((base != 0 && base < 2) || base > 36) {
745 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
746 return NULL;
747 }
748
Guido van Rossumc35f9331996-09-11 23:30:42 +0000749 while (*s && isspace(Py_CHARMASK(*s)))
750 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000751 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000752 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000753 return NULL;
754 }
Barry Warsawf5256011996-12-09 18:35:56 +0000755 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000756 if (x == NULL)
757 return NULL;
758 if (base == 0 && (*end == 'l' || *end == 'L'))
759 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000760 while (*end && isspace(Py_CHARMASK(*end)))
761 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000763 sprintf(buffer, "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000764 PyErr_SetString(PyExc_ValueError, buffer);
765 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000766 return NULL;
767 }
768 return x;
769}
770
771
Guido van Rossum983c9301997-12-29 19:52:29 +0000772static char atof__doc__[] =
773"atof(s) -> float\n\
774\n\
775Return the floating point number represented by the string s.";
776
Barry Warsawf5256011996-12-09 18:35:56 +0000777static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778strop_atof(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000779 PyObject *self; /* Not used */
780 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000781{
Barry Warsawf5256011996-12-09 18:35:56 +0000782 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 char *s, *end;
784 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000785 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786
Barry Warsawf5256011996-12-09 18:35:56 +0000787 if (!PyArg_Parse(args, "s", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000789 while (*s && isspace(Py_CHARMASK(*s)))
790 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000791 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000792 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000793 return NULL;
794 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000795 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000796 PyFPE_START_PROTECT("strop_atof", return 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 x = strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000798 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000799 while (*end && isspace(Py_CHARMASK(*end)))
800 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000801 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000802 sprintf(buffer, "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000803 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000804 return NULL;
805 }
806 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000807 sprintf(buffer, "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000808 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000809 return NULL;
810 }
Barry Warsawf5256011996-12-09 18:35:56 +0000811 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812}
813
814
Guido van Rossum983c9301997-12-29 19:52:29 +0000815static char maketrans__doc__[] =
816"maketrans(frm, to) -> string\n\
817\n\
818Return a translation table (a string of 256 bytes long)\n\
819suitable for use in string.translate. The strings frm and to\n\
820must be of the same length.";
821
Guido van Rossumed7253c1996-07-23 18:12:39 +0000822static PyObject *
823strop_maketrans(self, args)
824 PyObject *self; /* Not used */
825 PyObject *args;
826{
Guido van Rossume0548b81997-01-06 16:50:09 +0000827 unsigned char *c, *from=NULL, *to=NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000828 int i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000829 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000830
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000831 if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen))
832 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000833
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000834 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000835 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000836 "maketrans arguments must have same length");
837 return NULL;
838 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000839
840 result = PyString_FromStringAndSize((char *)NULL, 256);
841 if (result == NULL)
842 return NULL;
843 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000844 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000845 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000846 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000847 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000848
Guido van Rossume0548b81997-01-06 16:50:09 +0000849 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000850}
851
852
Guido van Rossum983c9301997-12-29 19:52:29 +0000853static char translate__doc__[] =
854"translate(s,table [,deletechars]) -> string\n\
855\n\
856Return a copy of the string s, where all characters occurring\n\
857in the optional argument deletechars are removed, and the\n\
858remaining characters have been mapped through the given\n\
859translation table, which must be a string of length 256.";
860
Barry Warsawf5256011996-12-09 18:35:56 +0000861static PyObject *
Guido van Rossuma3127e81995-09-13 17:39:06 +0000862strop_translate(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000863 PyObject *self;
864 PyObject *args;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000865{
Guido van Rossume0548b81997-01-06 16:50:09 +0000866 register char *input, *table, *output;
867 register int i, c, changed = 0;
868 PyObject *input_obj;
869 char *table1, *output_start, *del_table=NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000870 int inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000871 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000872 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000873
Guido van Rossume0548b81997-01-06 16:50:09 +0000874 if (!PyArg_ParseTuple(args, "Ss#|s#", &input_obj,
875 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000876 return NULL;
877 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000878 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000879 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000880 return NULL;
881 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000882
Guido van Rossume0548b81997-01-06 16:50:09 +0000883 table = table1;
884 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000885 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000886 if (result == NULL)
887 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000888 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000889 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000890
Guido van Rossume0548b81997-01-06 16:50:09 +0000891 if (dellen == 0) {
892 /* If no deletions are required, use faster code */
893 for (i = inlen; --i >= 0; ) {
894 c = Py_CHARMASK(*input++);
895 if (Py_CHARMASK((*output++ = table[c])) != c)
896 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000897 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000898 if (changed)
899 return result;
900 Py_DECREF(result);
901 Py_INCREF(input_obj);
902 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000903 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000904
905 for (i = 0; i < 256; i++)
906 trans_table[i] = Py_CHARMASK(table[i]);
907
Guido van Rossum983c9301997-12-29 19:52:29 +0000908 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000909 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000910
911 for (i = inlen; --i >= 0; ) {
912 c = Py_CHARMASK(*input++);
913 if (trans_table[c] != -1)
914 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
915 continue;
916 changed = 1;
917 }
918 if (!changed) {
919 Py_DECREF(result);
920 Py_INCREF(input_obj);
921 return input_obj;
922 }
923 /* Fix the size of the resulting string */
924 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
Guido van Rossum983c9301997-12-29 19:52:29 +0000925 return NULL;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000926 return result;
927}
928
929
Guido van Rossum101923b1997-04-02 06:11:18 +0000930/* What follows is used for implementing replace(). Perry Stoll. */
931
932/*
933 mymemfind
934
935 strstr replacement for arbitrary blocks of memory.
936
937 Locates the first occurance in the memory pointed to by MEM of the
938 contents of memory pointed to by PAT. Returns the index into MEM if
939 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +0000940 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +0000941*/
942static int mymemfind(mem, len, pat, pat_len)
943 char *mem;
944 int len;
945 char *pat;
946 int pat_len;
947{
948 register int ii;
949
950 /* pattern can not occur in the last pat_len-1 chars */
951 len -= pat_len;
952
953 for (ii = 0; ii <= len; ii++) {
954 if (mem[ii] == pat[0] &&
955 (pat_len == 1 ||
956 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
957 return ii;
958 }
959 }
960 return -1;
961}
962
963/*
964 mymemcnt
965
966 Return the number of distinct times PAT is found in MEM.
967 meaning mem=1111 and pat==11 returns 2.
968 mem=11111 and pat==11 also return 2.
969 */
970static int mymemcnt(mem, len, pat, pat_len)
971 char *mem;
972 int len;
973 char *pat;
974 int pat_len;
975{
976 register int offset = 0;
977 int nfound = 0;
978
979 while (len >= 0) {
980 offset = mymemfind(mem, len, pat, pat_len);
981 if (offset == -1)
982 break;
983 mem += offset + pat_len;
984 len -= offset + pat_len;
985 nfound++;
986 }
987 return nfound;
988}
989
Guido van Rossum983c9301997-12-29 19:52:29 +0000990/*
Guido van Rossum101923b1997-04-02 06:11:18 +0000991 mymemreplace
992
993 Return a string in which all occurences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +0000994 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +0000995
996 If length of PAT is less than length of STR or there are no occurences
997 of PAT in STR, then the original string is returned. Otherwise, a new
998 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +0000999
Guido van Rossum101923b1997-04-02 06:11:18 +00001000 on return, out_len is:
1001 the length of output string, or
1002 -1 if the input string is returned, or
1003 unchanged if an error occurs (no memory).
1004
1005 return value is:
1006 the new string allocated locally, or
1007 NULL if an error occurred.
1008*/
Barry Warsawf577c081997-11-29 00:10:07 +00001009static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001010 char *str;
1011 int len; /* input string */
1012 char *pat;
1013 int pat_len; /* pattern string to find */
1014 char *sub;
1015 int sub_len; /* substitution string */
Barry Warsawf577c081997-11-29 00:10:07 +00001016 int count; /* number of replacements, 0 == all */
Guido van Rossum101923b1997-04-02 06:11:18 +00001017 int *out_len;
1018
1019{
1020 char *out_s;
1021 char *new_s;
1022 int nfound, offset, new_len;
1023
1024 if (len == 0 || pat_len > len)
1025 goto return_same;
1026
1027 /* find length of output string */
1028 nfound = mymemcnt(str, len, pat, pat_len);
Barry Warsawf577c081997-11-29 00:10:07 +00001029 if (count > 0)
1030 nfound = nfound > count ? count : nfound;
Guido van Rossum101923b1997-04-02 06:11:18 +00001031 if (nfound == 0)
1032 goto return_same;
1033 new_len = len + nfound*(sub_len - pat_len);
1034
1035 new_s = (char *)malloc(new_len);
1036 if (new_s == NULL) return NULL;
1037
1038 *out_len = new_len;
1039 out_s = new_s;
1040
1041 while (len > 0) {
1042 /* find index of next instance of pattern */
1043 offset = mymemfind(str, len, pat, pat_len);
1044 /* if not found, break out of loop */
1045 if (offset == -1) break;
1046
1047 /* copy non matching part of input string */
1048 memcpy(new_s, str, offset); /* copy part of str before pat */
1049 str += offset + pat_len; /* move str past pattern */
1050 len -= offset + pat_len; /* reduce length of str remaining */
1051
1052 /* copy substitute into the output string */
1053 new_s += offset; /* move new_s to dest for sub string */
1054 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1055 new_s += sub_len; /* offset new_s past sub string */
Barry Warsawf577c081997-11-29 00:10:07 +00001056
1057 /* break when we've done count replacements */
1058 if (--count == 0) break;
Guido van Rossum101923b1997-04-02 06:11:18 +00001059 }
1060 /* copy any remaining values into output string */
1061 if (len > 0)
1062 memcpy(new_s, str, len);
1063 return out_s;
1064
1065 return_same:
1066 *out_len = -1;
1067 return str;
1068}
1069
1070
Guido van Rossum983c9301997-12-29 19:52:29 +00001071static char replace__doc__[] =
1072"replace (str, old, new[, maxsplit]) -> string\n\
1073\n\
1074Return a copy of string str with all occurrences of substring\n\
1075old replaced by new. If the optional argument maxsplit is\n\
1076given, only the first maxsplit occurrences are replaced.";
1077
1078static PyObject *
Guido van Rossum101923b1997-04-02 06:11:18 +00001079strop_replace(self, args)
1080 PyObject *self; /* Not used */
1081 PyObject *args;
1082{
1083 char *str, *pat,*sub,*new_s;
1084 int len,pat_len,sub_len,out_len;
Barry Warsawf577c081997-11-29 00:10:07 +00001085 int count = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001086 PyObject *new;
1087
Barry Warsawf577c081997-11-29 00:10:07 +00001088 if (!PyArg_ParseTuple(args, "s#s#s#|i",
1089 &str, &len, &pat, &pat_len, &sub, &sub_len,
1090 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001091 return NULL;
Barry Warsawf577c081997-11-29 00:10:07 +00001092 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001093 if (new_s == NULL) {
1094 PyErr_NoMemory();
1095 return NULL;
1096 }
1097 if (out_len == -1) {
1098 /* we're returning another reference to the input string */
1099 new = PyTuple_GetItem(args, 0);
1100 Py_XINCREF(new);
1101 }
1102 else {
1103 new = PyString_FromStringAndSize(new_s, out_len);
1104 free(new_s);
1105 }
1106 return new;
1107}
1108
1109
Guido van Rossume270b431992-09-03 20:21:07 +00001110/* List of functions defined in the module */
1111
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001112static PyMethodDef
1113strop_methods[] = {
Guido van Rossum983c9301997-12-29 19:52:29 +00001114 {"atof", strop_atof, 1, atof__doc__},
1115 {"atoi", strop_atoi, 1, atoi__doc__},
1116 {"atol", strop_atol, 1, atol__doc__},
1117 {"capitalize", strop_capitalize, 1, capitalize__doc__},
1118 {"find", strop_find, 1, find__doc__},
1119 {"join", strop_joinfields, 1, joinfields__doc__},
1120 {"joinfields", strop_joinfields, 1, joinfields__doc__},
1121 {"lstrip", strop_lstrip, 1, lstrip__doc__},
1122 {"lower", strop_lower, 1, lower__doc__},
1123 {"maketrans", strop_maketrans, 1, maketrans__doc__},
1124 {"replace", strop_replace, 1, replace__doc__},
1125 {"rfind", strop_rfind, 1, rfind__doc__},
1126 {"rstrip", strop_rstrip, 1,rstrip__doc__},
1127 {"split", strop_splitfields, 1, splitfields__doc__},
1128 {"splitfields", strop_splitfields, 1, splitfields__doc__},
1129 {"strip", strop_strip, 1, strip__doc__},
1130 {"swapcase", strop_swapcase, 1, swapcase__doc__},
1131 {"translate", strop_translate, 1, translate__doc__},
1132 {"upper", strop_upper, 1, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001133 {NULL, NULL} /* sentinel */
1134};
1135
1136
Guido van Rossume270b431992-09-03 20:21:07 +00001137void
1138initstrop()
1139{
Barry Warsawf5256011996-12-09 18:35:56 +00001140 PyObject *m, *d, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001141 char buf[256];
1142 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001143 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1144 (PyObject*)NULL, PYTHON_API_VERSION);
Barry Warsawf5256011996-12-09 18:35:56 +00001145 d = PyModule_GetDict(m);
Guido van Rossume22e6441993-07-09 10:51:31 +00001146
1147 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001148 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001149 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001150 if (isspace(c))
1151 buf[n++] = c;
1152 }
Barry Warsawf5256011996-12-09 18:35:56 +00001153 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001154 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001155 PyDict_SetItemString(d, "whitespace", s);
1156 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001157 }
1158 /* Create 'lowercase' object */
1159 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001160 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001161 if (islower(c))
1162 buf[n++] = c;
1163 }
Barry Warsawf5256011996-12-09 18:35:56 +00001164 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001165 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001166 PyDict_SetItemString(d, "lowercase", s);
1167 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001168 }
1169
1170 /* Create 'uppercase' object */
1171 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001172 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001173 if (isupper(c))
1174 buf[n++] = c;
1175 }
Barry Warsawf5256011996-12-09 18:35:56 +00001176 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001177 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001178 PyDict_SetItemString(d, "uppercase", s);
1179 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001180 }
1181
Barry Warsawf5256011996-12-09 18:35:56 +00001182 if (PyErr_Occurred())
1183 Py_FatalError("can't initialize module strop");
Guido van Rossume270b431992-09-03 20:21:07 +00001184}