blob: 2d884744aa7fe78e472a964c935c0dcbd6d64ef9 [file] [log] [blame]
Guido van Rossume270b431992-09-03 20:21:07 +00001/* strop module */
2
Martin v. Löwis5c97c792006-02-17 15:49:09 +00003#define PY_SSIZE_T_CLEAN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004#include "Python.h"
5#include <ctype.h>
6
7PyDoc_STRVAR(strop_module__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00008"Common string manipulations, optimized for speed.\n"
9"\n"
10"Always use \"import string\" rather than referencing\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000011"this module directly.");
Guido van Rossum983c9301997-12-29 19:52:29 +000012
Guido van Rossume22e6441993-07-09 10:51:31 +000013/* XXX This file assumes that the <ctype.h> is*() functions
14 XXX are defined for all 8-bit characters! */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000015
Guido van Rossum2e0a6542001-05-15 02:14:44 +000016#define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \
17 "strop functions are obsolete; use string methods")) \
18 return NULL
19
Guido van Rossum7999a5c1996-08-08 19:16:15 +000020/* The lstrip(), rstrip() and strip() functions are implemented
21 in do_strip(), which uses an additional parameter to indicate what
22 type of strip should occur. */
23
24#define LEFTSTRIP 0
25#define RIGHTSTRIP 1
26#define BOTHSTRIP 2
27
Guido van Rossume270b431992-09-03 20:21:07 +000028
Barry Warsawf5256011996-12-09 18:35:56 +000029static PyObject *
Martin v. Löwis5c97c792006-02-17 15:49:09 +000030split_whitespace(char *s, Py_ssize_t len, Py_ssize_t maxsplit)
Guido van Rossum009e79b1995-05-03 17:40:23 +000031{
Martin v. Löwis5c97c792006-02-17 15:49:09 +000032 Py_ssize_t i = 0, j;
33 int err;
34 Py_ssize_t countsplit = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +000035 PyObject* item;
36 PyObject *list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +000037
Guido van Rossume270b431992-09-03 20:21:07 +000038 if (list == NULL)
39 return NULL;
40
Guido van Rossume270b431992-09-03 20:21:07 +000041 while (i < len) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +000042 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000043 i = i+1;
44 }
45 j = i;
Guido van Rossumee1813d1995-02-14 00:58:59 +000046 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000047 i = i+1;
48 }
49 if (j < i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000050 item = PyString_FromStringAndSize(s+j, i-j);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000051 if (item == NULL)
52 goto finally;
53
Barry Warsawf5256011996-12-09 18:35:56 +000054 err = PyList_Append(list, item);
55 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000056 if (err < 0)
57 goto finally;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000058
59 countsplit++;
Barry Warsaw93be92d1997-12-02 00:29:30 +000060 while (i < len && isspace(Py_CHARMASK(s[i]))) {
61 i = i+1;
62 }
63 if (maxsplit && (countsplit >= maxsplit) && i < len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000064 item = PyString_FromStringAndSize(
Martin v. Löwis5c97c792006-02-17 15:49:09 +000065 s+i, len - i);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000066 if (item == NULL)
67 goto finally;
68
Barry Warsawf5256011996-12-09 18:35:56 +000069 err = PyList_Append(list, item);
70 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000071 if (err < 0)
72 goto finally;
73
Guido van Rossum7999a5c1996-08-08 19:16:15 +000074 i = len;
75 }
Guido van Rossume270b431992-09-03 20:21:07 +000076 }
77 }
Guido van Rossume270b431992-09-03 20:21:07 +000078 return list;
Barry Warsawe8fc29c1997-01-03 22:45:34 +000079 finally:
80 Py_DECREF(list);
81 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +000082}
83
84
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085PyDoc_STRVAR(splitfields__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +000086"split(s [,sep [,maxsplit]]) -> list of strings\n"
87"splitfields(s [,sep [,maxsplit]]) -> list of strings\n"
88"\n"
89"Return a list of the words in the string s, using sep as the\n"
90"delimiter string. If maxsplit is nonzero, splits into at most\n"
91"maxsplit words. If sep is not specified, any whitespace string\n"
92"is a separator. Maxsplit defaults to 0.\n"
93"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094"(split and splitfields are synonymous)");
Guido van Rossum983c9301997-12-29 19:52:29 +000095
Barry Warsawf5256011996-12-09 18:35:56 +000096static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +000097strop_splitfields(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +000098{
Martin v. Löwis5c97c792006-02-17 15:49:09 +000099 Py_ssize_t len, n, i, j, err;
100 Py_ssize_t splitcount, maxsplit;
Guido van Rossume270b431992-09-03 20:21:07 +0000101 char *s, *sub;
Barry Warsawf5256011996-12-09 18:35:56 +0000102 PyObject *list, *item;
Guido van Rossume270b431992-09-03 20:21:07 +0000103
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000104 WARN;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000105 sub = NULL;
106 n = 0;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000107 splitcount = 0;
108 maxsplit = 0;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000109 if (!PyArg_ParseTuple(args, "t#|z#n:split", &s, &len, &sub, &n, &maxsplit))
Guido van Rossume270b431992-09-03 20:21:07 +0000110 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000111 if (sub == NULL)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000112 return split_whitespace(s, len, maxsplit);
Guido van Rossume270b431992-09-03 20:21:07 +0000113 if (n == 0) {
Barry Warsawf5256011996-12-09 18:35:56 +0000114 PyErr_SetString(PyExc_ValueError, "empty separator");
Guido van Rossume270b431992-09-03 20:21:07 +0000115 return NULL;
116 }
117
Barry Warsawf5256011996-12-09 18:35:56 +0000118 list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +0000119 if (list == NULL)
120 return NULL;
121
122 i = j = 0;
123 while (i+n <= len) {
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000124 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000125 item = PyString_FromStringAndSize(s+j, i-j);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000126 if (item == NULL)
127 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000128 err = PyList_Append(list, item);
129 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000130 if (err < 0)
131 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000132 i = j = i + n;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000133 splitcount++;
134 if (maxsplit && (splitcount >= maxsplit))
135 break;
Guido van Rossume270b431992-09-03 20:21:07 +0000136 }
137 else
138 i++;
139 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000140 item = PyString_FromStringAndSize(s+j, len-j);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000141 if (item == NULL)
142 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000143 err = PyList_Append(list, item);
144 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000145 if (err < 0)
146 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000147
148 return list;
Guido van Rossum572d2d91993-11-05 10:14:49 +0000149
150 fail:
Barry Warsawf5256011996-12-09 18:35:56 +0000151 Py_DECREF(list);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000152 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000153}
154
155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000156PyDoc_STRVAR(joinfields__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000157"join(list [,sep]) -> string\n"
158"joinfields(list [,sep]) -> string\n"
159"\n"
160"Return a string composed of the words in list, with\n"
161"intervening occurrences of sep. Sep defaults to a single\n"
162"space.\n"
163"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164"(join and joinfields are synonymous)");
Guido van Rossum983c9301997-12-29 19:52:29 +0000165
Barry Warsawf5256011996-12-09 18:35:56 +0000166static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000167strop_joinfields(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000168{
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000169 PyObject *seq;
170 char *sep = NULL;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000171 Py_ssize_t seqlen, seplen = 0;
172 Py_ssize_t i, reslen = 0, slen = 0, sz = 100;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000173 PyObject *res = NULL;
174 char* p = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000175 ssizeargfunc getitemfunc;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000176
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000177 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000178 if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000179 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000180 if (sep == NULL) {
181 sep = " ";
182 seplen = 1;
183 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000184
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000185 seqlen = PySequence_Size(seq);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000186 if (seqlen < 0 && PyErr_Occurred())
187 return NULL;
188
189 if (seqlen == 1) {
190 /* Optimization if there's only one item */
191 PyObject *item = PySequence_GetItem(seq, 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000192 if (item && !PyString_Check(item)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000193 PyErr_SetString(PyExc_TypeError,
194 "first argument must be sequence of strings");
Guido van Rossumbf338301998-10-19 13:38:36 +0000195 Py_DECREF(item);
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +0000196 return NULL;
197 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000198 return item;
199 }
200
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000201 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000202 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000203 p = PyString_AsString(res);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000204
205 /* optimize for lists, since it's the most common case. all others
206 * (tuples and arbitrary sequences) just use the sequence abstract
207 * interface.
Barry Warsaw04d2d151997-01-03 23:46:51 +0000208 */
209 if (PyList_Check(seq)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000210 for (i = 0; i < seqlen; i++) {
211 PyObject *item = PyList_GET_ITEM(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000212 if (!PyString_Check(item)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000213 PyErr_SetString(PyExc_TypeError,
214 "first argument must be sequence of strings");
215 Py_DECREF(res);
216 return NULL;
217 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000218 slen = PyString_GET_SIZE(item);
Neal Norwitze7d8be82008-07-31 17:17:14 +0000219 if (slen > PY_SSIZE_T_MAX - reslen ||
220 seplen > PY_SSIZE_T_MAX - reslen - seplen) {
221 PyErr_SetString(PyExc_OverflowError,
222 "input too long");
223 Py_DECREF(res);
224 return NULL;
225 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000226 while (reslen + slen + seplen >= sz) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000227 if (_PyString_Resize(&res, sz * 2) < 0)
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000228 return NULL;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000229 sz *= 2;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000230 p = PyString_AsString(res) + reslen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000231 }
232 if (i > 0) {
233 memcpy(p, sep, seplen);
234 p += seplen;
235 reslen += seplen;
236 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000237 memcpy(p, PyString_AS_STRING(item), slen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000238 p += slen;
239 reslen += slen;
240 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000241 _PyString_Resize(&res, reslen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000242 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000243 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000244
245 if (seq->ob_type->tp_as_sequence == NULL ||
246 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
247 {
Barry Warsawf5256011996-12-09 18:35:56 +0000248 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000249 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000250 return NULL;
251 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000252 /* This is now type safe */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000253 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000254 PyObject *item = getitemfunc(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000255 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000256 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000257 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000258 Py_DECREF(res);
259 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000260 return NULL;
261 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000262 slen = PyString_GET_SIZE(item);
Neal Norwitze7d8be82008-07-31 17:17:14 +0000263 if (slen > PY_SSIZE_T_MAX - reslen ||
264 seplen > PY_SSIZE_T_MAX - reslen - seplen) {
265 PyErr_SetString(PyExc_OverflowError,
266 "input too long");
267 Py_DECREF(res);
268 Py_XDECREF(item);
269 return NULL;
270 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000271 while (reslen + slen + seplen >= sz) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000272 if (_PyString_Resize(&res, sz * 2) < 0) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000273 Py_DECREF(item);
274 return NULL;
275 }
276 sz *= 2;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000277 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000278 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000279 if (i > 0) {
280 memcpy(p, sep, seplen);
281 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000282 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000283 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000284 memcpy(p, PyString_AS_STRING(item), slen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000285 p += slen;
286 reslen += slen;
287 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000288 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000289 _PyString_Resize(&res, reslen);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000290 return res;
291}
292
Guido van Rossum983c9301997-12-29 19:52:29 +0000293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000294PyDoc_STRVAR(find__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000295"find(s, sub [,start [,end]]) -> in\n"
296"\n"
297"Return the lowest index in s where substring sub is found,\n"
298"such that sub is contained within s[start,end]. Optional\n"
299"arguments start and end are interpreted as in slice notation.\n"
300"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000301"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000302
Barry Warsawf5256011996-12-09 18:35:56 +0000303static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000304strop_find(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000305{
306 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000307 Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000308
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000309 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000310 if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000311 return NULL;
312
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000313 if (last > len)
314 last = len;
315 if (last < 0)
316 last += len;
317 if (last < 0)
318 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000319 if (i < 0)
320 i += len;
321 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000322 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000323
Guido van Rossum031c6311998-03-24 04:19:22 +0000324 if (n == 0 && i <= last)
Barry Warsawf5256011996-12-09 18:35:56 +0000325 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000326
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000327 last -= n;
328 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000329 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000330 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000331 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000332
Barry Warsawf5256011996-12-09 18:35:56 +0000333 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000334}
335
336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(rfind__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000338"rfind(s, sub [,start [,end]]) -> int\n"
339"\n"
340"Return the highest index in s where substring sub is found,\n"
341"such that sub is contained within s[start,end]. Optional\n"
342"arguments start and end are interpreted as in slice notation.\n"
343"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000344"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000345
Barry Warsawf5256011996-12-09 18:35:56 +0000346static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000347strop_rfind(PyObject *self, PyObject *args)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000348{
349 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000350 Py_ssize_t len, n, j;
Martin v. Löwis23089152006-04-13 07:34:09 +0000351 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000352
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000353 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000354 if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000355 return NULL;
356
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000357 if (last > len)
358 last = len;
359 if (last < 0)
360 last += len;
361 if (last < 0)
362 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000363 if (i < 0)
364 i += len;
365 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000366 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000367
Guido van Rossum031c6311998-03-24 04:19:22 +0000368 if (n == 0 && i <= last)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000369 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000370
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000371 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000373 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000374 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000375
Barry Warsawf5256011996-12-09 18:35:56 +0000376 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000377}
378
Guido van Rossum983c9301997-12-29 19:52:29 +0000379
Barry Warsawf5256011996-12-09 18:35:56 +0000380static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000381do_strip(PyObject *args, int striptype)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000382{
383 char *s;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000384 Py_ssize_t len, i, j;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000385
386
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000387 if (PyString_AsStringAndSize(args, &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000388 return NULL;
389
390 i = 0;
391 if (striptype != RIGHTSTRIP) {
392 while (i < len && isspace(Py_CHARMASK(s[i]))) {
393 i++;
394 }
395 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000396
397 j = len;
398 if (striptype != LEFTSTRIP) {
399 do {
400 j--;
401 } while (j >= i && isspace(Py_CHARMASK(s[j])));
402 j++;
403 }
404
405 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000406 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000407 return args;
408 }
409 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000410 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000411}
412
Guido van Rossume270b431992-09-03 20:21:07 +0000413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(strip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000415"strip(s) -> string\n"
416"\n"
417"Return a copy of the string s with leading and trailing\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000418"whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000419
Barry Warsawf5256011996-12-09 18:35:56 +0000420static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000421strop_strip(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000422{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000423 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000424 return do_strip(args, BOTHSTRIP);
425}
Guido van Rossume270b431992-09-03 20:21:07 +0000426
Guido van Rossum983c9301997-12-29 19:52:29 +0000427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000428PyDoc_STRVAR(lstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000429"lstrip(s) -> string\n"
430"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000431"Return a copy of the string s with leading whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000432
Barry Warsawf5256011996-12-09 18:35:56 +0000433static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000434strop_lstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000435{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000436 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000437 return do_strip(args, LEFTSTRIP);
438}
Guido van Rossume270b431992-09-03 20:21:07 +0000439
Guido van Rossum983c9301997-12-29 19:52:29 +0000440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000441PyDoc_STRVAR(rstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000442"rstrip(s) -> string\n"
443"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000444"Return a copy of the string s with trailing whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000445
Barry Warsawf5256011996-12-09 18:35:56 +0000446static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000447strop_rstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000448{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000449 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000450 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000451}
452
453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454PyDoc_STRVAR(lower__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000455"lower(s) -> string\n"
456"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457"Return a copy of the string s converted to lowercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000458
Barry Warsawf5256011996-12-09 18:35:56 +0000459static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000460strop_lower(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000461{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000462 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000464 PyObject *newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000465 int changed;
466
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000467 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000468 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000469 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000470 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000471 if (newstr == NULL)
Guido van Rossum5c850621992-09-11 23:55:51 +0000472 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000473 s_new = PyString_AsString(newstr);
Guido van Rossum5c850621992-09-11 23:55:51 +0000474 changed = 0;
475 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000476 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000477 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000478 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000479 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000480 } else
481 *s_new = c;
482 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000483 }
484 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000485 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000486 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000487 return args;
488 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000489 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000490}
491
492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000493PyDoc_STRVAR(upper__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000494"upper(s) -> string\n"
495"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496"Return a copy of the string s converted to uppercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000497
Barry Warsawf5256011996-12-09 18:35:56 +0000498static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000499strop_upper(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000500{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000501 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000502 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000503 PyObject *newstr;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000504 int changed;
505
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000506 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000507 if (PyString_AsStringAndSize(args, &s, &n))
Barry Warsaw04d2d151997-01-03 23:46:51 +0000508 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000509 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000510 if (newstr == NULL)
Barry Warsaw04d2d151997-01-03 23:46:51 +0000511 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000512 s_new = PyString_AsString(newstr);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000513 changed = 0;
514 for (i = 0; i < n; i++) {
515 int c = Py_CHARMASK(*s++);
516 if (islower(c)) {
517 changed = 1;
518 *s_new = toupper(c);
519 } else
520 *s_new = c;
521 s_new++;
522 }
523 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000524 Py_DECREF(newstr);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000525 Py_INCREF(args);
526 return args;
527 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000528 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000529}
530
531
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000532PyDoc_STRVAR(capitalize__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000533"capitalize(s) -> string\n"
534"\n"
535"Return a copy of the string s with only its first character\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000536"capitalized.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000537
Barry Warsawf5256011996-12-09 18:35:56 +0000538static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000539strop_capitalize(PyObject *self, PyObject *args)
Guido van Rossum27457531996-06-12 04:24:52 +0000540{
541 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000542 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000543 PyObject *newstr;
Guido van Rossum27457531996-06-12 04:24:52 +0000544 int changed;
545
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000546 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000547 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000548 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000549 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000550 if (newstr == NULL)
Guido van Rossum27457531996-06-12 04:24:52 +0000551 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000552 s_new = PyString_AsString(newstr);
Guido van Rossum27457531996-06-12 04:24:52 +0000553 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000554 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000555 int c = Py_CHARMASK(*s++);
556 if (islower(c)) {
557 changed = 1;
558 *s_new = toupper(c);
559 } else
560 *s_new = c;
561 s_new++;
562 }
563 for (i = 1; i < n; i++) {
564 int c = Py_CHARMASK(*s++);
565 if (isupper(c)) {
566 changed = 1;
567 *s_new = tolower(c);
568 } else
569 *s_new = c;
570 s_new++;
571 }
572 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000573 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000574 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000575 return args;
576 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000577 return newstr;
Guido van Rossum27457531996-06-12 04:24:52 +0000578}
579
580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000581PyDoc_STRVAR(expandtabs__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000582"expandtabs(string, [tabsize]) -> string\n"
583"\n"
584"Expand tabs in a string, i.e. replace them by one or more spaces,\n"
585"depending on the current column and the given tab size (default 8).\n"
586"The column number is reset to zero after each newline occurring in the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000587"string. This doesn't understand other non-printing characters.");
Guido van Rossum54ec2881999-01-25 22:36:24 +0000588
589static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000590strop_expandtabs(PyObject *self, PyObject *args)
Guido van Rossum54ec2881999-01-25 22:36:24 +0000591{
592 /* Original by Fredrik Lundh */
593 char* e;
594 char* p;
595 char* q;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000596 Py_ssize_t i, j, old_j;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000597 PyObject* out;
598 char* string;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000599 Py_ssize_t stringlen;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000600 int tabsize = 8;
601
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000602 WARN;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000603 /* Get arguments */
Guido van Rossum43713e52000-02-29 13:59:29 +0000604 if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize))
Guido van Rossum54ec2881999-01-25 22:36:24 +0000605 return NULL;
606 if (tabsize < 1) {
607 PyErr_SetString(PyExc_ValueError,
608 "tabsize must be at least 1");
609 return NULL;
610 }
611
612 /* First pass: determine size of output string */
Gregory P. Smith9d534572008-06-11 07:41:16 +0000613 i = j = old_j = 0; /* j: current column; i: total of previous lines */
Guido van Rossum54ec2881999-01-25 22:36:24 +0000614 e = string + stringlen;
615 for (p = string; p < e; p++) {
Gregory P. Smith9d534572008-06-11 07:41:16 +0000616 if (*p == '\t') {
Guido van Rossum54ec2881999-01-25 22:36:24 +0000617 j += tabsize - (j%tabsize);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000618 if (old_j > j) {
619 PyErr_SetString(PyExc_OverflowError,
620 "new string is too long");
621 return NULL;
622 }
623 old_j = j;
624 } else {
Guido van Rossum54ec2881999-01-25 22:36:24 +0000625 j++;
626 if (*p == '\n') {
627 i += j;
628 j = 0;
629 }
630 }
631 }
632
Gregory P. Smith9d534572008-06-11 07:41:16 +0000633 if ((i + j) < 0) {
634 PyErr_SetString(PyExc_OverflowError, "new string is too long");
635 return NULL;
636 }
637
Guido van Rossum54ec2881999-01-25 22:36:24 +0000638 /* Second pass: create output string and fill it */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000639 out = PyString_FromStringAndSize(NULL, i+j);
Guido van Rossum54ec2881999-01-25 22:36:24 +0000640 if (out == NULL)
641 return NULL;
642
643 i = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000644 q = PyString_AS_STRING(out);
Guido van Rossum54ec2881999-01-25 22:36:24 +0000645
646 for (p = string; p < e; p++) {
647 if (*p == '\t') {
648 j = tabsize - (i%tabsize);
649 i += j;
650 while (j-- > 0)
651 *q++ = ' ';
652 } else {
653 *q++ = *p;
654 i++;
655 if (*p == '\n')
656 i = 0;
657 }
658 }
659
660 return out;
661}
662
663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(count__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000665"count(s, sub[, start[, end]]) -> int\n"
666"\n"
667"Return the number of occurrences of substring sub in string\n"
668"s[start:end]. Optional arguments start and end are\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669"interpreted as in slice notation.");
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000670
671static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000672strop_count(PyObject *self, PyObject *args)
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000673{
674 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000675 Py_ssize_t len, n;
Martin v. Löwis23089152006-04-13 07:34:09 +0000676 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000677 Py_ssize_t m, r;
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000678
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000679 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000680 if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last))
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000681 return NULL;
682 if (last > len)
683 last = len;
684 if (last < 0)
685 last += len;
686 if (last < 0)
687 last = 0;
688 if (i < 0)
689 i += len;
690 if (i < 0)
691 i = 0;
692 m = last + 1 - n;
693 if (n == 0)
694 return PyInt_FromLong((long) (m-i));
695
696 r = 0;
697 while (i < m) {
698 if (!memcmp(s+i, sub, n)) {
699 r++;
700 i += n;
701 } else {
702 i++;
703 }
704 }
705 return PyInt_FromLong((long) r);
706}
707
708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000709PyDoc_STRVAR(swapcase__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000710"swapcase(s) -> string\n"
711"\n"
712"Return a copy of the string s with upper case characters\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000713"converted to lowercase and vice versa.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000714
Barry Warsawf5256011996-12-09 18:35:56 +0000715static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000716strop_swapcase(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000717{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000718 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000719 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000720 PyObject *newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000721 int changed;
722
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000723 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000724 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000725 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000726 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000727 if (newstr == NULL)
Guido van Rossum5c850621992-09-11 23:55:51 +0000728 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000729 s_new = PyString_AsString(newstr);
Guido van Rossum5c850621992-09-11 23:55:51 +0000730 changed = 0;
731 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000732 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000733 if (islower(c)) {
734 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000735 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000736 }
737 else if (isupper(c)) {
738 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000739 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000740 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000741 else
742 *s_new = c;
743 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000744 }
745 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000746 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000747 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000748 return args;
749 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000750 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000751}
752
753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000754PyDoc_STRVAR(atoi__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000755"atoi(s [,base]) -> int\n"
756"\n"
757"Return the integer represented by the string s in the given\n"
758"base, which defaults to 10. The string s must consist of one\n"
759"or more digits, possibly preceded by a sign. If base is 0, it\n"
760"is chosen from the leading characters of s, 0 for octal, 0x or\n"
761"0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762"accepted.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000763
Barry Warsawf5256011996-12-09 18:35:56 +0000764static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000765strop_atoi(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000766{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 char *s, *end;
768 int base = 10;
769 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000770 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000772 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000773 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000774 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000775
776 if ((base != 0 && base < 2) || base > 36) {
777 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
778 return NULL;
779 }
780
Guido van Rossumc35f9331996-09-11 23:30:42 +0000781 while (*s && isspace(Py_CHARMASK(*s)))
782 s++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 errno = 0;
784 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000785 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 else
Barry Warsawf5256011996-12-09 18:35:56 +0000787 x = PyOS_strtol(s, &end, base);
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000788 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Guido van Rossum923fece51998-08-04 15:04:52 +0000789 goto bad;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000790 while (*end && isspace(Py_CHARMASK(*end)))
791 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000792 if (*end != '\0') {
Guido van Rossum923fece51998-08-04 15:04:52 +0000793 bad:
Tim Peters885d4572001-11-28 20:27:42 +0000794 PyOS_snprintf(buffer, sizeof(buffer),
795 "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000796 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 return NULL;
798 }
799 else if (errno != 0) {
Tim Peters75cdad52001-11-28 22:07:30 +0000800 PyOS_snprintf(buffer, sizeof(buffer),
801 "atoi() literal too large: %.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 }
Barry Warsawf5256011996-12-09 18:35:56 +0000805 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806}
807
808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000809PyDoc_STRVAR(atol__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000810"atol(s [,base]) -> long\n"
811"\n"
812"Return the long integer represented by the string s in the\n"
813"given base, which defaults to 10. The string s must consist\n"
814"of one or more digits, possibly preceded by a sign. If base\n"
815"is 0, it is chosen from the leading characters of s, 0 for\n"
816"octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
817"0x or 0X is accepted. A trailing L or l is not accepted,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818"unless base is 0.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000819
Barry Warsawf5256011996-12-09 18:35:56 +0000820static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000821strop_atol(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822{
823 char *s, *end;
824 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000825 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000826 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000828 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000829 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000831
832 if ((base != 0 && base < 2) || base > 36) {
833 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
834 return NULL;
835 }
836
Guido van Rossumc35f9331996-09-11 23:30:42 +0000837 while (*s && isspace(Py_CHARMASK(*s)))
838 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000839 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000840 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000841 return NULL;
842 }
Barry Warsawf5256011996-12-09 18:35:56 +0000843 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844 if (x == NULL)
845 return NULL;
846 if (base == 0 && (*end == 'l' || *end == 'L'))
847 end++;
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') {
Tim Peters75cdad52001-11-28 22:07:30 +0000851 PyOS_snprintf(buffer, sizeof(buffer),
852 "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000853 PyErr_SetString(PyExc_ValueError, buffer);
854 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000855 return NULL;
856 }
857 return x;
858}
859
860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000861PyDoc_STRVAR(atof__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000862"atof(s) -> float\n"
863"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000864"Return the floating point number represented by the string s.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000865
Barry Warsawf5256011996-12-09 18:35:56 +0000866static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000867strop_atof(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000868{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000869 char *s, *end;
870 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000871 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000872
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000873 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000874 if (!PyArg_ParseTuple(args, "s:atof", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000875 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000876 while (*s && isspace(Py_CHARMASK(*s)))
877 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000878 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000879 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000880 return NULL;
881 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000882 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000883 PyFPE_START_PROTECT("strop_atof", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000884 x = PyOS_ascii_strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000885 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000886 while (*end && isspace(Py_CHARMASK(*end)))
887 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000888 if (*end != '\0') {
Tim Peters885d4572001-11-28 20:27:42 +0000889 PyOS_snprintf(buffer, sizeof(buffer),
890 "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000891 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000892 return NULL;
893 }
894 else if (errno != 0) {
Tim Peters885d4572001-11-28 20:27:42 +0000895 PyOS_snprintf(buffer, sizeof(buffer),
896 "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000897 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000898 return NULL;
899 }
Barry Warsawf5256011996-12-09 18:35:56 +0000900 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000901}
902
903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000904PyDoc_STRVAR(maketrans__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000905"maketrans(frm, to) -> string\n"
906"\n"
907"Return a translation table (a string of 256 bytes long)\n"
908"suitable for use in string.translate. The strings frm and to\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000909"must be of the same length.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000910
Guido van Rossumed7253c1996-07-23 18:12:39 +0000911static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000912strop_maketrans(PyObject *self, PyObject *args)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000913{
Guido van Rossume0548b81997-01-06 16:50:09 +0000914 unsigned char *c, *from=NULL, *to=NULL;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000915 Py_ssize_t i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000916 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000917
Guido van Rossum43713e52000-02-29 13:59:29 +0000918 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000919 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000920
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000921 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000922 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000923 "maketrans arguments must have same length");
924 return NULL;
925 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000926
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000927 result = PyString_FromStringAndSize((char *)NULL, 256);
Guido van Rossume0548b81997-01-06 16:50:09 +0000928 if (result == NULL)
929 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000930 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000931 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000932 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000933 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000934 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000935
Guido van Rossume0548b81997-01-06 16:50:09 +0000936 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000937}
938
939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940PyDoc_STRVAR(translate__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000941"translate(s,table [,deletechars]) -> string\n"
942"\n"
943"Return a copy of the string s, where all characters occurring\n"
944"in the optional argument deletechars are removed, and the\n"
945"remaining characters have been mapped through the given\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000946"translation table, which must be a string of length 256.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000947
Barry Warsawf5256011996-12-09 18:35:56 +0000948static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000949strop_translate(PyObject *self, PyObject *args)
Guido van Rossuma3127e81995-09-13 17:39:06 +0000950{
Guido van Rossume0548b81997-01-06 16:50:09 +0000951 register char *input, *table, *output;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000952 Py_ssize_t i;
953 int c, changed = 0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000954 PyObject *input_obj;
955 char *table1, *output_start, *del_table=NULL;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000956 Py_ssize_t inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000957 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000958 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000959
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000960 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000961 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
Guido van Rossume0548b81997-01-06 16:50:09 +0000962 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000963 return NULL;
964 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000965 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000966 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000967 return NULL;
968 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000969
Guido van Rossume0548b81997-01-06 16:50:09 +0000970 table = table1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000971 inlen = PyString_GET_SIZE(input_obj);
972 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000973 if (result == NULL)
974 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000975 output_start = output = PyString_AsString(result);
976 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000977
Guido van Rossume0548b81997-01-06 16:50:09 +0000978 if (dellen == 0) {
979 /* If no deletions are required, use faster code */
980 for (i = inlen; --i >= 0; ) {
981 c = Py_CHARMASK(*input++);
982 if (Py_CHARMASK((*output++ = table[c])) != c)
983 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000984 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000985 if (changed)
986 return result;
987 Py_DECREF(result);
988 Py_INCREF(input_obj);
989 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000990 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000991
992 for (i = 0; i < 256; i++)
993 trans_table[i] = Py_CHARMASK(table[i]);
994
Guido van Rossum983c9301997-12-29 19:52:29 +0000995 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000996 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000997
998 for (i = inlen; --i >= 0; ) {
999 c = Py_CHARMASK(*input++);
1000 if (trans_table[c] != -1)
1001 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1002 continue;
1003 changed = 1;
1004 }
1005 if (!changed) {
1006 Py_DECREF(result);
1007 Py_INCREF(input_obj);
1008 return input_obj;
1009 }
1010 /* Fix the size of the resulting string */
Tim Peters5de98422002-04-27 18:44:32 +00001011 if (inlen > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001012 _PyString_Resize(&result, output - output_start);
Guido van Rossuma3127e81995-09-13 17:39:06 +00001013 return result;
1014}
1015
1016
Guido van Rossum101923b1997-04-02 06:11:18 +00001017/* What follows is used for implementing replace(). Perry Stoll. */
1018
1019/*
1020 mymemfind
1021
1022 strstr replacement for arbitrary blocks of memory.
1023
Barry Warsaw51ac5802000-03-20 16:36:48 +00001024 Locates the first occurrence in the memory pointed to by MEM of the
Guido van Rossum101923b1997-04-02 06:11:18 +00001025 contents of memory pointed to by PAT. Returns the index into MEM if
1026 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +00001027 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +00001028*/
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001029static Py_ssize_t
1030mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001031{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001032 register Py_ssize_t ii;
Guido van Rossum101923b1997-04-02 06:11:18 +00001033
1034 /* pattern can not occur in the last pat_len-1 chars */
1035 len -= pat_len;
1036
1037 for (ii = 0; ii <= len; ii++) {
1038 if (mem[ii] == pat[0] &&
1039 (pat_len == 1 ||
1040 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1041 return ii;
1042 }
1043 }
1044 return -1;
1045}
1046
1047/*
1048 mymemcnt
1049
1050 Return the number of distinct times PAT is found in MEM.
1051 meaning mem=1111 and pat==11 returns 2.
1052 mem=11111 and pat==11 also return 2.
1053 */
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001054static Py_ssize_t
1055mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001056{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001057 register Py_ssize_t offset = 0;
1058 Py_ssize_t nfound = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001059
1060 while (len >= 0) {
1061 offset = mymemfind(mem, len, pat, pat_len);
1062 if (offset == -1)
1063 break;
1064 mem += offset + pat_len;
1065 len -= offset + pat_len;
1066 nfound++;
1067 }
1068 return nfound;
1069}
1070
Guido van Rossum983c9301997-12-29 19:52:29 +00001071/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001072 mymemreplace
1073
Thomas Wouters7e474022000-07-16 12:04:32 +00001074 Return a string in which all occurrences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001075 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001076
Thomas Wouters7e474022000-07-16 12:04:32 +00001077 If length of PAT is less than length of STR or there are no occurrences
Guido van Rossum101923b1997-04-02 06:11:18 +00001078 of PAT in STR, then the original string is returned. Otherwise, a new
1079 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001080
Guido van Rossum101923b1997-04-02 06:11:18 +00001081 on return, out_len is:
1082 the length of output string, or
1083 -1 if the input string is returned, or
1084 unchanged if an error occurs (no memory).
1085
1086 return value is:
1087 the new string allocated locally, or
1088 NULL if an error occurred.
1089*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001090static char *
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001091mymemreplace(const char *str, Py_ssize_t len, /* input string */
1092 const char *pat, Py_ssize_t pat_len, /* pattern string to find */
1093 const char *sub, Py_ssize_t sub_len, /* substitution string */
1094 Py_ssize_t count, /* number of replacements */
1095 Py_ssize_t *out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001096{
1097 char *out_s;
1098 char *new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001099 Py_ssize_t nfound, offset, new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001100
1101 if (len == 0 || pat_len > len)
1102 goto return_same;
1103
1104 /* find length of output string */
1105 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters9c012af2001-05-10 00:32:57 +00001106 if (count < 0)
Martin v. Löwis23089152006-04-13 07:34:09 +00001107 count = PY_SSIZE_T_MAX;
Tim Peters9c012af2001-05-10 00:32:57 +00001108 else if (nfound > count)
1109 nfound = count;
Guido van Rossum101923b1997-04-02 06:11:18 +00001110 if (nfound == 0)
1111 goto return_same;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001112
Guido van Rossum101923b1997-04-02 06:11:18 +00001113 new_len = len + nfound*(sub_len - pat_len);
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001114 if (new_len == 0) {
Tim Peters4cd44ef2001-05-10 00:05:33 +00001115 /* Have to allocate something for the caller to free(). */
1116 out_s = (char *)PyMem_MALLOC(1);
Tim Peters9c012af2001-05-10 00:32:57 +00001117 if (out_s == NULL)
Tim Peters4cd44ef2001-05-10 00:05:33 +00001118 return NULL;
1119 out_s[0] = '\0';
Guido van Rossum101923b1997-04-02 06:11:18 +00001120 }
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001121 else {
1122 assert(new_len > 0);
1123 new_s = (char *)PyMem_MALLOC(new_len);
1124 if (new_s == NULL)
1125 return NULL;
1126 out_s = new_s;
1127
Tim Peters9c012af2001-05-10 00:32:57 +00001128 for (; count > 0 && len > 0; --count) {
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001129 /* find index of next instance of pattern */
1130 offset = mymemfind(str, len, pat, pat_len);
1131 if (offset == -1)
1132 break;
1133
1134 /* copy non matching part of input string */
1135 memcpy(new_s, str, offset);
1136 str += offset + pat_len;
1137 len -= offset + pat_len;
1138
1139 /* copy substitute into the output string */
1140 new_s += offset;
1141 memcpy(new_s, sub, sub_len);
1142 new_s += sub_len;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001143 }
1144 /* copy any remaining values into output string */
1145 if (len > 0)
1146 memcpy(new_s, str, len);
1147 }
1148 *out_len = new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001149 return out_s;
1150
1151 return_same:
1152 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001153 return (char *)str; /* cast away const */
Guido van Rossum101923b1997-04-02 06:11:18 +00001154}
1155
1156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001157PyDoc_STRVAR(replace__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00001158"replace (str, old, new[, maxsplit]) -> string\n"
1159"\n"
1160"Return a copy of string str with all occurrences of substring\n"
1161"old replaced by new. If the optional argument maxsplit is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001162"given, only the first maxsplit occurrences are replaced.");
Guido van Rossum983c9301997-12-29 19:52:29 +00001163
1164static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +00001165strop_replace(PyObject *self, PyObject *args)
Guido van Rossum101923b1997-04-02 06:11:18 +00001166{
1167 char *str, *pat,*sub,*new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001168 Py_ssize_t len,pat_len,sub_len,out_len;
1169 Py_ssize_t count = -1;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001170 PyObject *newstr;
Guido van Rossum101923b1997-04-02 06:11:18 +00001171
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001172 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001173 if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",
Barry Warsawf577c081997-11-29 00:10:07 +00001174 &str, &len, &pat, &pat_len, &sub, &sub_len,
1175 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001176 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001177 if (pat_len <= 0) {
1178 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1179 return NULL;
1180 }
Tim Peters1ee77d92001-05-10 01:23:39 +00001181 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1182 * current (2.1) string.py and string methods. Preserve this for
1183 * ... well, hard to say for what <wink>.
1184 */
1185 if (count == 0)
1186 count = -1;
Barry Warsawf577c081997-11-29 00:10:07 +00001187 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001188 if (new_s == NULL) {
1189 PyErr_NoMemory();
1190 return NULL;
1191 }
1192 if (out_len == -1) {
1193 /* we're returning another reference to the input string */
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001194 newstr = PyTuple_GetItem(args, 0);
1195 Py_XINCREF(newstr);
Guido van Rossum101923b1997-04-02 06:11:18 +00001196 }
1197 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001198 newstr = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001199 PyMem_FREE(new_s);
Guido van Rossum101923b1997-04-02 06:11:18 +00001200 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001201 return newstr;
Guido van Rossum101923b1997-04-02 06:11:18 +00001202}
1203
1204
Guido van Rossume270b431992-09-03 20:21:07 +00001205/* List of functions defined in the module */
1206
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001207static PyMethodDef
1208strop_methods[] = {
Tim Peters0f8b4942001-05-09 22:15:03 +00001209 {"atof", strop_atof, METH_VARARGS, atof__doc__},
1210 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
1211 {"atol", strop_atol, METH_VARARGS, atol__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001212 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001213 {"count", strop_count, METH_VARARGS, count__doc__},
1214 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
1215 {"find", strop_find, METH_VARARGS, find__doc__},
1216 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
1217 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001218 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
1219 {"lower", strop_lower, METH_O, lower__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001220 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
1221 {"replace", strop_replace, METH_VARARGS, replace__doc__},
1222 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001223 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001224 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
1225 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001226 {"strip", strop_strip, METH_O, strip__doc__},
1227 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001228 {"translate", strop_translate, METH_VARARGS, translate__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001229 {"upper", strop_upper, METH_O, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001230 {NULL, NULL} /* sentinel */
1231};
1232
1233
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001234PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001235initstrop(void)
Guido van Rossume270b431992-09-03 20:21:07 +00001236{
Fred Drake4baedc12002-04-01 14:53:37 +00001237 PyObject *m, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001238 char buf[256];
1239 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001240 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1241 (PyObject*)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001242 if (m == NULL)
1243 return;
Guido van Rossume22e6441993-07-09 10:51:31 +00001244
1245 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001246 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001247 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001248 if (isspace(c))
1249 buf[n++] = c;
1250 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001251 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001252 if (s)
1253 PyModule_AddObject(m, "whitespace", s);
1254
Guido van Rossume22e6441993-07-09 10:51:31 +00001255 /* Create 'lowercase' object */
1256 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001257 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001258 if (islower(c))
1259 buf[n++] = c;
1260 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001261 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001262 if (s)
1263 PyModule_AddObject(m, "lowercase", s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001264
1265 /* Create 'uppercase' object */
1266 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001267 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001268 if (isupper(c))
1269 buf[n++] = c;
1270 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001271 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001272 if (s)
1273 PyModule_AddObject(m, "uppercase", s);
Guido van Rossume270b431992-09-03 20:21:07 +00001274}