blob: bc609590d4c0e468ba1035b407075ae00504776e [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);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000219 while (reslen + slen + seplen >= sz) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000220 if (_PyString_Resize(&res, sz * 2) < 0)
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000221 return NULL;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000222 sz *= 2;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000223 p = PyString_AsString(res) + reslen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000224 }
225 if (i > 0) {
226 memcpy(p, sep, seplen);
227 p += seplen;
228 reslen += seplen;
229 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000230 memcpy(p, PyString_AS_STRING(item), slen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000231 p += slen;
232 reslen += slen;
233 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000234 _PyString_Resize(&res, reslen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000235 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000236 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000237
238 if (seq->ob_type->tp_as_sequence == NULL ||
239 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
240 {
Barry Warsawf5256011996-12-09 18:35:56 +0000241 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000242 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000243 return NULL;
244 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000245 /* This is now type safe */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000246 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000247 PyObject *item = getitemfunc(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000248 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000249 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000250 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000251 Py_DECREF(res);
252 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000253 return NULL;
254 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000255 slen = PyString_GET_SIZE(item);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000256 while (reslen + slen + seplen >= sz) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000257 if (_PyString_Resize(&res, sz * 2) < 0) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000258 Py_DECREF(item);
259 return NULL;
260 }
261 sz *= 2;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000262 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000263 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000264 if (i > 0) {
265 memcpy(p, sep, seplen);
266 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000267 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000268 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000269 memcpy(p, PyString_AS_STRING(item), slen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000270 p += slen;
271 reslen += slen;
272 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000273 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000274 _PyString_Resize(&res, reslen);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000275 return res;
276}
277
Guido van Rossum983c9301997-12-29 19:52:29 +0000278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(find__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000280"find(s, sub [,start [,end]]) -> in\n"
281"\n"
282"Return the lowest index in s where substring sub is found,\n"
283"such that sub is contained within s[start,end]. Optional\n"
284"arguments start and end are interpreted as in slice notation.\n"
285"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000287
Barry Warsawf5256011996-12-09 18:35:56 +0000288static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000289strop_find(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000290{
291 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000292 Py_ssize_t len, n, i = 0, last = PY_SSIZE_T_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000293
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000294 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000295 if (!PyArg_ParseTuple(args, "t#t#|nn:find", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000296 return NULL;
297
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000298 if (last > len)
299 last = len;
300 if (last < 0)
301 last += len;
302 if (last < 0)
303 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000304 if (i < 0)
305 i += len;
306 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000307 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000308
Guido van Rossum031c6311998-03-24 04:19:22 +0000309 if (n == 0 && i <= last)
Barry Warsawf5256011996-12-09 18:35:56 +0000310 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000311
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000312 last -= n;
313 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000314 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000315 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000316 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000317
Barry Warsawf5256011996-12-09 18:35:56 +0000318 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000319}
320
321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000322PyDoc_STRVAR(rfind__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000323"rfind(s, sub [,start [,end]]) -> int\n"
324"\n"
325"Return the highest index in s where substring sub is found,\n"
326"such that sub is contained within s[start,end]. Optional\n"
327"arguments start and end are interpreted as in slice notation.\n"
328"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000329"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000330
Barry Warsawf5256011996-12-09 18:35:56 +0000331static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000332strop_rfind(PyObject *self, PyObject *args)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000333{
334 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000335 Py_ssize_t len, n, j;
Martin v. Löwis23089152006-04-13 07:34:09 +0000336 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000337
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000338 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000339 if (!PyArg_ParseTuple(args, "t#t#|nn:rfind", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000340 return NULL;
341
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000342 if (last > len)
343 last = len;
344 if (last < 0)
345 last += len;
346 if (last < 0)
347 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000348 if (i < 0)
349 i += len;
350 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000351 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000352
Guido van Rossum031c6311998-03-24 04:19:22 +0000353 if (n == 0 && i <= last)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000354 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000355
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000356 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000357 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000358 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000359 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000360
Barry Warsawf5256011996-12-09 18:35:56 +0000361 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000362}
363
Guido van Rossum983c9301997-12-29 19:52:29 +0000364
Barry Warsawf5256011996-12-09 18:35:56 +0000365static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000366do_strip(PyObject *args, int striptype)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000367{
368 char *s;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000369 Py_ssize_t len, i, j;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000370
371
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000372 if (PyString_AsStringAndSize(args, &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000373 return NULL;
374
375 i = 0;
376 if (striptype != RIGHTSTRIP) {
377 while (i < len && isspace(Py_CHARMASK(s[i]))) {
378 i++;
379 }
380 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000381
382 j = len;
383 if (striptype != LEFTSTRIP) {
384 do {
385 j--;
386 } while (j >= i && isspace(Py_CHARMASK(s[j])));
387 j++;
388 }
389
390 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000391 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000392 return args;
393 }
394 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000395 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000396}
397
Guido van Rossume270b431992-09-03 20:21:07 +0000398
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000399PyDoc_STRVAR(strip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000400"strip(s) -> string\n"
401"\n"
402"Return a copy of the string s with leading and trailing\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000403"whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000404
Barry Warsawf5256011996-12-09 18:35:56 +0000405static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000406strop_strip(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000407{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000408 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000409 return do_strip(args, BOTHSTRIP);
410}
Guido van Rossume270b431992-09-03 20:21:07 +0000411
Guido van Rossum983c9301997-12-29 19:52:29 +0000412
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000413PyDoc_STRVAR(lstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000414"lstrip(s) -> string\n"
415"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416"Return a copy of the string s with leading whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000417
Barry Warsawf5256011996-12-09 18:35:56 +0000418static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000419strop_lstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000420{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000421 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000422 return do_strip(args, LEFTSTRIP);
423}
Guido van Rossume270b431992-09-03 20:21:07 +0000424
Guido van Rossum983c9301997-12-29 19:52:29 +0000425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000426PyDoc_STRVAR(rstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000427"rstrip(s) -> string\n"
428"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000429"Return a copy of the string s with trailing whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000430
Barry Warsawf5256011996-12-09 18:35:56 +0000431static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000432strop_rstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000433{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000434 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000435 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000436}
437
438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000439PyDoc_STRVAR(lower__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000440"lower(s) -> string\n"
441"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442"Return a copy of the string s converted to lowercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000443
Barry Warsawf5256011996-12-09 18:35:56 +0000444static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000445strop_lower(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000446{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000447 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000449 PyObject *newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000450 int changed;
451
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000452 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000453 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000454 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000455 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000456 if (newstr == NULL)
Guido van Rossum5c850621992-09-11 23:55:51 +0000457 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000458 s_new = PyString_AsString(newstr);
Guido van Rossum5c850621992-09-11 23:55:51 +0000459 changed = 0;
460 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000461 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000462 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000463 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000464 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000465 } else
466 *s_new = c;
467 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000468 }
469 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000470 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000471 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000472 return args;
473 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000474 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000475}
476
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(upper__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000479"upper(s) -> string\n"
480"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000481"Return a copy of the string s converted to uppercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000482
Barry Warsawf5256011996-12-09 18:35:56 +0000483static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000484strop_upper(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000485{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000486 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000487 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000488 PyObject *newstr;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000489 int changed;
490
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000491 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 if (PyString_AsStringAndSize(args, &s, &n))
Barry Warsaw04d2d151997-01-03 23:46:51 +0000493 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000494 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000495 if (newstr == NULL)
Barry Warsaw04d2d151997-01-03 23:46:51 +0000496 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000497 s_new = PyString_AsString(newstr);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000498 changed = 0;
499 for (i = 0; i < n; i++) {
500 int c = Py_CHARMASK(*s++);
501 if (islower(c)) {
502 changed = 1;
503 *s_new = toupper(c);
504 } else
505 *s_new = c;
506 s_new++;
507 }
508 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000509 Py_DECREF(newstr);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000510 Py_INCREF(args);
511 return args;
512 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000513 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000514}
515
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(capitalize__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000518"capitalize(s) -> string\n"
519"\n"
520"Return a copy of the string s with only its first character\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521"capitalized.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000522
Barry Warsawf5256011996-12-09 18:35:56 +0000523static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000524strop_capitalize(PyObject *self, PyObject *args)
Guido van Rossum27457531996-06-12 04:24:52 +0000525{
526 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000528 PyObject *newstr;
Guido van Rossum27457531996-06-12 04:24:52 +0000529 int changed;
530
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000531 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000532 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000533 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000534 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000535 if (newstr == NULL)
Guido van Rossum27457531996-06-12 04:24:52 +0000536 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000537 s_new = PyString_AsString(newstr);
Guido van Rossum27457531996-06-12 04:24:52 +0000538 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000539 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000540 int c = Py_CHARMASK(*s++);
541 if (islower(c)) {
542 changed = 1;
543 *s_new = toupper(c);
544 } else
545 *s_new = c;
546 s_new++;
547 }
548 for (i = 1; i < n; i++) {
549 int c = Py_CHARMASK(*s++);
550 if (isupper(c)) {
551 changed = 1;
552 *s_new = tolower(c);
553 } else
554 *s_new = c;
555 s_new++;
556 }
557 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000558 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000559 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000560 return args;
561 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000562 return newstr;
Guido van Rossum27457531996-06-12 04:24:52 +0000563}
564
565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000566PyDoc_STRVAR(expandtabs__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000567"expandtabs(string, [tabsize]) -> string\n"
568"\n"
569"Expand tabs in a string, i.e. replace them by one or more spaces,\n"
570"depending on the current column and the given tab size (default 8).\n"
571"The column number is reset to zero after each newline occurring in the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000572"string. This doesn't understand other non-printing characters.");
Guido van Rossum54ec2881999-01-25 22:36:24 +0000573
574static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000575strop_expandtabs(PyObject *self, PyObject *args)
Guido van Rossum54ec2881999-01-25 22:36:24 +0000576{
577 /* Original by Fredrik Lundh */
578 char* e;
579 char* p;
580 char* q;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000581 Py_ssize_t i, j, old_j;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000582 PyObject* out;
583 char* string;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000584 Py_ssize_t stringlen;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000585 int tabsize = 8;
586
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000587 WARN;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000588 /* Get arguments */
Guido van Rossum43713e52000-02-29 13:59:29 +0000589 if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize))
Guido van Rossum54ec2881999-01-25 22:36:24 +0000590 return NULL;
591 if (tabsize < 1) {
592 PyErr_SetString(PyExc_ValueError,
593 "tabsize must be at least 1");
594 return NULL;
595 }
596
597 /* First pass: determine size of output string */
Gregory P. Smith9d534572008-06-11 07:41:16 +0000598 i = j = old_j = 0; /* j: current column; i: total of previous lines */
Guido van Rossum54ec2881999-01-25 22:36:24 +0000599 e = string + stringlen;
600 for (p = string; p < e; p++) {
Gregory P. Smith9d534572008-06-11 07:41:16 +0000601 if (*p == '\t') {
Guido van Rossum54ec2881999-01-25 22:36:24 +0000602 j += tabsize - (j%tabsize);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000603 if (old_j > j) {
604 PyErr_SetString(PyExc_OverflowError,
605 "new string is too long");
606 return NULL;
607 }
608 old_j = j;
609 } else {
Guido van Rossum54ec2881999-01-25 22:36:24 +0000610 j++;
611 if (*p == '\n') {
612 i += j;
613 j = 0;
614 }
615 }
616 }
617
Gregory P. Smith9d534572008-06-11 07:41:16 +0000618 if ((i + j) < 0) {
619 PyErr_SetString(PyExc_OverflowError, "new string is too long");
620 return NULL;
621 }
622
Guido van Rossum54ec2881999-01-25 22:36:24 +0000623 /* Second pass: create output string and fill it */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000624 out = PyString_FromStringAndSize(NULL, i+j);
Guido van Rossum54ec2881999-01-25 22:36:24 +0000625 if (out == NULL)
626 return NULL;
627
628 i = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000629 q = PyString_AS_STRING(out);
Guido van Rossum54ec2881999-01-25 22:36:24 +0000630
631 for (p = string; p < e; p++) {
632 if (*p == '\t') {
633 j = tabsize - (i%tabsize);
634 i += j;
635 while (j-- > 0)
636 *q++ = ' ';
637 } else {
638 *q++ = *p;
639 i++;
640 if (*p == '\n')
641 i = 0;
642 }
643 }
644
645 return out;
646}
647
648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649PyDoc_STRVAR(count__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000650"count(s, sub[, start[, end]]) -> int\n"
651"\n"
652"Return the number of occurrences of substring sub in string\n"
653"s[start:end]. Optional arguments start and end are\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654"interpreted as in slice notation.");
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000655
656static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000657strop_count(PyObject *self, PyObject *args)
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000658{
659 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000660 Py_ssize_t len, n;
Martin v. Löwis23089152006-04-13 07:34:09 +0000661 Py_ssize_t i = 0, last = PY_SSIZE_T_MAX;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000662 Py_ssize_t m, r;
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000663
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000664 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000665 if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last))
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000666 return NULL;
667 if (last > len)
668 last = len;
669 if (last < 0)
670 last += len;
671 if (last < 0)
672 last = 0;
673 if (i < 0)
674 i += len;
675 if (i < 0)
676 i = 0;
677 m = last + 1 - n;
678 if (n == 0)
679 return PyInt_FromLong((long) (m-i));
680
681 r = 0;
682 while (i < m) {
683 if (!memcmp(s+i, sub, n)) {
684 r++;
685 i += n;
686 } else {
687 i++;
688 }
689 }
690 return PyInt_FromLong((long) r);
691}
692
693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000694PyDoc_STRVAR(swapcase__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000695"swapcase(s) -> string\n"
696"\n"
697"Return a copy of the string s with upper case characters\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698"converted to lowercase and vice versa.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000699
Barry Warsawf5256011996-12-09 18:35:56 +0000700static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000701strop_swapcase(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000702{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000703 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000704 Py_ssize_t i, n;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000705 PyObject *newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000706 int changed;
707
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000708 WARN;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000709 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000710 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000711 newstr = PyString_FromStringAndSize(NULL, n);
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000712 if (newstr == NULL)
Guido van Rossum5c850621992-09-11 23:55:51 +0000713 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000714 s_new = PyString_AsString(newstr);
Guido van Rossum5c850621992-09-11 23:55:51 +0000715 changed = 0;
716 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000717 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000718 if (islower(c)) {
719 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000720 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000721 }
722 else if (isupper(c)) {
723 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000724 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000725 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000726 else
727 *s_new = c;
728 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000729 }
730 if (!changed) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000731 Py_DECREF(newstr);
Barry Warsawf5256011996-12-09 18:35:56 +0000732 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000733 return args;
734 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000735 return newstr;
Guido van Rossum5c850621992-09-11 23:55:51 +0000736}
737
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(atoi__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000740"atoi(s [,base]) -> int\n"
741"\n"
742"Return the integer represented by the string s in the given\n"
743"base, which defaults to 10. The string s must consist of one\n"
744"or more digits, possibly preceded by a sign. If base is 0, it\n"
745"is chosen from the leading characters of s, 0 for octal, 0x or\n"
746"0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000747"accepted.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000748
Barry Warsawf5256011996-12-09 18:35:56 +0000749static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000750strop_atoi(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752 char *s, *end;
753 int base = 10;
754 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000755 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000756
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000757 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000758 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000760
761 if ((base != 0 && base < 2) || base > 36) {
762 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
763 return NULL;
764 }
765
Guido van Rossumc35f9331996-09-11 23:30:42 +0000766 while (*s && isspace(Py_CHARMASK(*s)))
767 s++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000768 errno = 0;
769 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000770 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771 else
Barry Warsawf5256011996-12-09 18:35:56 +0000772 x = PyOS_strtol(s, &end, base);
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000773 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Guido van Rossum923fece51998-08-04 15:04:52 +0000774 goto bad;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000775 while (*end && isspace(Py_CHARMASK(*end)))
776 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 if (*end != '\0') {
Guido van Rossum923fece51998-08-04 15:04:52 +0000778 bad:
Tim Peters885d4572001-11-28 20:27:42 +0000779 PyOS_snprintf(buffer, sizeof(buffer),
780 "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000781 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000782 return NULL;
783 }
784 else if (errno != 0) {
Tim Peters75cdad52001-11-28 22:07:30 +0000785 PyOS_snprintf(buffer, sizeof(buffer),
786 "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000787 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 return NULL;
789 }
Barry Warsawf5256011996-12-09 18:35:56 +0000790 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000791}
792
793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000794PyDoc_STRVAR(atol__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000795"atol(s [,base]) -> long\n"
796"\n"
797"Return the long integer represented by the string s in the\n"
798"given base, which defaults to 10. The string s must consist\n"
799"of one or more digits, possibly preceded by a sign. If base\n"
800"is 0, it is chosen from the leading characters of s, 0 for\n"
801"octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
802"0x or 0X is accepted. A trailing L or l is not accepted,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000803"unless base is 0.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000804
Barry Warsawf5256011996-12-09 18:35:56 +0000805static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000806strop_atol(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000807{
808 char *s, *end;
809 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000810 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000811 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000813 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000814 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000815 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000816
817 if ((base != 0 && base < 2) || base > 36) {
818 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
819 return NULL;
820 }
821
Guido van Rossumc35f9331996-09-11 23:30:42 +0000822 while (*s && isspace(Py_CHARMASK(*s)))
823 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000824 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000825 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000826 return NULL;
827 }
Barry Warsawf5256011996-12-09 18:35:56 +0000828 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 if (x == NULL)
830 return NULL;
831 if (base == 0 && (*end == 'l' || *end == 'L'))
832 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000833 while (*end && isspace(Py_CHARMASK(*end)))
834 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835 if (*end != '\0') {
Tim Peters75cdad52001-11-28 22:07:30 +0000836 PyOS_snprintf(buffer, sizeof(buffer),
837 "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000838 PyErr_SetString(PyExc_ValueError, buffer);
839 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000840 return NULL;
841 }
842 return x;
843}
844
845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000846PyDoc_STRVAR(atof__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000847"atof(s) -> float\n"
848"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849"Return the floating point number represented by the string s.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000850
Barry Warsawf5256011996-12-09 18:35:56 +0000851static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000852strop_atof(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854 char *s, *end;
855 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000856 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000858 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000859 if (!PyArg_ParseTuple(args, "s:atof", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000861 while (*s && isspace(Py_CHARMASK(*s)))
862 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000863 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000864 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000865 return NULL;
866 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000867 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000868 PyFPE_START_PROTECT("strop_atof", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000869 x = PyOS_ascii_strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000870 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000871 while (*end && isspace(Py_CHARMASK(*end)))
872 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873 if (*end != '\0') {
Tim Peters885d4572001-11-28 20:27:42 +0000874 PyOS_snprintf(buffer, sizeof(buffer),
875 "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000876 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877 return NULL;
878 }
879 else if (errno != 0) {
Tim Peters885d4572001-11-28 20:27:42 +0000880 PyOS_snprintf(buffer, sizeof(buffer),
881 "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000882 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000883 return NULL;
884 }
Barry Warsawf5256011996-12-09 18:35:56 +0000885 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886}
887
888
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000889PyDoc_STRVAR(maketrans__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000890"maketrans(frm, to) -> string\n"
891"\n"
892"Return a translation table (a string of 256 bytes long)\n"
893"suitable for use in string.translate. The strings frm and to\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000894"must be of the same length.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000895
Guido van Rossumed7253c1996-07-23 18:12:39 +0000896static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000897strop_maketrans(PyObject *self, PyObject *args)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000898{
Guido van Rossume0548b81997-01-06 16:50:09 +0000899 unsigned char *c, *from=NULL, *to=NULL;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000900 Py_ssize_t i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000901 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000902
Guido van Rossum43713e52000-02-29 13:59:29 +0000903 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000904 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000905
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000906 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000907 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000908 "maketrans arguments must have same length");
909 return NULL;
910 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000911
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000912 result = PyString_FromStringAndSize((char *)NULL, 256);
Guido van Rossume0548b81997-01-06 16:50:09 +0000913 if (result == NULL)
914 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000915 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000916 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000917 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000918 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000919 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000920
Guido van Rossume0548b81997-01-06 16:50:09 +0000921 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000922}
923
924
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000925PyDoc_STRVAR(translate__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000926"translate(s,table [,deletechars]) -> string\n"
927"\n"
928"Return a copy of the string s, where all characters occurring\n"
929"in the optional argument deletechars are removed, and the\n"
930"remaining characters have been mapped through the given\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000931"translation table, which must be a string of length 256.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000932
Barry Warsawf5256011996-12-09 18:35:56 +0000933static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000934strop_translate(PyObject *self, PyObject *args)
Guido van Rossuma3127e81995-09-13 17:39:06 +0000935{
Guido van Rossume0548b81997-01-06 16:50:09 +0000936 register char *input, *table, *output;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000937 Py_ssize_t i;
938 int c, changed = 0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000939 PyObject *input_obj;
940 char *table1, *output_start, *del_table=NULL;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000941 Py_ssize_t inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000942 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000943 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000944
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000945 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000946 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
Guido van Rossume0548b81997-01-06 16:50:09 +0000947 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000948 return NULL;
949 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000950 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000951 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000952 return NULL;
953 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000954
Guido van Rossume0548b81997-01-06 16:50:09 +0000955 table = table1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000956 inlen = PyString_GET_SIZE(input_obj);
957 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000958 if (result == NULL)
959 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000960 output_start = output = PyString_AsString(result);
961 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000962
Guido van Rossume0548b81997-01-06 16:50:09 +0000963 if (dellen == 0) {
964 /* If no deletions are required, use faster code */
965 for (i = inlen; --i >= 0; ) {
966 c = Py_CHARMASK(*input++);
967 if (Py_CHARMASK((*output++ = table[c])) != c)
968 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000969 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000970 if (changed)
971 return result;
972 Py_DECREF(result);
973 Py_INCREF(input_obj);
974 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000975 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000976
977 for (i = 0; i < 256; i++)
978 trans_table[i] = Py_CHARMASK(table[i]);
979
Guido van Rossum983c9301997-12-29 19:52:29 +0000980 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000981 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000982
983 for (i = inlen; --i >= 0; ) {
984 c = Py_CHARMASK(*input++);
985 if (trans_table[c] != -1)
986 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
987 continue;
988 changed = 1;
989 }
990 if (!changed) {
991 Py_DECREF(result);
992 Py_INCREF(input_obj);
993 return input_obj;
994 }
995 /* Fix the size of the resulting string */
Tim Peters5de98422002-04-27 18:44:32 +0000996 if (inlen > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000997 _PyString_Resize(&result, output - output_start);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000998 return result;
999}
1000
1001
Guido van Rossum101923b1997-04-02 06:11:18 +00001002/* What follows is used for implementing replace(). Perry Stoll. */
1003
1004/*
1005 mymemfind
1006
1007 strstr replacement for arbitrary blocks of memory.
1008
Barry Warsaw51ac5802000-03-20 16:36:48 +00001009 Locates the first occurrence in the memory pointed to by MEM of the
Guido van Rossum101923b1997-04-02 06:11:18 +00001010 contents of memory pointed to by PAT. Returns the index into MEM if
1011 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +00001012 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +00001013*/
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001014static Py_ssize_t
1015mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001016{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001017 register Py_ssize_t ii;
Guido van Rossum101923b1997-04-02 06:11:18 +00001018
1019 /* pattern can not occur in the last pat_len-1 chars */
1020 len -= pat_len;
1021
1022 for (ii = 0; ii <= len; ii++) {
1023 if (mem[ii] == pat[0] &&
1024 (pat_len == 1 ||
1025 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1026 return ii;
1027 }
1028 }
1029 return -1;
1030}
1031
1032/*
1033 mymemcnt
1034
1035 Return the number of distinct times PAT is found in MEM.
1036 meaning mem=1111 and pat==11 returns 2.
1037 mem=11111 and pat==11 also return 2.
1038 */
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001039static Py_ssize_t
1040mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001041{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001042 register Py_ssize_t offset = 0;
1043 Py_ssize_t nfound = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001044
1045 while (len >= 0) {
1046 offset = mymemfind(mem, len, pat, pat_len);
1047 if (offset == -1)
1048 break;
1049 mem += offset + pat_len;
1050 len -= offset + pat_len;
1051 nfound++;
1052 }
1053 return nfound;
1054}
1055
Guido van Rossum983c9301997-12-29 19:52:29 +00001056/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001057 mymemreplace
1058
Thomas Wouters7e474022000-07-16 12:04:32 +00001059 Return a string in which all occurrences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001060 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001061
Thomas Wouters7e474022000-07-16 12:04:32 +00001062 If length of PAT is less than length of STR or there are no occurrences
Guido van Rossum101923b1997-04-02 06:11:18 +00001063 of PAT in STR, then the original string is returned. Otherwise, a new
1064 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001065
Guido van Rossum101923b1997-04-02 06:11:18 +00001066 on return, out_len is:
1067 the length of output string, or
1068 -1 if the input string is returned, or
1069 unchanged if an error occurs (no memory).
1070
1071 return value is:
1072 the new string allocated locally, or
1073 NULL if an error occurred.
1074*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001075static char *
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001076mymemreplace(const char *str, Py_ssize_t len, /* input string */
1077 const char *pat, Py_ssize_t pat_len, /* pattern string to find */
1078 const char *sub, Py_ssize_t sub_len, /* substitution string */
1079 Py_ssize_t count, /* number of replacements */
1080 Py_ssize_t *out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001081{
1082 char *out_s;
1083 char *new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001084 Py_ssize_t nfound, offset, new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001085
1086 if (len == 0 || pat_len > len)
1087 goto return_same;
1088
1089 /* find length of output string */
1090 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters9c012af2001-05-10 00:32:57 +00001091 if (count < 0)
Martin v. Löwis23089152006-04-13 07:34:09 +00001092 count = PY_SSIZE_T_MAX;
Tim Peters9c012af2001-05-10 00:32:57 +00001093 else if (nfound > count)
1094 nfound = count;
Guido van Rossum101923b1997-04-02 06:11:18 +00001095 if (nfound == 0)
1096 goto return_same;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001097
Guido van Rossum101923b1997-04-02 06:11:18 +00001098 new_len = len + nfound*(sub_len - pat_len);
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001099 if (new_len == 0) {
Tim Peters4cd44ef2001-05-10 00:05:33 +00001100 /* Have to allocate something for the caller to free(). */
1101 out_s = (char *)PyMem_MALLOC(1);
Tim Peters9c012af2001-05-10 00:32:57 +00001102 if (out_s == NULL)
Tim Peters4cd44ef2001-05-10 00:05:33 +00001103 return NULL;
1104 out_s[0] = '\0';
Guido van Rossum101923b1997-04-02 06:11:18 +00001105 }
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001106 else {
1107 assert(new_len > 0);
1108 new_s = (char *)PyMem_MALLOC(new_len);
1109 if (new_s == NULL)
1110 return NULL;
1111 out_s = new_s;
1112
Tim Peters9c012af2001-05-10 00:32:57 +00001113 for (; count > 0 && len > 0; --count) {
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001114 /* find index of next instance of pattern */
1115 offset = mymemfind(str, len, pat, pat_len);
1116 if (offset == -1)
1117 break;
1118
1119 /* copy non matching part of input string */
1120 memcpy(new_s, str, offset);
1121 str += offset + pat_len;
1122 len -= offset + pat_len;
1123
1124 /* copy substitute into the output string */
1125 new_s += offset;
1126 memcpy(new_s, sub, sub_len);
1127 new_s += sub_len;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001128 }
1129 /* copy any remaining values into output string */
1130 if (len > 0)
1131 memcpy(new_s, str, len);
1132 }
1133 *out_len = new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001134 return out_s;
1135
1136 return_same:
1137 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001138 return (char *)str; /* cast away const */
Guido van Rossum101923b1997-04-02 06:11:18 +00001139}
1140
1141
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001142PyDoc_STRVAR(replace__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00001143"replace (str, old, new[, maxsplit]) -> string\n"
1144"\n"
1145"Return a copy of string str with all occurrences of substring\n"
1146"old replaced by new. If the optional argument maxsplit is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001147"given, only the first maxsplit occurrences are replaced.");
Guido van Rossum983c9301997-12-29 19:52:29 +00001148
1149static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +00001150strop_replace(PyObject *self, PyObject *args)
Guido van Rossum101923b1997-04-02 06:11:18 +00001151{
1152 char *str, *pat,*sub,*new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001153 Py_ssize_t len,pat_len,sub_len,out_len;
1154 Py_ssize_t count = -1;
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001155 PyObject *newstr;
Guido van Rossum101923b1997-04-02 06:11:18 +00001156
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001157 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001158 if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",
Barry Warsawf577c081997-11-29 00:10:07 +00001159 &str, &len, &pat, &pat_len, &sub, &sub_len,
1160 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001161 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001162 if (pat_len <= 0) {
1163 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1164 return NULL;
1165 }
Tim Peters1ee77d92001-05-10 01:23:39 +00001166 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1167 * current (2.1) string.py and string methods. Preserve this for
1168 * ... well, hard to say for what <wink>.
1169 */
1170 if (count == 0)
1171 count = -1;
Barry Warsawf577c081997-11-29 00:10:07 +00001172 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001173 if (new_s == NULL) {
1174 PyErr_NoMemory();
1175 return NULL;
1176 }
1177 if (out_len == -1) {
1178 /* we're returning another reference to the input string */
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001179 newstr = PyTuple_GetItem(args, 0);
1180 Py_XINCREF(newstr);
Guido van Rossum101923b1997-04-02 06:11:18 +00001181 }
1182 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001183 newstr = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001184 PyMem_FREE(new_s);
Guido van Rossum101923b1997-04-02 06:11:18 +00001185 }
Anthony Baxter7cbc0f52006-04-13 07:19:01 +00001186 return newstr;
Guido van Rossum101923b1997-04-02 06:11:18 +00001187}
1188
1189
Guido van Rossume270b431992-09-03 20:21:07 +00001190/* List of functions defined in the module */
1191
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001192static PyMethodDef
1193strop_methods[] = {
Tim Peters0f8b4942001-05-09 22:15:03 +00001194 {"atof", strop_atof, METH_VARARGS, atof__doc__},
1195 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
1196 {"atol", strop_atol, METH_VARARGS, atol__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001197 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001198 {"count", strop_count, METH_VARARGS, count__doc__},
1199 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
1200 {"find", strop_find, METH_VARARGS, find__doc__},
1201 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
1202 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001203 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
1204 {"lower", strop_lower, METH_O, lower__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001205 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
1206 {"replace", strop_replace, METH_VARARGS, replace__doc__},
1207 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001208 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001209 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
1210 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001211 {"strip", strop_strip, METH_O, strip__doc__},
1212 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001213 {"translate", strop_translate, METH_VARARGS, translate__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001214 {"upper", strop_upper, METH_O, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001215 {NULL, NULL} /* sentinel */
1216};
1217
1218
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001219PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001220initstrop(void)
Guido van Rossume270b431992-09-03 20:21:07 +00001221{
Fred Drake4baedc12002-04-01 14:53:37 +00001222 PyObject *m, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001223 char buf[256];
1224 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001225 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1226 (PyObject*)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001227 if (m == NULL)
1228 return;
Guido van Rossume22e6441993-07-09 10:51:31 +00001229
1230 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001231 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001232 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001233 if (isspace(c))
1234 buf[n++] = c;
1235 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001236 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001237 if (s)
1238 PyModule_AddObject(m, "whitespace", s);
1239
Guido van Rossume22e6441993-07-09 10:51:31 +00001240 /* Create 'lowercase' object */
1241 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001242 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001243 if (islower(c))
1244 buf[n++] = c;
1245 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001246 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001247 if (s)
1248 PyModule_AddObject(m, "lowercase", s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001249
1250 /* Create 'uppercase' object */
1251 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001252 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001253 if (isupper(c))
1254 buf[n++] = c;
1255 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001256 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001257 if (s)
1258 PyModule_AddObject(m, "uppercase", s);
Guido van Rossume270b431992-09-03 20:21:07 +00001259}