blob: cffef3af3a6d11295251601125d7eb2d77cc6368 [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) {
Martin v. Löwis5c97c792006-02-17 15:49:09 +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) {
Barry Warsawf5256011996-12-09 18:35:56 +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)) {
Martin v. Löwis5c97c792006-02-17 15:49:09 +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 }
Martin v. Löwis5c97c792006-02-17 15:49:09 +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);
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +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
201 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
202 return NULL;
203 p = PyString_AsString(res);
204
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);
212 if (!PyString_Check(item)) {
213 PyErr_SetString(PyExc_TypeError,
214 "first argument must be sequence of strings");
215 Py_DECREF(res);
216 return NULL;
217 }
218 slen = PyString_GET_SIZE(item);
219 while (reslen + slen + seplen >= sz) {
Tim Peters5de98422002-04-27 18:44:32 +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;
223 p = PyString_AsString(res) + reslen;
224 }
225 if (i > 0) {
226 memcpy(p, sep, seplen);
227 p += seplen;
228 reslen += seplen;
229 }
230 memcpy(p, PyString_AS_STRING(item), slen);
231 p += slen;
232 reslen += slen;
233 }
Tim Peters5de98422002-04-27 18:44:32 +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);
248 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 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000255 slen = PyString_GET_SIZE(item);
256 while (reslen + slen + seplen >= sz) {
Tim Peters5de98422002-04-27 18:44:32 +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;
262 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 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000269 memcpy(p, PyString_AS_STRING(item), slen);
270 p += slen;
271 reslen += slen;
272 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000273 }
Tim Peters5de98422002-04-27 18:44:32 +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;
336 Py_ssize_t i = 0, last = INT_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
Neal Norwitz187ae562002-04-02 18:17:57 +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
Barry Warsawf5256011996-12-09 18:35:56 +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;
Barry Warsawf5256011996-12-09 18:35:56 +0000449 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000450 int changed;
451
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000452 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000453 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000454 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000455 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000456 if (new == NULL)
457 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000458 s_new = PyString_AsString(new);
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) {
Barry Warsawf5256011996-12-09 18:35:56 +0000470 Py_DECREF(new);
471 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000472 return args;
473 }
474 return new;
475}
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;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000488 PyObject *new;
489 int changed;
490
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000491 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000492 if (PyString_AsStringAndSize(args, &s, &n))
Barry Warsaw04d2d151997-01-03 23:46:51 +0000493 return NULL;
494 new = PyString_FromStringAndSize(NULL, n);
495 if (new == NULL)
496 return NULL;
497 s_new = PyString_AsString(new);
498 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) {
509 Py_DECREF(new);
510 Py_INCREF(args);
511 return args;
512 }
513 return new;
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;
Barry Warsawf5256011996-12-09 18:35:56 +0000528 PyObject *new;
Guido van Rossum27457531996-06-12 04:24:52 +0000529 int changed;
530
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000531 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000532 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000533 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000534 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum27457531996-06-12 04:24:52 +0000535 if (new == NULL)
536 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000537 s_new = PyString_AsString(new);
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) {
Barry Warsawf5256011996-12-09 18:35:56 +0000558 Py_DECREF(new);
559 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000560 return args;
561 }
562 return new;
563}
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;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000581 Py_ssize_t i, 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 */
598 i = j = 0; /* j: current column; i: total of previous lines */
599 e = string + stringlen;
600 for (p = string; p < e; p++) {
601 if (*p == '\t')
602 j += tabsize - (j%tabsize);
603 else {
604 j++;
605 if (*p == '\n') {
606 i += j;
607 j = 0;
608 }
609 }
610 }
611
612 /* Second pass: create output string and fill it */
613 out = PyString_FromStringAndSize(NULL, i+j);
614 if (out == NULL)
615 return NULL;
616
617 i = 0;
618 q = PyString_AS_STRING(out);
619
620 for (p = string; p < e; p++) {
621 if (*p == '\t') {
622 j = tabsize - (i%tabsize);
623 i += j;
624 while (j-- > 0)
625 *q++ = ' ';
626 } else {
627 *q++ = *p;
628 i++;
629 if (*p == '\n')
630 i = 0;
631 }
632 }
633
634 return out;
635}
636
637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000638PyDoc_STRVAR(count__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000639"count(s, sub[, start[, end]]) -> int\n"
640"\n"
641"Return the number of occurrences of substring sub in string\n"
642"s[start:end]. Optional arguments start and end are\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643"interpreted as in slice notation.");
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000644
645static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000646strop_count(PyObject *self, PyObject *args)
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000647{
648 char *s, *sub;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000649 Py_ssize_t len, n;
650 Py_ssize_t i = 0, last = INT_MAX;
651 Py_ssize_t m, r;
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000652
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000653 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000654 if (!PyArg_ParseTuple(args, "t#t#|nn:count", &s, &len, &sub, &n, &i, &last))
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000655 return NULL;
656 if (last > len)
657 last = len;
658 if (last < 0)
659 last += len;
660 if (last < 0)
661 last = 0;
662 if (i < 0)
663 i += len;
664 if (i < 0)
665 i = 0;
666 m = last + 1 - n;
667 if (n == 0)
668 return PyInt_FromLong((long) (m-i));
669
670 r = 0;
671 while (i < m) {
672 if (!memcmp(s+i, sub, n)) {
673 r++;
674 i += n;
675 } else {
676 i++;
677 }
678 }
679 return PyInt_FromLong((long) r);
680}
681
682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000683PyDoc_STRVAR(swapcase__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000684"swapcase(s) -> string\n"
685"\n"
686"Return a copy of the string s with upper case characters\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000687"converted to lowercase and vice versa.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000688
Barry Warsawf5256011996-12-09 18:35:56 +0000689static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000690strop_swapcase(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000691{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000692 char *s, *s_new;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000693 Py_ssize_t i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000694 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000695 int changed;
696
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000697 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000698 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000699 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000700 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000701 if (new == NULL)
702 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000703 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000704 changed = 0;
705 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000706 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000707 if (islower(c)) {
708 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000709 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000710 }
711 else if (isupper(c)) {
712 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000713 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000714 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000715 else
716 *s_new = c;
717 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000718 }
719 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000720 Py_DECREF(new);
721 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000722 return args;
723 }
724 return new;
725}
726
727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000728PyDoc_STRVAR(atoi__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000729"atoi(s [,base]) -> int\n"
730"\n"
731"Return the integer represented by the string s in the given\n"
732"base, which defaults to 10. The string s must consist of one\n"
733"or more digits, possibly preceded by a sign. If base is 0, it\n"
734"is chosen from the leading characters of s, 0 for octal, 0x or\n"
735"0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736"accepted.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000737
Barry Warsawf5256011996-12-09 18:35:56 +0000738static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000739strop_atoi(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741 char *s, *end;
742 int base = 10;
743 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000744 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000745
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000746 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000747 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000749
750 if ((base != 0 && base < 2) || base > 36) {
751 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
752 return NULL;
753 }
754
Guido van Rossumc35f9331996-09-11 23:30:42 +0000755 while (*s && isspace(Py_CHARMASK(*s)))
756 s++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000757 errno = 0;
758 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000759 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000760 else
Barry Warsawf5256011996-12-09 18:35:56 +0000761 x = PyOS_strtol(s, &end, base);
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000762 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Guido van Rossum923fece51998-08-04 15:04:52 +0000763 goto bad;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000764 while (*end && isspace(Py_CHARMASK(*end)))
765 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000766 if (*end != '\0') {
Guido van Rossum923fece51998-08-04 15:04:52 +0000767 bad:
Tim Peters885d4572001-11-28 20:27:42 +0000768 PyOS_snprintf(buffer, sizeof(buffer),
769 "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000770 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771 return NULL;
772 }
773 else if (errno != 0) {
Tim Peters75cdad52001-11-28 22:07:30 +0000774 PyOS_snprintf(buffer, sizeof(buffer),
775 "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000776 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 return NULL;
778 }
Barry Warsawf5256011996-12-09 18:35:56 +0000779 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000780}
781
782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000783PyDoc_STRVAR(atol__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000784"atol(s [,base]) -> long\n"
785"\n"
786"Return the long integer represented by the string s in the\n"
787"given base, which defaults to 10. The string s must consist\n"
788"of one or more digits, possibly preceded by a sign. If base\n"
789"is 0, it is chosen from the leading characters of s, 0 for\n"
790"octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
791"0x or 0X is accepted. A trailing L or l is not accepted,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000792"unless base is 0.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000793
Barry Warsawf5256011996-12-09 18:35:56 +0000794static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000795strop_atol(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796{
797 char *s, *end;
798 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000799 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000800 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000801
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000802 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000803 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000804 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000805
806 if ((base != 0 && base < 2) || base > 36) {
807 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
808 return NULL;
809 }
810
Guido van Rossumc35f9331996-09-11 23:30:42 +0000811 while (*s && isspace(Py_CHARMASK(*s)))
812 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000813 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000814 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000815 return NULL;
816 }
Barry Warsawf5256011996-12-09 18:35:56 +0000817 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000818 if (x == NULL)
819 return NULL;
820 if (base == 0 && (*end == 'l' || *end == 'L'))
821 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000822 while (*end && isspace(Py_CHARMASK(*end)))
823 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000824 if (*end != '\0') {
Tim Peters75cdad52001-11-28 22:07:30 +0000825 PyOS_snprintf(buffer, sizeof(buffer),
826 "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000827 PyErr_SetString(PyExc_ValueError, buffer);
828 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 return NULL;
830 }
831 return x;
832}
833
834
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000835PyDoc_STRVAR(atof__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000836"atof(s) -> float\n"
837"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838"Return the floating point number represented by the string s.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000839
Barry Warsawf5256011996-12-09 18:35:56 +0000840static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000841strop_atof(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000842{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000843 char *s, *end;
844 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000845 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000847 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000848 if (!PyArg_ParseTuple(args, "s:atof", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000849 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000850 while (*s && isspace(Py_CHARMASK(*s)))
851 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000852 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000853 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000854 return NULL;
855 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000856 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000857 PyFPE_START_PROTECT("strop_atof", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000858 x = PyOS_ascii_strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000859 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000860 while (*end && isspace(Py_CHARMASK(*end)))
861 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000862 if (*end != '\0') {
Tim Peters885d4572001-11-28 20:27:42 +0000863 PyOS_snprintf(buffer, sizeof(buffer),
864 "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000865 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866 return NULL;
867 }
868 else if (errno != 0) {
Tim Peters885d4572001-11-28 20:27:42 +0000869 PyOS_snprintf(buffer, sizeof(buffer),
870 "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000871 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000872 return NULL;
873 }
Barry Warsawf5256011996-12-09 18:35:56 +0000874 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000875}
876
877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000878PyDoc_STRVAR(maketrans__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000879"maketrans(frm, to) -> string\n"
880"\n"
881"Return a translation table (a string of 256 bytes long)\n"
882"suitable for use in string.translate. The strings frm and to\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000883"must be of the same length.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000884
Guido van Rossumed7253c1996-07-23 18:12:39 +0000885static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000886strop_maketrans(PyObject *self, PyObject *args)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000887{
Guido van Rossume0548b81997-01-06 16:50:09 +0000888 unsigned char *c, *from=NULL, *to=NULL;
Martin v. Löwis5c97c792006-02-17 15:49:09 +0000889 Py_ssize_t i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000890 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000891
Guido van Rossum43713e52000-02-29 13:59:29 +0000892 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000893 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000894
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000895 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000896 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000897 "maketrans arguments must have same length");
898 return NULL;
899 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000900
901 result = PyString_FromStringAndSize((char *)NULL, 256);
902 if (result == NULL)
903 return NULL;
904 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000905 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000906 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000907 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000908 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000909
Guido van Rossume0548b81997-01-06 16:50:09 +0000910 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000911}
912
913
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000914PyDoc_STRVAR(translate__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000915"translate(s,table [,deletechars]) -> string\n"
916"\n"
917"Return a copy of the string s, where all characters occurring\n"
918"in the optional argument deletechars are removed, and the\n"
919"remaining characters have been mapped through the given\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000920"translation table, which must be a string of length 256.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000921
Barry Warsawf5256011996-12-09 18:35:56 +0000922static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000923strop_translate(PyObject *self, PyObject *args)
Guido van Rossuma3127e81995-09-13 17:39:06 +0000924{
Guido van Rossume0548b81997-01-06 16:50:09 +0000925 register char *input, *table, *output;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000926 Py_ssize_t i;
927 int c, changed = 0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000928 PyObject *input_obj;
929 char *table1, *output_start, *del_table=NULL;
Martin v. Löwisad0a4622006-02-16 14:30:23 +0000930 Py_ssize_t inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000931 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000932 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000933
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000934 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000935 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
Guido van Rossume0548b81997-01-06 16:50:09 +0000936 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000937 return NULL;
938 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000939 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000940 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000941 return NULL;
942 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000943
Guido van Rossume0548b81997-01-06 16:50:09 +0000944 table = table1;
945 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000946 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000947 if (result == NULL)
948 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000949 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000950 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000951
Guido van Rossume0548b81997-01-06 16:50:09 +0000952 if (dellen == 0) {
953 /* If no deletions are required, use faster code */
954 for (i = inlen; --i >= 0; ) {
955 c = Py_CHARMASK(*input++);
956 if (Py_CHARMASK((*output++ = table[c])) != c)
957 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000958 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000959 if (changed)
960 return result;
961 Py_DECREF(result);
962 Py_INCREF(input_obj);
963 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000964 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000965
966 for (i = 0; i < 256; i++)
967 trans_table[i] = Py_CHARMASK(table[i]);
968
Guido van Rossum983c9301997-12-29 19:52:29 +0000969 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000970 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000971
972 for (i = inlen; --i >= 0; ) {
973 c = Py_CHARMASK(*input++);
974 if (trans_table[c] != -1)
975 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
976 continue;
977 changed = 1;
978 }
979 if (!changed) {
980 Py_DECREF(result);
981 Py_INCREF(input_obj);
982 return input_obj;
983 }
984 /* Fix the size of the resulting string */
Tim Peters5de98422002-04-27 18:44:32 +0000985 if (inlen > 0)
986 _PyString_Resize(&result, output - output_start);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000987 return result;
988}
989
990
Guido van Rossum101923b1997-04-02 06:11:18 +0000991/* What follows is used for implementing replace(). Perry Stoll. */
992
993/*
994 mymemfind
995
996 strstr replacement for arbitrary blocks of memory.
997
Barry Warsaw51ac5802000-03-20 16:36:48 +0000998 Locates the first occurrence in the memory pointed to by MEM of the
Guido van Rossum101923b1997-04-02 06:11:18 +0000999 contents of memory pointed to by PAT. Returns the index into MEM if
1000 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +00001001 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +00001002*/
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001003static Py_ssize_t
1004mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001005{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001006 register Py_ssize_t ii;
Guido van Rossum101923b1997-04-02 06:11:18 +00001007
1008 /* pattern can not occur in the last pat_len-1 chars */
1009 len -= pat_len;
1010
1011 for (ii = 0; ii <= len; ii++) {
1012 if (mem[ii] == pat[0] &&
1013 (pat_len == 1 ||
1014 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1015 return ii;
1016 }
1017 }
1018 return -1;
1019}
1020
1021/*
1022 mymemcnt
1023
1024 Return the number of distinct times PAT is found in MEM.
1025 meaning mem=1111 and pat==11 returns 2.
1026 mem=11111 and pat==11 also return 2.
1027 */
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001028static Py_ssize_t
1029mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001030{
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001031 register Py_ssize_t offset = 0;
1032 Py_ssize_t nfound = 0;
Guido van Rossum101923b1997-04-02 06:11:18 +00001033
1034 while (len >= 0) {
1035 offset = mymemfind(mem, len, pat, pat_len);
1036 if (offset == -1)
1037 break;
1038 mem += offset + pat_len;
1039 len -= offset + pat_len;
1040 nfound++;
1041 }
1042 return nfound;
1043}
1044
Guido van Rossum983c9301997-12-29 19:52:29 +00001045/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001046 mymemreplace
1047
Thomas Wouters7e474022000-07-16 12:04:32 +00001048 Return a string in which all occurrences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001049 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001050
Thomas Wouters7e474022000-07-16 12:04:32 +00001051 If length of PAT is less than length of STR or there are no occurrences
Guido van Rossum101923b1997-04-02 06:11:18 +00001052 of PAT in STR, then the original string is returned. Otherwise, a new
1053 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001054
Guido van Rossum101923b1997-04-02 06:11:18 +00001055 on return, out_len is:
1056 the length of output string, or
1057 -1 if the input string is returned, or
1058 unchanged if an error occurs (no memory).
1059
1060 return value is:
1061 the new string allocated locally, or
1062 NULL if an error occurred.
1063*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001064static char *
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001065mymemreplace(const char *str, Py_ssize_t len, /* input string */
1066 const char *pat, Py_ssize_t pat_len, /* pattern string to find */
1067 const char *sub, Py_ssize_t sub_len, /* substitution string */
1068 Py_ssize_t count, /* number of replacements */
1069 Py_ssize_t *out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001070{
1071 char *out_s;
1072 char *new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001073 Py_ssize_t nfound, offset, new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001074
1075 if (len == 0 || pat_len > len)
1076 goto return_same;
1077
1078 /* find length of output string */
1079 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters9c012af2001-05-10 00:32:57 +00001080 if (count < 0)
1081 count = INT_MAX;
1082 else if (nfound > count)
1083 nfound = count;
Guido van Rossum101923b1997-04-02 06:11:18 +00001084 if (nfound == 0)
1085 goto return_same;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001086
Guido van Rossum101923b1997-04-02 06:11:18 +00001087 new_len = len + nfound*(sub_len - pat_len);
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001088 if (new_len == 0) {
Tim Peters4cd44ef2001-05-10 00:05:33 +00001089 /* Have to allocate something for the caller to free(). */
1090 out_s = (char *)PyMem_MALLOC(1);
Tim Peters9c012af2001-05-10 00:32:57 +00001091 if (out_s == NULL)
Tim Peters4cd44ef2001-05-10 00:05:33 +00001092 return NULL;
1093 out_s[0] = '\0';
Guido van Rossum101923b1997-04-02 06:11:18 +00001094 }
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001095 else {
1096 assert(new_len > 0);
1097 new_s = (char *)PyMem_MALLOC(new_len);
1098 if (new_s == NULL)
1099 return NULL;
1100 out_s = new_s;
1101
Tim Peters9c012af2001-05-10 00:32:57 +00001102 for (; count > 0 && len > 0; --count) {
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001103 /* find index of next instance of pattern */
1104 offset = mymemfind(str, len, pat, pat_len);
1105 if (offset == -1)
1106 break;
1107
1108 /* copy non matching part of input string */
1109 memcpy(new_s, str, offset);
1110 str += offset + pat_len;
1111 len -= offset + pat_len;
1112
1113 /* copy substitute into the output string */
1114 new_s += offset;
1115 memcpy(new_s, sub, sub_len);
1116 new_s += sub_len;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001117 }
1118 /* copy any remaining values into output string */
1119 if (len > 0)
1120 memcpy(new_s, str, len);
1121 }
1122 *out_len = new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001123 return out_s;
1124
1125 return_same:
1126 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001127 return (char *)str; /* cast away const */
Guido van Rossum101923b1997-04-02 06:11:18 +00001128}
1129
1130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001131PyDoc_STRVAR(replace__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00001132"replace (str, old, new[, maxsplit]) -> string\n"
1133"\n"
1134"Return a copy of string str with all occurrences of substring\n"
1135"old replaced by new. If the optional argument maxsplit is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136"given, only the first maxsplit occurrences are replaced.");
Guido van Rossum983c9301997-12-29 19:52:29 +00001137
1138static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +00001139strop_replace(PyObject *self, PyObject *args)
Guido van Rossum101923b1997-04-02 06:11:18 +00001140{
1141 char *str, *pat,*sub,*new_s;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001142 Py_ssize_t len,pat_len,sub_len,out_len;
1143 Py_ssize_t count = -1;
Guido van Rossum101923b1997-04-02 06:11:18 +00001144 PyObject *new;
1145
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001146 WARN;
Martin v. Löwis5c97c792006-02-17 15:49:09 +00001147 if (!PyArg_ParseTuple(args, "t#t#t#|n:replace",
Barry Warsawf577c081997-11-29 00:10:07 +00001148 &str, &len, &pat, &pat_len, &sub, &sub_len,
1149 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001150 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001151 if (pat_len <= 0) {
1152 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1153 return NULL;
1154 }
Tim Peters1ee77d92001-05-10 01:23:39 +00001155 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1156 * current (2.1) string.py and string methods. Preserve this for
1157 * ... well, hard to say for what <wink>.
1158 */
1159 if (count == 0)
1160 count = -1;
Barry Warsawf577c081997-11-29 00:10:07 +00001161 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001162 if (new_s == NULL) {
1163 PyErr_NoMemory();
1164 return NULL;
1165 }
1166 if (out_len == -1) {
1167 /* we're returning another reference to the input string */
1168 new = PyTuple_GetItem(args, 0);
1169 Py_XINCREF(new);
1170 }
1171 else {
1172 new = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001173 PyMem_FREE(new_s);
Guido van Rossum101923b1997-04-02 06:11:18 +00001174 }
1175 return new;
1176}
1177
1178
Guido van Rossume270b431992-09-03 20:21:07 +00001179/* List of functions defined in the module */
1180
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001181static PyMethodDef
1182strop_methods[] = {
Tim Peters0f8b4942001-05-09 22:15:03 +00001183 {"atof", strop_atof, METH_VARARGS, atof__doc__},
1184 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
1185 {"atol", strop_atol, METH_VARARGS, atol__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001186 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001187 {"count", strop_count, METH_VARARGS, count__doc__},
1188 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
1189 {"find", strop_find, METH_VARARGS, find__doc__},
1190 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
1191 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001192 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
1193 {"lower", strop_lower, METH_O, lower__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001194 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
1195 {"replace", strop_replace, METH_VARARGS, replace__doc__},
1196 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001197 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001198 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
1199 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001200 {"strip", strop_strip, METH_O, strip__doc__},
1201 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001202 {"translate", strop_translate, METH_VARARGS, translate__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001203 {"upper", strop_upper, METH_O, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001204 {NULL, NULL} /* sentinel */
1205};
1206
1207
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001208PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001209initstrop(void)
Guido van Rossume270b431992-09-03 20:21:07 +00001210{
Fred Drake4baedc12002-04-01 14:53:37 +00001211 PyObject *m, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001212 char buf[256];
1213 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001214 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1215 (PyObject*)NULL, PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001216 if (m == NULL)
1217 return;
Guido van Rossume22e6441993-07-09 10:51:31 +00001218
1219 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001220 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001221 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001222 if (isspace(c))
1223 buf[n++] = c;
1224 }
Barry Warsawf5256011996-12-09 18:35:56 +00001225 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001226 if (s)
1227 PyModule_AddObject(m, "whitespace", s);
1228
Guido van Rossume22e6441993-07-09 10:51:31 +00001229 /* Create 'lowercase' object */
1230 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001231 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001232 if (islower(c))
1233 buf[n++] = c;
1234 }
Barry Warsawf5256011996-12-09 18:35:56 +00001235 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001236 if (s)
1237 PyModule_AddObject(m, "lowercase", s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001238
1239 /* Create 'uppercase' object */
1240 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001241 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001242 if (isupper(c))
1243 buf[n++] = c;
1244 }
Barry Warsawf5256011996-12-09 18:35:56 +00001245 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001246 if (s)
1247 PyModule_AddObject(m, "uppercase", s);
Guido van Rossume270b431992-09-03 20:21:07 +00001248}