blob: 0f1ef957affa2c42171344f1a3286e7f00e9cd23 [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__[] =
35"Common string manipulations, optimized for speed\n\
36Always use \"import string\" rather than referencing\n\
37this module directly";
38
Barry Warsawf5256011996-12-09 18:35:56 +000039#include "Python.h"
Guido van Rossume270b431992-09-03 20:21:07 +000040
Guido van Rossum7b7c5781997-03-14 04:13:56 +000041#ifdef HAVE_LIMITS_H
42#include <limits.h>
43#else
44#define INT_MAX 2147483647
45#endif
46
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000047#include <ctype.h>
Guido van Rossume22e6441993-07-09 10:51:31 +000048/* XXX This file assumes that the <ctype.h> is*() functions
49 XXX are defined for all 8-bit characters! */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000050
Guido van Rossum7999a5c1996-08-08 19:16:15 +000051/* The lstrip(), rstrip() and strip() functions are implemented
52 in do_strip(), which uses an additional parameter to indicate what
53 type of strip should occur. */
54
55#define LEFTSTRIP 0
56#define RIGHTSTRIP 1
57#define BOTHSTRIP 2
58
Guido van Rossume270b431992-09-03 20:21:07 +000059
Barry Warsawf5256011996-12-09 18:35:56 +000060static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +000061split_whitespace(s, len, maxsplit)
Guido van Rossume270b431992-09-03 20:21:07 +000062 char *s;
Guido van Rossum009e79b1995-05-03 17:40:23 +000063 int len;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000064 int maxsplit;
Guido van Rossum009e79b1995-05-03 17:40:23 +000065{
Barry Warsawe8fc29c1997-01-03 22:45:34 +000066 int i = 0, j, err;
67 int countsplit = 0;
68 PyObject* item;
69 PyObject *list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +000070
Guido van Rossume270b431992-09-03 20:21:07 +000071 if (list == NULL)
72 return NULL;
73
Guido van Rossume270b431992-09-03 20:21:07 +000074 while (i < len) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +000075 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000076 i = i+1;
77 }
78 j = i;
Guido van Rossumee1813d1995-02-14 00:58:59 +000079 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000080 i = i+1;
81 }
82 if (j < i) {
Barry Warsawf5256011996-12-09 18:35:56 +000083 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Barry Warsawe8fc29c1997-01-03 22:45:34 +000084 if (item == NULL)
85 goto finally;
86
Barry Warsawf5256011996-12-09 18:35:56 +000087 err = PyList_Append(list, item);
88 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000089 if (err < 0)
90 goto finally;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000091
92 countsplit++;
Barry Warsaw93be92d1997-12-02 00:29:30 +000093 while (i < len && isspace(Py_CHARMASK(s[i]))) {
94 i = i+1;
95 }
96 if (maxsplit && (countsplit >= maxsplit) && i < len) {
Barry Warsawf5256011996-12-09 18:35:56 +000097 item = PyString_FromStringAndSize(
98 s+i, (int)(len - i));
Barry Warsawe8fc29c1997-01-03 22:45:34 +000099 if (item == NULL)
100 goto finally;
101
Barry Warsawf5256011996-12-09 18:35:56 +0000102 err = PyList_Append(list, item);
103 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000104 if (err < 0)
105 goto finally;
106
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000107 i = len;
108 }
Guido van Rossume270b431992-09-03 20:21:07 +0000109 }
110 }
Guido van Rossume270b431992-09-03 20:21:07 +0000111 return list;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000112 finally:
113 Py_DECREF(list);
114 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000115}
116
117
Guido van Rossum983c9301997-12-29 19:52:29 +0000118static char splitfields__doc__[] =
119"split(str [,sep [,maxsplit]]) -> list of strings\n\
120splitfields(str [,sep [,maxsplit]]) -> list of strings\n\
121\n\
122Return a list of the words in the string s, using sep as the\n\
123delimiter string. If maxsplit is nonzero, splits into at most\n\
124maxsplit words If sep is not specified, any whitespace string\n\
125is a separator. Maxsplit defaults to 0.\n\
126\n\
127(split and splitfields are synonymous)";
128
Barry Warsawf5256011996-12-09 18:35:56 +0000129static PyObject *
Guido van Rossume270b431992-09-03 20:21:07 +0000130strop_splitfields(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000131 PyObject *self; /* Not used */
132 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000133{
Guido van Rossum572d2d91993-11-05 10:14:49 +0000134 int len, n, i, j, err;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000135 int splitcount, maxsplit;
Guido van Rossume270b431992-09-03 20:21:07 +0000136 char *s, *sub;
Barry Warsawf5256011996-12-09 18:35:56 +0000137 PyObject *list, *item;
Guido van Rossume270b431992-09-03 20:21:07 +0000138
Guido van Rossum009e79b1995-05-03 17:40:23 +0000139 sub = NULL;
140 n = 0;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000141 splitcount = 0;
142 maxsplit = 0;
Barry Warsawf5256011996-12-09 18:35:56 +0000143 if (!PyArg_ParseTuple(args, "s#|z#i", &s, &len, &sub, &n, &maxsplit))
Guido van Rossume270b431992-09-03 20:21:07 +0000144 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000145 if (sub == NULL)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000146 return split_whitespace(s, len, maxsplit);
Guido van Rossume270b431992-09-03 20:21:07 +0000147 if (n == 0) {
Barry Warsawf5256011996-12-09 18:35:56 +0000148 PyErr_SetString(PyExc_ValueError, "empty separator");
Guido van Rossume270b431992-09-03 20:21:07 +0000149 return NULL;
150 }
151
Barry Warsawf5256011996-12-09 18:35:56 +0000152 list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +0000153 if (list == NULL)
154 return NULL;
155
156 i = j = 0;
157 while (i+n <= len) {
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000158 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000159 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000160 if (item == NULL)
161 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000162 err = PyList_Append(list, item);
163 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000164 if (err < 0)
165 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000166 i = j = i + n;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000167 splitcount++;
168 if (maxsplit && (splitcount >= maxsplit))
169 break;
Guido van Rossume270b431992-09-03 20:21:07 +0000170 }
171 else
172 i++;
173 }
Barry Warsawf5256011996-12-09 18:35:56 +0000174 item = PyString_FromStringAndSize(s+j, (int)(len-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000175 if (item == NULL)
176 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000177 err = PyList_Append(list, item);
178 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000179 if (err < 0)
180 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000181
182 return list;
Guido van Rossum572d2d91993-11-05 10:14:49 +0000183
184 fail:
Barry Warsawf5256011996-12-09 18:35:56 +0000185 Py_DECREF(list);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000186 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000187}
188
189
Guido van Rossum983c9301997-12-29 19:52:29 +0000190static char joinfields__doc__[] =
191"join(list [,sep]) -> string\n\
192joinfields(list [,sep]) -> string\n\
193\n\
194Return a string composed of the words in list, with\n\
195intervening occurences of sep. The default separator is a\n\
196single space.\n\
197\n\
198(joinfields and join are synonymous)";
199
Barry Warsawf5256011996-12-09 18:35:56 +0000200static PyObject *
Guido van Rossumc89705d1992-11-26 08:54:07 +0000201strop_joinfields(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000202 PyObject *self; /* Not used */
203 PyObject *args;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000204{
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000205 PyObject *seq;
206 char *sep = NULL;
207 int seqlen, seplen = 0;
208 int i, reslen = 0, slen = 0, sz = 100;
209 PyObject *res = NULL;
210 char* p = NULL;
211 intargfunc getitemfunc;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000212
Barry Warsawf5256011996-12-09 18:35:56 +0000213 if (!PyArg_ParseTuple(args, "O|s#", &seq, &sep, &seplen))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000214 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000215 if (sep == NULL) {
216 sep = " ";
217 seplen = 1;
218 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000219
220 seqlen = PySequence_Length(seq);
221 if (seqlen < 0 && PyErr_Occurred())
222 return NULL;
223
224 if (seqlen == 1) {
225 /* Optimization if there's only one item */
226 PyObject *item = PySequence_GetItem(seq, 0);
227 if (item && !PyString_Check(item))
228 PyErr_SetString(PyExc_TypeError,
229 "first argument must be sequence of strings");
230 return item;
231 }
232
233 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
234 return NULL;
235 p = PyString_AsString(res);
236
237 /* optimize for lists, since it's the most common case. all others
238 * (tuples and arbitrary sequences) just use the sequence abstract
239 * interface.
Barry Warsaw04d2d151997-01-03 23:46:51 +0000240 */
241 if (PyList_Check(seq)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000242 for (i = 0; i < seqlen; i++) {
243 PyObject *item = PyList_GET_ITEM(seq, i);
244 if (!PyString_Check(item)) {
245 PyErr_SetString(PyExc_TypeError,
246 "first argument must be sequence of strings");
247 Py_DECREF(res);
248 return NULL;
249 }
250 slen = PyString_GET_SIZE(item);
251 while (reslen + slen + seplen >= sz) {
252 if (_PyString_Resize(&res, sz * 2)) {
253 Py_DECREF(res);
254 return NULL;
255 }
256 sz *= 2;
257 p = PyString_AsString(res) + reslen;
258 }
259 if (i > 0) {
260 memcpy(p, sep, seplen);
261 p += seplen;
262 reslen += seplen;
263 }
264 memcpy(p, PyString_AS_STRING(item), slen);
265 p += slen;
266 reslen += slen;
267 }
268 if (_PyString_Resize(&res, reslen)) {
269 Py_DECREF(res);
270 res = NULL;
271 }
272 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000273 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000274 else if (!PySequence_Check(seq)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000275 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000276 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000277 return NULL;
278 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000279 /* type safe */
280 getitemfunc = seq->ob_type->tp_as_sequence->sq_item;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000281 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000282 PyObject *item = getitemfunc(seq, i);
283 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000284 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000285 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000286 Py_DECREF(res);
287 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000288 return NULL;
289 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000290 slen = PyString_GET_SIZE(item);
291 while (reslen + slen + seplen >= sz) {
292 if (_PyString_Resize(&res, sz * 2)) {
293 Py_DECREF(res);
294 Py_DECREF(item);
295 return NULL;
296 }
297 sz *= 2;
298 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000299 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000300 if (i > 0) {
301 memcpy(p, sep, seplen);
302 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000303 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000304 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000305 memcpy(p, PyString_AS_STRING(item), slen);
306 p += slen;
307 reslen += slen;
308 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000309 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000310 if (_PyString_Resize(&res, reslen)) {
311 Py_DECREF(res);
312 res = NULL;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000313 }
314 return res;
315}
316
Guido van Rossum983c9301997-12-29 19:52:29 +0000317
318static char find__doc__[] =
319"find(s, sub [,start [,end]]) -> in\n\
320\n\
321Return the lowest index in s where substring sub is found,\n\
322such that sub is contained within s[start,end]. Optional\n\
323arguments start and end are interpreted as in slice notation.\n\
324\n\
325Return -1 on failure.";
326
Barry Warsawf5256011996-12-09 18:35:56 +0000327static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000328strop_find(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000329 PyObject *self; /* Not used */
330 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000331{
332 char *s, *sub;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000333 int len, n, i = 0, last = INT_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000334
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000335 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000336 return NULL;
337
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000338 if (last > len)
339 last = len;
340 if (last < 0)
341 last += len;
342 if (last < 0)
343 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000344 if (i < 0)
345 i += len;
346 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000347 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000348
349 if (n == 0)
Barry Warsawf5256011996-12-09 18:35:56 +0000350 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000351
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000352 last -= n;
353 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000354 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000355 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000356 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000357
Barry Warsawf5256011996-12-09 18:35:56 +0000358 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000359}
360
361
Guido van Rossum983c9301997-12-29 19:52:29 +0000362static char rfind__doc__[] =
363"rfind(s, sub [,start [,end]]) -> int\n\
364\n\
365Return the highest index in s where substring sub is found,\n\
366such that sub is contained within s[start,end]. Optional\n\
367arguments start and end are interpreted as in slice notation.\n\
368\n\
369Return -1 on failure.";
370
Barry Warsawf5256011996-12-09 18:35:56 +0000371static PyObject *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000372strop_rfind(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000373 PyObject *self; /* Not used */
374 PyObject *args;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000375{
376 char *s, *sub;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000377 int len, n, j;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000378 int i = 0, last = INT_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000379
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000380 if (!PyArg_ParseTuple(args, "s#s#|ii", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000381 return NULL;
382
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000383 if (last > len)
384 last = len;
385 if (last < 0)
386 last += len;
387 if (last < 0)
388 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000389 if (i < 0)
390 i += len;
391 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000392 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000393
394 if (n == 0)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000395 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000396
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000397 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000398 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000399 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000400 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000401
Barry Warsawf5256011996-12-09 18:35:56 +0000402 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000403}
404
Guido van Rossum983c9301997-12-29 19:52:29 +0000405
Barry Warsawf5256011996-12-09 18:35:56 +0000406static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000407do_strip(args, striptype)
Barry Warsawf5256011996-12-09 18:35:56 +0000408 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000409 int striptype;
410{
411 char *s;
412 int len, i, j;
413
414
Barry Warsawf5256011996-12-09 18:35:56 +0000415 if (!PyArg_Parse(args, "s#", &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000416 return NULL;
417
418 i = 0;
419 if (striptype != RIGHTSTRIP) {
420 while (i < len && isspace(Py_CHARMASK(s[i]))) {
421 i++;
422 }
423 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000424
425 j = len;
426 if (striptype != LEFTSTRIP) {
427 do {
428 j--;
429 } while (j >= i && isspace(Py_CHARMASK(s[j])));
430 j++;
431 }
432
433 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000434 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000435 return args;
436 }
437 else
Barry Warsawf5256011996-12-09 18:35:56 +0000438 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000439}
440
Guido van Rossume270b431992-09-03 20:21:07 +0000441
Guido van Rossum983c9301997-12-29 19:52:29 +0000442static char strip__doc__[] =
443"strip(s) -> string\n\
444\n\
445Return a copy of the string s with leading and trailing\n\
446whitespace removed.";
447
Barry Warsawf5256011996-12-09 18:35:56 +0000448static PyObject *
Guido van Rossume270b431992-09-03 20:21:07 +0000449strop_strip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000450 PyObject *self; /* Not used */
451 PyObject *args;
Guido van Rossume270b431992-09-03 20:21:07 +0000452{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000453 return do_strip(args, BOTHSTRIP);
454}
Guido van Rossume270b431992-09-03 20:21:07 +0000455
Guido van Rossum983c9301997-12-29 19:52:29 +0000456
457static char lstrip__doc__[] =
458"lstrip(s) -> string\n\
459\n\
460Return a copy of the string s with leading whitespace removed.";
461
Barry Warsawf5256011996-12-09 18:35:56 +0000462static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000463strop_lstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000464 PyObject *self; /* Not used */
465 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000466{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000467 return do_strip(args, LEFTSTRIP);
468}
Guido van Rossume270b431992-09-03 20:21:07 +0000469
Guido van Rossum983c9301997-12-29 19:52:29 +0000470
471static char rstrip__doc__[] =
472"rstrip(s) -> string\n\
473\n\
474Return a copy of the string s with trailing whitespace removed.";
475
Barry Warsawf5256011996-12-09 18:35:56 +0000476static PyObject *
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000477strop_rstrip(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000478 PyObject *self; /* Not used */
479 PyObject *args;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000480{
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000481 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000482}
483
484
Guido van Rossum983c9301997-12-29 19:52:29 +0000485static char lower__doc__[] =
486"lower(s) -> string\n\
487\n\
488Return a copy of the string s converted to lowercase.";
489
Barry Warsawf5256011996-12-09 18:35:56 +0000490static PyObject *
Barry Warsaw04d2d151997-01-03 23:46:51 +0000491strop_lower(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000492 PyObject *self; /* Not used */
493 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000494{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000495 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000496 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000497 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000498 int changed;
499
Barry Warsawf5256011996-12-09 18:35:56 +0000500 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000501 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000502 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000503 if (new == NULL)
504 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000505 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000506 changed = 0;
507 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000508 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000509 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000510 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000511 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000512 } else
513 *s_new = c;
514 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000515 }
516 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000517 Py_DECREF(new);
518 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000519 return args;
520 }
521 return new;
522}
523
524
Guido van Rossum983c9301997-12-29 19:52:29 +0000525static char upper__doc__[] =
526"upper(s) -> string\n\
527\n\
528Return a copy of the string s converted to uppercase.";
529
Barry Warsawf5256011996-12-09 18:35:56 +0000530static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000531strop_upper(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000532 PyObject *self; /* Not used */
533 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000534{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000535 char *s, *s_new;
536 int i, n;
537 PyObject *new;
538 int changed;
539
540 if (!PyArg_Parse(args, "s#", &s, &n))
541 return NULL;
542 new = PyString_FromStringAndSize(NULL, n);
543 if (new == NULL)
544 return NULL;
545 s_new = PyString_AsString(new);
546 changed = 0;
547 for (i = 0; i < n; i++) {
548 int c = Py_CHARMASK(*s++);
549 if (islower(c)) {
550 changed = 1;
551 *s_new = toupper(c);
552 } else
553 *s_new = c;
554 s_new++;
555 }
556 if (!changed) {
557 Py_DECREF(new);
558 Py_INCREF(args);
559 return args;
560 }
561 return new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000562}
563
564
Guido van Rossum983c9301997-12-29 19:52:29 +0000565static char capitalize__doc__[] =
566"capitalize(s) -> string\n\
567\n\
568Return a copy of the string s with only its first character\n\
569capitalized.";
570
Barry Warsawf5256011996-12-09 18:35:56 +0000571static PyObject *
Guido van Rossum27457531996-06-12 04:24:52 +0000572strop_capitalize(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000573 PyObject *self; /* Not used */
574 PyObject *args;
Guido van Rossum27457531996-06-12 04:24:52 +0000575{
576 char *s, *s_new;
577 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000578 PyObject *new;
Guido van Rossum27457531996-06-12 04:24:52 +0000579 int changed;
580
Barry Warsawf5256011996-12-09 18:35:56 +0000581 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000582 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000583 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum27457531996-06-12 04:24:52 +0000584 if (new == NULL)
585 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000586 s_new = PyString_AsString(new);
Guido van Rossum27457531996-06-12 04:24:52 +0000587 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000588 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000589 int c = Py_CHARMASK(*s++);
590 if (islower(c)) {
591 changed = 1;
592 *s_new = toupper(c);
593 } else
594 *s_new = c;
595 s_new++;
596 }
597 for (i = 1; i < n; i++) {
598 int c = Py_CHARMASK(*s++);
599 if (isupper(c)) {
600 changed = 1;
601 *s_new = tolower(c);
602 } else
603 *s_new = c;
604 s_new++;
605 }
606 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000607 Py_DECREF(new);
608 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000609 return args;
610 }
611 return new;
612}
613
614
Guido van Rossum983c9301997-12-29 19:52:29 +0000615static char swapcase__doc__[] =
616"swapcase(s) -> string\n\
617\n\
618Return a copy of the string s with upper case characters\n\
619converted to lowercase and vice versa.";
620
Barry Warsawf5256011996-12-09 18:35:56 +0000621static PyObject *
Guido van Rossum5c850621992-09-11 23:55:51 +0000622strop_swapcase(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000623 PyObject *self; /* Not used */
624 PyObject *args;
Guido van Rossum5c850621992-09-11 23:55:51 +0000625{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000626 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000627 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000628 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000629 int changed;
630
Barry Warsawf5256011996-12-09 18:35:56 +0000631 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000632 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000633 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000634 if (new == NULL)
635 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000636 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000637 changed = 0;
638 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000639 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000640 if (islower(c)) {
641 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000642 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000643 }
644 else if (isupper(c)) {
645 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000646 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000647 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000648 else
649 *s_new = c;
650 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000651 }
652 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000653 Py_DECREF(new);
654 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000655 return args;
656 }
657 return new;
658}
659
660
Guido van Rossum983c9301997-12-29 19:52:29 +0000661static char atoi__doc__[] =
662"atoi(s [,base]) -> int\n\
663\n\
664Return the integer represented by the string s in the given\n\
665base, which defaults to 10. The string s must consist of one\n\
666or more digits, possibly preceded by a sign. If base is 0, it\n\
667is chosen from the leading characters of s, 0 for octal, 0x or\n\
6680X for hexadecimal. If base is 16, a preceding 0x or 0X is\n\
669accepted.";
670
Barry Warsawf5256011996-12-09 18:35:56 +0000671static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000672strop_atoi(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000673 PyObject *self; /* Not used */
674 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000675{
Barry Warsawf5256011996-12-09 18:35:56 +0000676 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
677 extern unsigned long
678 PyOS_strtoul Py_PROTO((const char *, char **, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679 char *s, *end;
680 int base = 10;
681 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000682 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000683
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000684 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000685 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000686
687 if ((base != 0 && base < 2) || base > 36) {
688 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
689 return NULL;
690 }
691
Guido van Rossumc35f9331996-09-11 23:30:42 +0000692 while (*s && isspace(Py_CHARMASK(*s)))
693 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000694 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000695 PyErr_SetString(PyExc_ValueError, "empty string for atoi()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000696 return NULL;
697 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698 errno = 0;
699 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000700 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000701 else
Barry Warsawf5256011996-12-09 18:35:56 +0000702 x = PyOS_strtol(s, &end, base);
Guido van Rossumc35f9331996-09-11 23:30:42 +0000703 while (*end && isspace(Py_CHARMASK(*end)))
704 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000705 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000706 sprintf(buffer, "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000707 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708 return NULL;
709 }
710 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000711 sprintf(buffer, "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000712 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000713 return NULL;
714 }
Barry Warsawf5256011996-12-09 18:35:56 +0000715 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716}
717
718
Guido van Rossum983c9301997-12-29 19:52:29 +0000719static char atol__doc__[] =
720"atol(s [,base]) -> long\n\
721\n\
722Return the long integer represented by the string s in the\n\
723given base, which defaults to 10. The string s must consist\n\
724of one or more digits, possibly preceded by a sign. If base\n\
725is 0, it is chosen from the leading characters of s, 0 for\n\
726octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n\
7270x or 0X is accepted. A trailing L or l is not accepted,\n\
728unless base is 0.";
729
Barry Warsawf5256011996-12-09 18:35:56 +0000730static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731strop_atol(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000732 PyObject *self; /* Not used */
733 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734{
735 char *s, *end;
736 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000737 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000738 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000740 if (!PyArg_ParseTuple(args, "s|i", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000742
743 if ((base != 0 && base < 2) || base > 36) {
744 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
745 return NULL;
746 }
747
Guido van Rossumc35f9331996-09-11 23:30:42 +0000748 while (*s && isspace(Py_CHARMASK(*s)))
749 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000750 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000751 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000752 return NULL;
753 }
Barry Warsawf5256011996-12-09 18:35:56 +0000754 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000755 if (x == NULL)
756 return NULL;
757 if (base == 0 && (*end == 'l' || *end == 'L'))
758 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000759 while (*end && isspace(Py_CHARMASK(*end)))
760 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000762 sprintf(buffer, "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000763 PyErr_SetString(PyExc_ValueError, buffer);
764 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765 return NULL;
766 }
767 return x;
768}
769
770
Guido van Rossum983c9301997-12-29 19:52:29 +0000771static char atof__doc__[] =
772"atof(s) -> float\n\
773\n\
774Return the floating point number represented by the string s.";
775
Barry Warsawf5256011996-12-09 18:35:56 +0000776static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777strop_atof(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000778 PyObject *self; /* Not used */
779 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000780{
Barry Warsawf5256011996-12-09 18:35:56 +0000781 extern double strtod Py_PROTO((const char *, char **));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000782 char *s, *end;
783 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000784 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000785
Barry Warsawf5256011996-12-09 18:35:56 +0000786 if (!PyArg_Parse(args, "s", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000788 while (*s && isspace(Py_CHARMASK(*s)))
789 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000790 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000791 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000792 return NULL;
793 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000795 PyFPE_START_PROTECT("strop_atof", return 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 x = strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000797 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000798 while (*end && isspace(Py_CHARMASK(*end)))
799 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800 if (*end != '\0') {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000801 sprintf(buffer, "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000802 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 return NULL;
804 }
805 else if (errno != 0) {
Guido van Rossumc35f9331996-09-11 23:30:42 +0000806 sprintf(buffer, "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000807 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 return NULL;
809 }
Barry Warsawf5256011996-12-09 18:35:56 +0000810 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000811}
812
813
Guido van Rossum983c9301997-12-29 19:52:29 +0000814static char maketrans__doc__[] =
815"maketrans(frm, to) -> string\n\
816\n\
817Return a translation table (a string of 256 bytes long)\n\
818suitable for use in string.translate. The strings frm and to\n\
819must be of the same length.";
820
Guido van Rossumed7253c1996-07-23 18:12:39 +0000821static PyObject *
822strop_maketrans(self, args)
823 PyObject *self; /* Not used */
824 PyObject *args;
825{
Guido van Rossume0548b81997-01-06 16:50:09 +0000826 unsigned char *c, *from=NULL, *to=NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000827 int i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000828 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000829
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000830 if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen, &to, &tolen))
831 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000832
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000833 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000834 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000835 "maketrans arguments must have same length");
836 return NULL;
837 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000838
839 result = PyString_FromStringAndSize((char *)NULL, 256);
840 if (result == NULL)
841 return NULL;
842 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000843 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000844 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000845 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000846 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000847
Guido van Rossume0548b81997-01-06 16:50:09 +0000848 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000849}
850
851
Guido van Rossum983c9301997-12-29 19:52:29 +0000852static char translate__doc__[] =
853"translate(s,table [,deletechars]) -> string\n\
854\n\
855Return a copy of the string s, where all characters occurring\n\
856in the optional argument deletechars are removed, and the\n\
857remaining characters have been mapped through the given\n\
858translation table, which must be a string of length 256.";
859
Barry Warsawf5256011996-12-09 18:35:56 +0000860static PyObject *
Guido van Rossuma3127e81995-09-13 17:39:06 +0000861strop_translate(self, args)
Barry Warsawf5256011996-12-09 18:35:56 +0000862 PyObject *self;
863 PyObject *args;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000864{
Guido van Rossume0548b81997-01-06 16:50:09 +0000865 register char *input, *table, *output;
866 register int i, c, changed = 0;
867 PyObject *input_obj;
868 char *table1, *output_start, *del_table=NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000869 int inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000870 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000871 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000872
Guido van Rossume0548b81997-01-06 16:50:09 +0000873 if (!PyArg_ParseTuple(args, "Ss#|s#", &input_obj,
874 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000875 return NULL;
876 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000877 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000878 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000879 return NULL;
880 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000881
Guido van Rossume0548b81997-01-06 16:50:09 +0000882 table = table1;
883 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000884 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000885 if (result == NULL)
886 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000887 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000888 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000889
Guido van Rossume0548b81997-01-06 16:50:09 +0000890 if (dellen == 0) {
891 /* If no deletions are required, use faster code */
892 for (i = inlen; --i >= 0; ) {
893 c = Py_CHARMASK(*input++);
894 if (Py_CHARMASK((*output++ = table[c])) != c)
895 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000896 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000897 if (changed)
898 return result;
899 Py_DECREF(result);
900 Py_INCREF(input_obj);
901 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000902 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000903
904 for (i = 0; i < 256; i++)
905 trans_table[i] = Py_CHARMASK(table[i]);
906
Guido van Rossum983c9301997-12-29 19:52:29 +0000907 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000908 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000909
910 for (i = inlen; --i >= 0; ) {
911 c = Py_CHARMASK(*input++);
912 if (trans_table[c] != -1)
913 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
914 continue;
915 changed = 1;
916 }
917 if (!changed) {
918 Py_DECREF(result);
919 Py_INCREF(input_obj);
920 return input_obj;
921 }
922 /* Fix the size of the resulting string */
923 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
Guido van Rossum983c9301997-12-29 19:52:29 +0000924 return NULL;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000925 return result;
926}
927
928
Guido van Rossum101923b1997-04-02 06:11:18 +0000929/* What follows is used for implementing replace(). Perry Stoll. */
930
931/*
932 mymemfind
933
934 strstr replacement for arbitrary blocks of memory.
935
936 Locates the first occurance in the memory pointed to by MEM of the
937 contents of memory pointed to by PAT. Returns the index into MEM if
938 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +0000939 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +0000940*/
941static int mymemfind(mem, len, pat, pat_len)
942 char *mem;
943 int len;
944 char *pat;
945 int pat_len;
946{
947 register int ii;
948
949 /* pattern can not occur in the last pat_len-1 chars */
950 len -= pat_len;
951
952 for (ii = 0; ii <= len; ii++) {
953 if (mem[ii] == pat[0] &&
954 (pat_len == 1 ||
955 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
956 return ii;
957 }
958 }
959 return -1;
960}
961
962/*
963 mymemcnt
964
965 Return the number of distinct times PAT is found in MEM.
966 meaning mem=1111 and pat==11 returns 2.
967 mem=11111 and pat==11 also return 2.
968 */
969static int mymemcnt(mem, len, pat, pat_len)
970 char *mem;
971 int len;
972 char *pat;
973 int pat_len;
974{
975 register int offset = 0;
976 int nfound = 0;
977
978 while (len >= 0) {
979 offset = mymemfind(mem, len, pat, pat_len);
980 if (offset == -1)
981 break;
982 mem += offset + pat_len;
983 len -= offset + pat_len;
984 nfound++;
985 }
986 return nfound;
987}
988
Guido van Rossum983c9301997-12-29 19:52:29 +0000989/*
Guido van Rossum101923b1997-04-02 06:11:18 +0000990 mymemreplace
991
992 Return a string in which all occurences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +0000993 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +0000994
995 If length of PAT is less than length of STR or there are no occurences
996 of PAT in STR, then the original string is returned. Otherwise, a new
997 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +0000998
Guido van Rossum101923b1997-04-02 06:11:18 +0000999 on return, out_len is:
1000 the length of output string, or
1001 -1 if the input string is returned, or
1002 unchanged if an error occurs (no memory).
1003
1004 return value is:
1005 the new string allocated locally, or
1006 NULL if an error occurred.
1007*/
Barry Warsawf577c081997-11-29 00:10:07 +00001008static char *mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001009 char *str;
1010 int len; /* input string */
1011 char *pat;
1012 int pat_len; /* pattern string to find */
1013 char *sub;
1014 int sub_len; /* substitution string */
Barry Warsawf577c081997-11-29 00:10:07 +00001015 int count; /* number of replacements, 0 == all */
Guido van Rossum101923b1997-04-02 06:11:18 +00001016 int *out_len;
1017
1018{
1019 char *out_s;
1020 char *new_s;
1021 int nfound, offset, new_len;
1022
1023 if (len == 0 || pat_len > len)
1024 goto return_same;
1025
1026 /* find length of output string */
1027 nfound = mymemcnt(str, len, pat, pat_len);
Barry Warsawf577c081997-11-29 00:10:07 +00001028 if (count > 0)
1029 nfound = nfound > count ? count : nfound;
Guido van Rossum101923b1997-04-02 06:11:18 +00001030 if (nfound == 0)
1031 goto return_same;
1032 new_len = len + nfound*(sub_len - pat_len);
1033
1034 new_s = (char *)malloc(new_len);
1035 if (new_s == NULL) return NULL;
1036
1037 *out_len = new_len;
1038 out_s = new_s;
1039
1040 while (len > 0) {
1041 /* find index of next instance of pattern */
1042 offset = mymemfind(str, len, pat, pat_len);
1043 /* if not found, break out of loop */
1044 if (offset == -1) break;
1045
1046 /* copy non matching part of input string */
1047 memcpy(new_s, str, offset); /* copy part of str before pat */
1048 str += offset + pat_len; /* move str past pattern */
1049 len -= offset + pat_len; /* reduce length of str remaining */
1050
1051 /* copy substitute into the output string */
1052 new_s += offset; /* move new_s to dest for sub string */
1053 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1054 new_s += sub_len; /* offset new_s past sub string */
Barry Warsawf577c081997-11-29 00:10:07 +00001055
1056 /* break when we've done count replacements */
1057 if (--count == 0) break;
Guido van Rossum101923b1997-04-02 06:11:18 +00001058 }
1059 /* copy any remaining values into output string */
1060 if (len > 0)
1061 memcpy(new_s, str, len);
1062 return out_s;
1063
1064 return_same:
1065 *out_len = -1;
1066 return str;
1067}
1068
1069
Guido van Rossum983c9301997-12-29 19:52:29 +00001070static char replace__doc__[] =
1071"replace (str, old, new[, maxsplit]) -> string\n\
1072\n\
1073Return a copy of string str with all occurrences of substring\n\
1074old replaced by new. If the optional argument maxsplit is\n\
1075given, only the first maxsplit occurrences are replaced.";
1076
1077static PyObject *
Guido van Rossum101923b1997-04-02 06:11:18 +00001078strop_replace(self, args)
1079 PyObject *self; /* Not used */
1080 PyObject *args;
1081{
1082 char *str, *pat,*sub,*new_s;
1083 int len,pat_len,sub_len,out_len;
Barry Warsawf577c081997-11-29 00:10:07 +00001084 int count = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001085 PyObject *new;
1086
Barry Warsawf577c081997-11-29 00:10:07 +00001087 if (!PyArg_ParseTuple(args, "s#s#s#|i",
1088 &str, &len, &pat, &pat_len, &sub, &sub_len,
1089 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001090 return NULL;
Barry Warsawf577c081997-11-29 00:10:07 +00001091 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001092 if (new_s == NULL) {
1093 PyErr_NoMemory();
1094 return NULL;
1095 }
1096 if (out_len == -1) {
1097 /* we're returning another reference to the input string */
1098 new = PyTuple_GetItem(args, 0);
1099 Py_XINCREF(new);
1100 }
1101 else {
1102 new = PyString_FromStringAndSize(new_s, out_len);
1103 free(new_s);
1104 }
1105 return new;
1106}
1107
1108
Guido van Rossume270b431992-09-03 20:21:07 +00001109/* List of functions defined in the module */
1110
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001111static PyMethodDef
1112strop_methods[] = {
Guido van Rossum983c9301997-12-29 19:52:29 +00001113 {"atof", strop_atof, 1, atof__doc__},
1114 {"atoi", strop_atoi, 1, atoi__doc__},
1115 {"atol", strop_atol, 1, atol__doc__},
1116 {"capitalize", strop_capitalize, 1, capitalize__doc__},
1117 {"find", strop_find, 1, find__doc__},
1118 {"join", strop_joinfields, 1, joinfields__doc__},
1119 {"joinfields", strop_joinfields, 1, joinfields__doc__},
1120 {"lstrip", strop_lstrip, 1, lstrip__doc__},
1121 {"lower", strop_lower, 1, lower__doc__},
1122 {"maketrans", strop_maketrans, 1, maketrans__doc__},
1123 {"replace", strop_replace, 1, replace__doc__},
1124 {"rfind", strop_rfind, 1, rfind__doc__},
1125 {"rstrip", strop_rstrip, 1,rstrip__doc__},
1126 {"split", strop_splitfields, 1, splitfields__doc__},
1127 {"splitfields", strop_splitfields, 1, splitfields__doc__},
1128 {"strip", strop_strip, 1, strip__doc__},
1129 {"swapcase", strop_swapcase, 1, swapcase__doc__},
1130 {"translate", strop_translate, 1, translate__doc__},
1131 {"upper", strop_upper, 1, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001132 {NULL, NULL} /* sentinel */
1133};
1134
1135
Guido van Rossume270b431992-09-03 20:21:07 +00001136void
1137initstrop()
1138{
Barry Warsawf5256011996-12-09 18:35:56 +00001139 PyObject *m, *d, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001140 char buf[256];
1141 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001142 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1143 (PyObject*)NULL, PYTHON_API_VERSION);
Barry Warsawf5256011996-12-09 18:35:56 +00001144 d = PyModule_GetDict(m);
Guido van Rossume22e6441993-07-09 10:51:31 +00001145
1146 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001147 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001148 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001149 if (isspace(c))
1150 buf[n++] = c;
1151 }
Barry Warsawf5256011996-12-09 18:35:56 +00001152 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001153 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001154 PyDict_SetItemString(d, "whitespace", s);
1155 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001156 }
1157 /* Create 'lowercase' object */
1158 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001159 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001160 if (islower(c))
1161 buf[n++] = c;
1162 }
Barry Warsawf5256011996-12-09 18:35:56 +00001163 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001164 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001165 PyDict_SetItemString(d, "lowercase", s);
1166 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001167 }
1168
1169 /* Create 'uppercase' object */
1170 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001171 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001172 if (isupper(c))
1173 buf[n++] = c;
1174 }
Barry Warsawf5256011996-12-09 18:35:56 +00001175 s = PyString_FromStringAndSize(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +00001176 if (s) {
Barry Warsawf5256011996-12-09 18:35:56 +00001177 PyDict_SetItemString(d, "uppercase", s);
1178 Py_DECREF(s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001179 }
1180
Barry Warsawf5256011996-12-09 18:35:56 +00001181 if (PyErr_Occurred())
1182 Py_FatalError("can't initialize module strop");
Guido van Rossume270b431992-09-03 20:21:07 +00001183}