blob: 426959e61a4a636d9c788906eec02086fc6ac0c5 [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);
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +0000228 if (item && !PyString_Check(item)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000229 PyErr_SetString(PyExc_TypeError,
230 "first argument must be sequence of strings");
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +0000231 return NULL;
232 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000233 return item;
234 }
235
236 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
237 return NULL;
238 p = PyString_AsString(res);
239
240 /* optimize for lists, since it's the most common case. all others
241 * (tuples and arbitrary sequences) just use the sequence abstract
242 * interface.
Barry Warsaw04d2d151997-01-03 23:46:51 +0000243 */
244 if (PyList_Check(seq)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000245 for (i = 0; i < seqlen; i++) {
246 PyObject *item = PyList_GET_ITEM(seq, i);
247 if (!PyString_Check(item)) {
248 PyErr_SetString(PyExc_TypeError,
249 "first argument must be sequence of strings");
250 Py_DECREF(res);
251 return NULL;
252 }
253 slen = PyString_GET_SIZE(item);
254 while (reslen + slen + seplen >= sz) {
255 if (_PyString_Resize(&res, sz * 2)) {
256 Py_DECREF(res);
257 return NULL;
258 }
259 sz *= 2;
260 p = PyString_AsString(res) + reslen;
261 }
262 if (i > 0) {
263 memcpy(p, sep, seplen);
264 p += seplen;
265 reslen += seplen;
266 }
267 memcpy(p, PyString_AS_STRING(item), slen);
268 p += slen;
269 reslen += slen;
270 }
271 if (_PyString_Resize(&res, reslen)) {
272 Py_DECREF(res);
273 res = NULL;
274 }
275 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000276 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000277
278 if (seq->ob_type->tp_as_sequence == NULL ||
279 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
280 {
Barry Warsawf5256011996-12-09 18:35:56 +0000281 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000282 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000283 return NULL;
284 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000285 /* This is now type safe */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000286 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000287 PyObject *item = getitemfunc(seq, i);
288 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000289 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000290 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000291 Py_DECREF(res);
292 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000293 return NULL;
294 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000295 slen = PyString_GET_SIZE(item);
296 while (reslen + slen + seplen >= sz) {
297 if (_PyString_Resize(&res, sz * 2)) {
298 Py_DECREF(res);
299 Py_DECREF(item);
300 return NULL;
301 }
302 sz *= 2;
303 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000304 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000305 if (i > 0) {
306 memcpy(p, sep, seplen);
307 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000308 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000309 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000310 memcpy(p, PyString_AS_STRING(item), slen);
311 p += slen;
312 reslen += slen;
313 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000314 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000315 if (_PyString_Resize(&res, reslen)) {
316 Py_DECREF(res);
317 res = NULL;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000318 }
319 return res;
320}
321
Guido van Rossum983c9301997-12-29 19:52:29 +0000322
323static char find__doc__[] =
324"find(s, sub [,start [,end]]) -> in\n\
325\n\
326Return the lowest index in s where substring sub is found,\n\
327such that sub is contained within s[start,end]. Optional\n\
328arguments start and end are interpreted as in slice notation.\n\
329\n\
330Return -1 on failure.";
331
Barry Warsawf5256011996-12-09 18:35:56 +0000332static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000333strop_find(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000334 PyObject *self; /* Not used */
335 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000336{
337 char *s, *sub;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000338 int len, n, i = 0, last = INT_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000339
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000340 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000341 return NULL;
342
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000343 if (last > len)
344 last = len;
345 if (last < 0)
346 last += len;
347 if (last < 0)
348 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000349 if (i < 0)
350 i += len;
351 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000352 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000353
Guido van Rossum031c6311998-03-24 04:19:22 +0000354 if (n == 0 && i <= last)
Barry Warsawf5256011996-12-09 18:35:56 +0000355 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000356
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000357 last -= n;
358 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000359 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000360 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000361 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000362
Barry Warsawf5256011996-12-09 18:35:56 +0000363 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000364}
365
366
Guido van Rossum983c9301997-12-29 19:52:29 +0000367static char rfind__doc__[] =
368"rfind(s, sub [,start [,end]]) -> int\n\
369\n\
370Return the highest index in s where substring sub is found,\n\
371such that sub is contained within s[start,end]. Optional\n\
372arguments start and end are interpreted as in slice notation.\n\
373\n\
374Return -1 on failure.";
375
Barry Warsawf5256011996-12-09 18:35:56 +0000376static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000377strop_rfind(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000378 PyObject *self; /* Not used */
379 PyObject *args;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000380{
381 char *s, *sub;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000382 int len, n, j;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000383 int i = 0, last = INT_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000384
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000385 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000386 return NULL;
387
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000388 if (last > len)
389 last = len;
390 if (last < 0)
391 last += len;
392 if (last < 0)
393 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000394 if (i < 0)
395 i += len;
396 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000397 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000398
Guido van Rossum031c6311998-03-24 04:19:22 +0000399 if (n == 0 && i <= last)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000400 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000401
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000402 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000403 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000404 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000405 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000406
Barry Warsawf5256011996-12-09 18:35:56 +0000407 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000408}
409
Guido van Rossum983c9301997-12-29 19:52:29 +0000410
Barry Warsawf5256011996-12-09 18:35:56 +0000411static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000412do_strip(args, striptype)
Barry Warsawf5256011996-12-09 18:35:56 +0000413 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000414 int striptype;
415{
416 char *s;
417 int len, i, j;
418
419
Barry Warsawf5256011996-12-09 18:35:56 +0000420 if (!PyArg_Parse(args, "s#", &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000421 return NULL;
422
423 i = 0;
424 if (striptype != RIGHTSTRIP) {
425 while (i < len && isspace(Py_CHARMASK(s[i]))) {
426 i++;
427 }
428 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000429
430 j = len;
431 if (striptype != LEFTSTRIP) {
432 do {
433 j--;
434 } while (j >= i && isspace(Py_CHARMASK(s[j])));
435 j++;
436 }
437
438 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000439 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000440 return args;
441 }
442 else
Barry Warsawf5256011996-12-09 18:35:56 +0000443 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000444}
445
Guido van Rossume270b431992-09-03 20:21:07 +0000446
Guido van Rossum983c9301997-12-29 19:52:29 +0000447static char strip__doc__[] =
448"strip(s) -> string\n\
449\n\
450Return a copy of the string s with leading and trailing\n\
451whitespace removed.";
452
Barry Warsawf5256011996-12-09 18:35:56 +0000453static PyObject *
Guido van Rossume270b431992-09-03 20:21:07 +0000454strop_strip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000455 PyObject *self; /* Not used */
456 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000457{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000458 return do_strip(args, BOTHSTRIP);
459}
Guido van Rossume270b431992-09-03 20:21:07 +0000460
Guido van Rossum983c9301997-12-29 19:52:29 +0000461
462static char lstrip__doc__[] =
463"lstrip(s) -> string\n\
464\n\
465Return a copy of the string s with leading whitespace removed.";
466
Barry Warsawf5256011996-12-09 18:35:56 +0000467static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000468strop_lstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000469 PyObject *self; /* Not used */
470 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000471{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000472 return do_strip(args, LEFTSTRIP);
473}
Guido van Rossume270b431992-09-03 20:21:07 +0000474
Guido van Rossum983c9301997-12-29 19:52:29 +0000475
476static char rstrip__doc__[] =
477"rstrip(s) -> string\n\
478\n\
479Return a copy of the string s with trailing whitespace removed.";
480
Barry Warsawf5256011996-12-09 18:35:56 +0000481static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000482strop_rstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000483 PyObject *self; /* Not used */
484 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000485{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000486 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000487}
488
489
Guido van Rossum983c9301997-12-29 19:52:29 +0000490static char lower__doc__[] =
491"lower(s) -> string\n\
492\n\
493Return a copy of the string s converted to lowercase.";
494
Barry Warsawf5256011996-12-09 18:35:56 +0000495static PyObject *
Barry Warsaw04d2d151997-01-03 23:46:51 +0000496strop_lower(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000497 PyObject *self; /* Not used */
498 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000499{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000500 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000501 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000502 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000503 int changed;
504
Barry Warsawf5256011996-12-09 18:35:56 +0000505 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000506 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000507 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000508 if (new == NULL)
509 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000510 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000511 changed = 0;
512 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000513 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000514 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000515 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000516 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000517 } else
518 *s_new = c;
519 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000520 }
521 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000522 Py_DECREF(new);
523 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000524 return args;
525 }
526 return new;
527}
528
529
Guido van Rossum983c9301997-12-29 19:52:29 +0000530static char upper__doc__[] =
531"upper(s) -> string\n\
532\n\
533Return a copy of the string s converted to uppercase.";
534
Barry Warsawf5256011996-12-09 18:35:56 +0000535static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000536strop_upper(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000537 PyObject *self; /* Not used */
538 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000539{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000540 char *s, *s_new;
541 int i, n;
542 PyObject *new;
543 int changed;
544
545 if (!PyArg_Parse(args, "s#", &s, &n))
546 return NULL;
547 new = PyString_FromStringAndSize(NULL, n);
548 if (new == NULL)
549 return NULL;
550 s_new = PyString_AsString(new);
551 changed = 0;
552 for (i = 0; i < n; i++) {
553 int c = Py_CHARMASK(*s++);
554 if (islower(c)) {
555 changed = 1;
556 *s_new = toupper(c);
557 } else
558 *s_new = c;
559 s_new++;
560 }
561 if (!changed) {
562 Py_DECREF(new);
563 Py_INCREF(args);
564 return args;
565 }
566 return new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000567}
568
569
Guido van Rossum983c9301997-12-29 19:52:29 +0000570static char capitalize__doc__[] =
571"capitalize(s) -> string\n\
572\n\
573Return a copy of the string s with only its first character\n\
574capitalized.";
575
Barry Warsawf5256011996-12-09 18:35:56 +0000576static PyObject *
Guido van Rossum27457531996-06-12 04:24:52 +0000577strop_capitalize(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000578 PyObject *self; /* Not used */
579 PyObject *args;
Guido van Rossum27457531996-06-12 04:24:52 +0000580{
581 char *s, *s_new;
582 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000583 PyObject *new;
Guido van Rossum27457531996-06-12 04:24:52 +0000584 int changed;
585
Barry Warsawf5256011996-12-09 18:35:56 +0000586 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000587 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000588 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum27457531996-06-12 04:24:52 +0000589 if (new == NULL)
590 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000591 s_new = PyString_AsString(new);
Guido van Rossum27457531996-06-12 04:24:52 +0000592 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000593 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000594 int c = Py_CHARMASK(*s++);
595 if (islower(c)) {
596 changed = 1;
597 *s_new = toupper(c);
598 } else
599 *s_new = c;
600 s_new++;
601 }
602 for (i = 1; i < n; i++) {
603 int c = Py_CHARMASK(*s++);
604 if (isupper(c)) {
605 changed = 1;
606 *s_new = tolower(c);
607 } else
608 *s_new = c;
609 s_new++;
610 }
611 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000612 Py_DECREF(new);
613 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000614 return args;
615 }
616 return new;
617}
618
619
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000620static char count__doc__[] =
621"count(s, sub[, start[, end]]) -> int\n\
622\n\
623Return the number of occurrences of substring sub in string\n\
624s[start:end]. Optional arguments start and end are\n\
625interpreted as in slice notation.";
626
627static PyObject *
628strop_count(self, args)
629 PyObject *self; /* Not used */
630 PyObject *args;
631{
632 char *s, *sub;
633 int len, n, j;
634 int i = 0, last = INT_MAX;
635 int m, r;
636
637 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
638 return NULL;
639 if (last > len)
640 last = len;
641 if (last < 0)
642 last += len;
643 if (last < 0)
644 last = 0;
645 if (i < 0)
646 i += len;
647 if (i < 0)
648 i = 0;
649 m = last + 1 - n;
650 if (n == 0)
651 return PyInt_FromLong((long) (m-i));
652
653 r = 0;
654 while (i < m) {
655 if (!memcmp(s+i, sub, n)) {
656 r++;
657 i += n;
658 } else {
659 i++;
660 }
661 }
662 return PyInt_FromLong((long) r);
663}
664
665
Guido van Rossum983c9301997-12-29 19:52:29 +0000666static char swapcase__doc__[] =
667"swapcase(s) -> string\n\
668\n\
669Return a copy of the string s with upper case characters\n\
670converted to lowercase and vice versa.";
671
Barry Warsawf5256011996-12-09 18:35:56 +0000672static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000673strop_swapcase(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000674 PyObject *self; /* Not used */
675 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000676{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000677 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000678 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000679 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000680 int changed;
681
Barry Warsawf5256011996-12-09 18:35:56 +0000682 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000683 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000684 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000685 if (new == NULL)
686 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000687 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000688 changed = 0;
689 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000690 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000691 if (islower(c)) {
692 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000693 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000694 }
695 else if (isupper(c)) {
696 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000697 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000698 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000699 else
700 *s_new = c;
701 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000702 }
703 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000704 Py_DECREF(new);
705 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000706 return args;
707 }
708 return new;
709}
710
711
Guido van Rossum983c9301997-12-29 19:52:29 +0000712static char atoi__doc__[] =
713"atoi(s [,base]) -> int\n\
714\n\
715Return the integer represented by the string s in the given\n\
716base, which defaults to 10. The string s must consist of one\n\
717or more digits, possibly preceded by a sign. If base is 0, it\n\
718is chosen from the leading characters of s, 0 for octal, 0x or\n\
7190X for hexadecimal. If base is 16, a preceding 0x or 0X is\n\
720accepted.";
721
Barry Warsawf5256011996-12-09 18:35:56 +0000722static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723strop_atoi(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000724 PyObject *self; /* Not used */
725 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000726{
Barry Warsawf5256011996-12-09 18:35:56 +0000727 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
728 extern unsigned long
729 PyOS_strtoul Py_PROTO((const char *, char **, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000730 char *s, *end;
731 int base = 10;
732 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000733 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000735 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000736 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000737
738 if ((base != 0 && base < 2) || base > 36) {
739 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
740 return NULL;
741 }
742
Guido van Rossumc35f9331996-09-11 23:30:42 +0000743 while (*s && isspace(Py_CHARMASK(*s)))
744 s++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000745 errno = 0;
746 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000747 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 else
Barry Warsawf5256011996-12-09 18:35:56 +0000749 x = PyOS_strtol(s, &end, base);
Guido van Rossum923fece51998-08-04 15:04:52 +0000750 if (end == s || !isxdigit(end[-1]))
751 goto bad;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000752 while (*end && isspace(Py_CHARMASK(*end)))
753 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 if (*end != '\0') {
Guido van Rossum923fece51998-08-04 15:04:52 +0000755 bad:
Guido van Rossumc35f9331996-09-11 23:30:42 +0000756 sprintf(buffer, "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000757 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 return NULL;
759 }
760 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000761 sprintf(buffer, "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000762 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763 return NULL;
764 }
Barry Warsawf5256011996-12-09 18:35:56 +0000765 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000766}
767
768
Guido van Rossum983c9301997-12-29 19:52:29 +0000769static char atol__doc__[] =
770"atol(s [,base]) -> long\n\
771\n\
772Return the long integer represented by the string s in the\n\
773given base, which defaults to 10. The string s must consist\n\
774of one or more digits, possibly preceded by a sign. If base\n\
775is 0, it is chosen from the leading characters of s, 0 for\n\
776octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n\
7770x or 0X is accepted. A trailing L or l is not accepted,\n\
778unless base is 0.";
779
Barry Warsawf5256011996-12-09 18:35:56 +0000780static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000781strop_atol(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000782 PyObject *self; /* Not used */
783 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784{
785 char *s, *end;
786 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000787 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000788 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000789
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000790 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000791 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000792
793 if ((base != 0 && base < 2) || base > 36) {
794 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
795 return NULL;
796 }
797
Guido van Rossumc35f9331996-09-11 23:30:42 +0000798 while (*s && isspace(Py_CHARMASK(*s)))
799 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000800 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000801 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000802 return NULL;
803 }
Barry Warsawf5256011996-12-09 18:35:56 +0000804 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805 if (x == NULL)
806 return NULL;
807 if (base == 0 && (*end == 'l' || *end == 'L'))
808 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000809 while (*end && isspace(Py_CHARMASK(*end)))
810 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000811 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000812 sprintf(buffer, "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000813 PyErr_SetString(PyExc_ValueError, buffer);
814 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000815 return NULL;
816 }
817 return x;
818}
819
820
Guido van Rossum983c9301997-12-29 19:52:29 +0000821static char atof__doc__[] =
822"atof(s) -> float\n\
823\n\
824Return the floating point number represented by the string s.";
825
Barry Warsawf5256011996-12-09 18:35:56 +0000826static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827strop_atof(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000828 PyObject *self; /* Not used */
829 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830{
Barry Warsawf5256011996-12-09 18:35:56 +0000831 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832 char *s, *end;
833 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000834 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835
Guido van Rossum74608f81997-12-30 05:44:10 +0000836 if (!PyArg_ParseTuple(args, "s", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000837 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000838 while (*s && isspace(Py_CHARMASK(*s)))
839 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000840 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000841 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000842 return NULL;
843 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000845 PyFPE_START_PROTECT("strop_atof", return 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846 x = strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000847 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000848 while (*end && isspace(Py_CHARMASK(*end)))
849 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000850 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000851 sprintf(buffer, "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000852 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853 return NULL;
854 }
855 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000856 sprintf(buffer, "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000857 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858 return NULL;
859 }
Barry Warsawf5256011996-12-09 18:35:56 +0000860 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000861}
862
863
Guido van Rossum983c9301997-12-29 19:52:29 +0000864static char maketrans__doc__[] =
865"maketrans(frm, to) -> string\n\
866\n\
867Return a translation table (a string of 256 bytes long)\n\
868suitable for use in string.translate. The strings frm and to\n\
869must be of the same length.";
870
Guido van Rossumed7253c1996-07-23 18:12:39 +0000871static PyObject *
872strop_maketrans(self, args)
873 PyObject *self; /* Not used */
874 PyObject *args;
875{
Guido van Rossume0548b81997-01-06 16:50:09 +0000876 unsigned char *c, *from=NULL, *to=NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000877 int i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000878 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000879
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000880 if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen))
881 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000882
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000883 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000884 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000885 "maketrans arguments must have same length");
886 return NULL;
887 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000888
889 result = PyString_FromStringAndSize((char *)NULL, 256);
890 if (result == NULL)
891 return NULL;
892 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000893 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000894 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000895 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000896 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000897
Guido van Rossume0548b81997-01-06 16:50:09 +0000898 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000899}
900
901
Guido van Rossum983c9301997-12-29 19:52:29 +0000902static char translate__doc__[] =
903"translate(s,table [,deletechars]) -> string\n\
904\n\
905Return a copy of the string s, where all characters occurring\n\
906in the optional argument deletechars are removed, and the\n\
907remaining characters have been mapped through the given\n\
908translation table, which must be a string of length 256.";
909
Barry Warsawf5256011996-12-09 18:35:56 +0000910static PyObject *
Guido van Rossuma3127e81995-09-13 17:39:06 +0000911strop_translate(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000912 PyObject *self;
913 PyObject *args;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000914{
Guido van Rossume0548b81997-01-06 16:50:09 +0000915 register char *input, *table, *output;
916 register int i, c, changed = 0;
917 PyObject *input_obj;
918 char *table1, *output_start, *del_table=NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000919 int inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000920 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000921 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000922
Guido van Rossume0548b81997-01-06 16:50:09 +0000923 if (!PyArg_ParseTuple(args, "Ss#|s#", &input_obj,
924 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000925 return NULL;
926 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000927 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000928 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000929 return NULL;
930 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000931
Guido van Rossume0548b81997-01-06 16:50:09 +0000932 table = table1;
933 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000934 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000935 if (result == NULL)
936 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000937 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000938 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000939
Guido van Rossume0548b81997-01-06 16:50:09 +0000940 if (dellen == 0) {
941 /* If no deletions are required, use faster code */
942 for (i = inlen; --i >= 0; ) {
943 c = Py_CHARMASK(*input++);
944 if (Py_CHARMASK((*output++ = table[c])) != c)
945 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000946 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000947 if (changed)
948 return result;
949 Py_DECREF(result);
950 Py_INCREF(input_obj);
951 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000952 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000953
954 for (i = 0; i < 256; i++)
955 trans_table[i] = Py_CHARMASK(table[i]);
956
Guido van Rossum983c9301997-12-29 19:52:29 +0000957 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000958 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000959
960 for (i = inlen; --i >= 0; ) {
961 c = Py_CHARMASK(*input++);
962 if (trans_table[c] != -1)
963 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
964 continue;
965 changed = 1;
966 }
967 if (!changed) {
968 Py_DECREF(result);
969 Py_INCREF(input_obj);
970 return input_obj;
971 }
972 /* Fix the size of the resulting string */
973 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
Guido van Rossum983c9301997-12-29 19:52:29 +0000974 return NULL;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000975 return result;
976}
977
978
Guido van Rossum101923b1997-04-02 06:11:18 +0000979/* What follows is used for implementing replace(). Perry Stoll. */
980
981/*
982 mymemfind
983
984 strstr replacement for arbitrary blocks of memory.
985
986 Locates the first occurance in the memory pointed to by MEM of the
987 contents of memory pointed to by PAT. Returns the index into MEM if
988 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +0000989 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +0000990*/
991static int mymemfind(mem, len, pat, pat_len)
992 char *mem;
993 int len;
994 char *pat;
995 int pat_len;
996{
997 register int ii;
998
999 /* pattern can not occur in the last pat_len-1 chars */
1000 len -= pat_len;
1001
1002 for (ii = 0; ii <= len; ii++) {
1003 if (mem[ii] == pat[0] &&
1004 (pat_len == 1 ||
1005 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1006 return ii;
1007 }
1008 }
1009 return -1;
1010}
1011
1012/*
1013 mymemcnt
1014
1015 Return the number of distinct times PAT is found in MEM.
1016 meaning mem=1111 and pat==11 returns 2.
1017 mem=11111 and pat==11 also return 2.
1018 */
1019static int mymemcnt(mem, len, pat, pat_len)
1020 char *mem;
1021 int len;
1022 char *pat;
1023 int pat_len;
1024{
1025 register int offset = 0;
1026 int nfound = 0;
1027
1028 while (len >= 0) {
1029 offset = mymemfind(mem, len, pat, pat_len);
1030 if (offset == -1)
1031 break;
1032 mem += offset + pat_len;
1033 len -= offset + pat_len;
1034 nfound++;
1035 }
1036 return nfound;
1037}
1038
Guido van Rossum983c9301997-12-29 19:52:29 +00001039/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001040 mymemreplace
1041
1042 Return a string in which all occurences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001043 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001044
1045 If length of PAT is less than length of STR or there are no occurences
1046 of PAT in STR, then the original string is returned. Otherwise, a new
1047 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001048
Guido van Rossum101923b1997-04-02 06:11:18 +00001049 on return, out_len is:
1050 the length of output string, or
1051 -1 if the input string is returned, or
1052 unchanged if an error occurs (no memory).
1053
1054 return value is:
1055 the new string allocated locally, or
1056 NULL if an error occurred.
1057*/
Barry Warsawf577c081997-11-29 00:10:07 +00001058static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001059 char *str;
1060 int len; /* input string */
1061 char *pat;
1062 int pat_len; /* pattern string to find */
1063 char *sub;
1064 int sub_len; /* substitution string */
Barry Warsawf577c081997-11-29 00:10:07 +00001065 int count; /* number of replacements, 0 == all */
Guido van Rossum101923b1997-04-02 06:11:18 +00001066 int *out_len;
1067
1068{
1069 char *out_s;
1070 char *new_s;
1071 int nfound, offset, new_len;
1072
1073 if (len == 0 || pat_len > len)
1074 goto return_same;
1075
1076 /* find length of output string */
1077 nfound = mymemcnt(str, len, pat, pat_len);
Barry Warsawf577c081997-11-29 00:10:07 +00001078 if (count > 0)
1079 nfound = nfound > count ? count : nfound;
Guido van Rossum101923b1997-04-02 06:11:18 +00001080 if (nfound == 0)
1081 goto return_same;
1082 new_len = len + nfound*(sub_len - pat_len);
1083
1084 new_s = (char *)malloc(new_len);
1085 if (new_s == NULL) return NULL;
1086
1087 *out_len = new_len;
1088 out_s = new_s;
1089
1090 while (len > 0) {
1091 /* find index of next instance of pattern */
1092 offset = mymemfind(str, len, pat, pat_len);
1093 /* if not found, break out of loop */
1094 if (offset == -1) break;
1095
1096 /* copy non matching part of input string */
1097 memcpy(new_s, str, offset); /* copy part of str before pat */
1098 str += offset + pat_len; /* move str past pattern */
1099 len -= offset + pat_len; /* reduce length of str remaining */
1100
1101 /* copy substitute into the output string */
1102 new_s += offset; /* move new_s to dest for sub string */
1103 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1104 new_s += sub_len; /* offset new_s past sub string */
Barry Warsawf577c081997-11-29 00:10:07 +00001105
1106 /* break when we've done count replacements */
1107 if (--count == 0) break;
Guido van Rossum101923b1997-04-02 06:11:18 +00001108 }
1109 /* copy any remaining values into output string */
1110 if (len > 0)
1111 memcpy(new_s, str, len);
1112 return out_s;
1113
1114 return_same:
1115 *out_len = -1;
1116 return str;
1117}
1118
1119
Guido van Rossum983c9301997-12-29 19:52:29 +00001120static char replace__doc__[] =
1121"replace (str, old, new[, maxsplit]) -> string\n\
1122\n\
1123Return a copy of string str with all occurrences of substring\n\
1124old replaced by new. If the optional argument maxsplit is\n\
1125given, only the first maxsplit occurrences are replaced.";
1126
1127static PyObject *
Guido van Rossum101923b1997-04-02 06:11:18 +00001128strop_replace(self, args)
1129 PyObject *self; /* Not used */
1130 PyObject *args;
1131{
1132 char *str, *pat,*sub,*new_s;
1133 int len,pat_len,sub_len,out_len;
Barry Warsawf577c081997-11-29 00:10:07 +00001134 int count = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001135 PyObject *new;
1136
Barry Warsawf577c081997-11-29 00:10:07 +00001137 if (!PyArg_ParseTuple(args, "s#s#s#|i",
1138 &str, &len, &pat, &pat_len, &sub, &sub_len,
1139 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001140 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001141 if (pat_len <= 0) {
1142 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1143 return NULL;
1144 }
Barry Warsawf577c081997-11-29 00:10:07 +00001145 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001146 if (new_s == NULL) {
1147 PyErr_NoMemory();
1148 return NULL;
1149 }
1150 if (out_len == -1) {
1151 /* we're returning another reference to the input string */
1152 new = PyTuple_GetItem(args, 0);
1153 Py_XINCREF(new);
1154 }
1155 else {
1156 new = PyString_FromStringAndSize(new_s, out_len);
1157 free(new_s);
1158 }
1159 return new;
1160}
1161
1162
Guido van Rossume270b431992-09-03 20:21:07 +00001163/* List of functions defined in the module */
1164
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001165static PyMethodDef
1166strop_methods[] = {
Guido van Rossum983c9301997-12-29 19:52:29 +00001167 {"atof", strop_atof, 1, atof__doc__},
1168 {"atoi", strop_atoi, 1, atoi__doc__},
1169 {"atol", strop_atol, 1, atol__doc__},
Guido van Rossum74608f81997-12-30 05:44:10 +00001170 {"capitalize", strop_capitalize, 0, capitalize__doc__},
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +00001171 {"count", strop_count, 1, count__doc__},
Guido van Rossum983c9301997-12-29 19:52:29 +00001172 {"find", strop_find, 1, find__doc__},
1173 {"join", strop_joinfields, 1, joinfields__doc__},
1174 {"joinfields", strop_joinfields, 1, joinfields__doc__},
Guido van Rossum74608f81997-12-30 05:44:10 +00001175 {"lstrip", strop_lstrip, 0, lstrip__doc__},
1176 {"lower", strop_lower, 0, lower__doc__},
Guido van Rossum983c9301997-12-29 19:52:29 +00001177 {"maketrans", strop_maketrans, 1, maketrans__doc__},
1178 {"replace", strop_replace, 1, replace__doc__},
1179 {"rfind", strop_rfind, 1, rfind__doc__},
Guido van Rossum74608f81997-12-30 05:44:10 +00001180 {"rstrip", strop_rstrip, 0, rstrip__doc__},
Guido van Rossum983c9301997-12-29 19:52:29 +00001181 {"split", strop_splitfields, 1, splitfields__doc__},
1182 {"splitfields", strop_splitfields, 1, splitfields__doc__},
Guido van Rossum74608f81997-12-30 05:44:10 +00001183 {"strip", strop_strip, 0, strip__doc__},
1184 {"swapcase", strop_swapcase, 0, swapcase__doc__},
Guido van Rossum983c9301997-12-29 19:52:29 +00001185 {"translate", strop_translate, 1, translate__doc__},
Guido van Rossum74608f81997-12-30 05:44:10 +00001186 {"upper", strop_upper, 0, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001187 {NULL, NULL} /* sentinel */
1188};
1189
1190
Guido van Rossume270b431992-09-03 20:21:07 +00001191void
1192initstrop()
1193{
Barry Warsawf5256011996-12-09 18:35:56 +00001194 PyObject *m, *d, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001195 char buf[256];
1196 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001197 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1198 (PyObject*)NULL, PYTHON_API_VERSION);
Barry Warsawf5256011996-12-09 18:35:56 +00001199 d = PyModule_GetDict(m);
Guido van Rossume22e6441993-07-09 10:51:31 +00001200
1201 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001202 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001203 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001204 if (isspace(c))
1205 buf[n++] = c;
1206 }
Barry Warsawf5256011996-12-09 18:35:56 +00001207 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001208 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001209 PyDict_SetItemString(d, "whitespace", s);
1210 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001211 }
1212 /* Create 'lowercase' object */
1213 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001214 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001215 if (islower(c))
1216 buf[n++] = c;
1217 }
Barry Warsawf5256011996-12-09 18:35:56 +00001218 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001219 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001220 PyDict_SetItemString(d, "lowercase", s);
1221 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001222 }
1223
1224 /* Create 'uppercase' object */
1225 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001226 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001227 if (isupper(c))
1228 buf[n++] = c;
1229 }
Barry Warsawf5256011996-12-09 18:35:56 +00001230 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001231 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001232 PyDict_SetItemString(d, "uppercase", s);
1233 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001234 }
1235
Barry Warsawf5256011996-12-09 18:35:56 +00001236 if (PyErr_Occurred())
1237 Py_FatalError("can't initialize module strop");
Guido van Rossume270b431992-09-03 20:21:07 +00001238}