blob: 8eb64a070ae91db175055bbd28c60f12189c38ed [file] [log] [blame]
Guido van Rossume270b431992-09-03 20:21:07 +00001/* strop module */
2
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003#include "Python.h"
4#include <ctype.h>
5
6PyDoc_STRVAR(strop_module__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00007"Common string manipulations, optimized for speed.\n"
8"\n"
9"Always use \"import string\" rather than referencing\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010"this module directly.");
Guido van Rossum983c9301997-12-29 19:52:29 +000011
Guido van Rossume22e6441993-07-09 10:51:31 +000012/* XXX This file assumes that the <ctype.h> is*() functions
13 XXX are defined for all 8-bit characters! */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000014
Guido van Rossum2e0a6542001-05-15 02:14:44 +000015#define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \
16 "strop functions are obsolete; use string methods")) \
17 return NULL
18
Guido van Rossum7999a5c1996-08-08 19:16:15 +000019/* The lstrip(), rstrip() and strip() functions are implemented
20 in do_strip(), which uses an additional parameter to indicate what
21 type of strip should occur. */
22
23#define LEFTSTRIP 0
24#define RIGHTSTRIP 1
25#define BOTHSTRIP 2
26
Guido van Rossume270b431992-09-03 20:21:07 +000027
Barry Warsawf5256011996-12-09 18:35:56 +000028static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +000029split_whitespace(char *s, int len, int maxsplit)
Guido van Rossum009e79b1995-05-03 17:40:23 +000030{
Barry Warsawe8fc29c1997-01-03 22:45:34 +000031 int i = 0, j, err;
32 int countsplit = 0;
33 PyObject* item;
34 PyObject *list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +000035
Guido van Rossume270b431992-09-03 20:21:07 +000036 if (list == NULL)
37 return NULL;
38
Guido van Rossume270b431992-09-03 20:21:07 +000039 while (i < len) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +000040 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000041 i = i+1;
42 }
43 j = i;
Guido van Rossumee1813d1995-02-14 00:58:59 +000044 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000045 i = i+1;
46 }
47 if (j < i) {
Barry Warsawf5256011996-12-09 18:35:56 +000048 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Barry Warsawe8fc29c1997-01-03 22:45:34 +000049 if (item == NULL)
50 goto finally;
51
Barry Warsawf5256011996-12-09 18:35:56 +000052 err = PyList_Append(list, item);
53 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000054 if (err < 0)
55 goto finally;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000056
57 countsplit++;
Barry Warsaw93be92d1997-12-02 00:29:30 +000058 while (i < len && isspace(Py_CHARMASK(s[i]))) {
59 i = i+1;
60 }
61 if (maxsplit && (countsplit >= maxsplit) && i < len) {
Barry Warsawf5256011996-12-09 18:35:56 +000062 item = PyString_FromStringAndSize(
63 s+i, (int)(len - i));
Barry Warsawe8fc29c1997-01-03 22:45:34 +000064 if (item == NULL)
65 goto finally;
66
Barry Warsawf5256011996-12-09 18:35:56 +000067 err = PyList_Append(list, item);
68 Py_DECREF(item);
Barry Warsawe8fc29c1997-01-03 22:45:34 +000069 if (err < 0)
70 goto finally;
71
Guido van Rossum7999a5c1996-08-08 19:16:15 +000072 i = len;
73 }
Guido van Rossume270b431992-09-03 20:21:07 +000074 }
75 }
Guido van Rossume270b431992-09-03 20:21:07 +000076 return list;
Barry Warsawe8fc29c1997-01-03 22:45:34 +000077 finally:
78 Py_DECREF(list);
79 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +000080}
81
82
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083PyDoc_STRVAR(splitfields__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +000084"split(s [,sep [,maxsplit]]) -> list of strings\n"
85"splitfields(s [,sep [,maxsplit]]) -> list of strings\n"
86"\n"
87"Return a list of the words in the string s, using sep as the\n"
88"delimiter string. If maxsplit is nonzero, splits into at most\n"
89"maxsplit words. If sep is not specified, any whitespace string\n"
90"is a separator. Maxsplit defaults to 0.\n"
91"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092"(split and splitfields are synonymous)");
Guido van Rossum983c9301997-12-29 19:52:29 +000093
Barry Warsawf5256011996-12-09 18:35:56 +000094static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +000095strop_splitfields(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +000096{
Guido van Rossum572d2d91993-11-05 10:14:49 +000097 int len, n, i, j, err;
Guido van Rossum7999a5c1996-08-08 19:16:15 +000098 int splitcount, maxsplit;
Guido van Rossume270b431992-09-03 20:21:07 +000099 char *s, *sub;
Barry Warsawf5256011996-12-09 18:35:56 +0000100 PyObject *list, *item;
Guido van Rossume270b431992-09-03 20:21:07 +0000101
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000102 WARN;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000103 sub = NULL;
104 n = 0;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000105 splitcount = 0;
106 maxsplit = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000107 if (!PyArg_ParseTuple(args, "t#|z#i:split", &s, &len, &sub, &n, &maxsplit))
Guido van Rossume270b431992-09-03 20:21:07 +0000108 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000109 if (sub == NULL)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000110 return split_whitespace(s, len, maxsplit);
Guido van Rossume270b431992-09-03 20:21:07 +0000111 if (n == 0) {
Barry Warsawf5256011996-12-09 18:35:56 +0000112 PyErr_SetString(PyExc_ValueError, "empty separator");
Guido van Rossume270b431992-09-03 20:21:07 +0000113 return NULL;
114 }
115
Barry Warsawf5256011996-12-09 18:35:56 +0000116 list = PyList_New(0);
Guido van Rossume270b431992-09-03 20:21:07 +0000117 if (list == NULL)
118 return NULL;
119
120 i = j = 0;
121 while (i+n <= len) {
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000122 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000123 item = PyString_FromStringAndSize(s+j, (int)(i-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000124 if (item == NULL)
125 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000126 err = PyList_Append(list, item);
127 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000128 if (err < 0)
129 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000130 i = j = i + n;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000131 splitcount++;
132 if (maxsplit && (splitcount >= maxsplit))
133 break;
Guido van Rossume270b431992-09-03 20:21:07 +0000134 }
135 else
136 i++;
137 }
Barry Warsawf5256011996-12-09 18:35:56 +0000138 item = PyString_FromStringAndSize(s+j, (int)(len-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000139 if (item == NULL)
140 goto fail;
Barry Warsawf5256011996-12-09 18:35:56 +0000141 err = PyList_Append(list, item);
142 Py_DECREF(item);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000143 if (err < 0)
144 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000145
146 return list;
Guido van Rossum572d2d91993-11-05 10:14:49 +0000147
148 fail:
Barry Warsawf5256011996-12-09 18:35:56 +0000149 Py_DECREF(list);
Guido van Rossum572d2d91993-11-05 10:14:49 +0000150 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000151}
152
153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154PyDoc_STRVAR(joinfields__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000155"join(list [,sep]) -> string\n"
156"joinfields(list [,sep]) -> string\n"
157"\n"
158"Return a string composed of the words in list, with\n"
159"intervening occurrences of sep. Sep defaults to a single\n"
160"space.\n"
161"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000162"(join and joinfields are synonymous)");
Guido van Rossum983c9301997-12-29 19:52:29 +0000163
Barry Warsawf5256011996-12-09 18:35:56 +0000164static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000165strop_joinfields(PyObject *self, PyObject *args)
Guido van Rossumc89705d1992-11-26 08:54:07 +0000166{
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000167 PyObject *seq;
168 char *sep = NULL;
169 int seqlen, seplen = 0;
170 int i, reslen = 0, slen = 0, sz = 100;
171 PyObject *res = NULL;
172 char* p = NULL;
173 intargfunc getitemfunc;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000174
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000175 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000176 if (!PyArg_ParseTuple(args, "O|t#:join", &seq, &sep, &seplen))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000177 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000178 if (sep == NULL) {
179 sep = " ";
180 seplen = 1;
181 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000182
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000183 seqlen = PySequence_Size(seq);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000184 if (seqlen < 0 && PyErr_Occurred())
185 return NULL;
186
187 if (seqlen == 1) {
188 /* Optimization if there's only one item */
189 PyObject *item = PySequence_GetItem(seq, 0);
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +0000190 if (item && !PyString_Check(item)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000191 PyErr_SetString(PyExc_TypeError,
192 "first argument must be sequence of strings");
Guido van Rossumbf338301998-10-19 13:38:36 +0000193 Py_DECREF(item);
Guido van Rossum1ad1b3f1998-02-06 22:37:12 +0000194 return NULL;
195 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000196 return item;
197 }
198
199 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
200 return NULL;
201 p = PyString_AsString(res);
202
203 /* optimize for lists, since it's the most common case. all others
204 * (tuples and arbitrary sequences) just use the sequence abstract
205 * interface.
Barry Warsaw04d2d151997-01-03 23:46:51 +0000206 */
207 if (PyList_Check(seq)) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000208 for (i = 0; i < seqlen; i++) {
209 PyObject *item = PyList_GET_ITEM(seq, i);
210 if (!PyString_Check(item)) {
211 PyErr_SetString(PyExc_TypeError,
212 "first argument must be sequence of strings");
213 Py_DECREF(res);
214 return NULL;
215 }
216 slen = PyString_GET_SIZE(item);
217 while (reslen + slen + seplen >= sz) {
Tim Peters5de98422002-04-27 18:44:32 +0000218 if (_PyString_Resize(&res, sz * 2) < 0)
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000219 return NULL;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000220 sz *= 2;
221 p = PyString_AsString(res) + reslen;
222 }
223 if (i > 0) {
224 memcpy(p, sep, seplen);
225 p += seplen;
226 reslen += seplen;
227 }
228 memcpy(p, PyString_AS_STRING(item), slen);
229 p += slen;
230 reslen += slen;
231 }
Tim Peters5de98422002-04-27 18:44:32 +0000232 _PyString_Resize(&res, reslen);
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000233 return res;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000234 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000235
236 if (seq->ob_type->tp_as_sequence == NULL ||
237 (getitemfunc = seq->ob_type->tp_as_sequence->sq_item) == NULL)
238 {
Barry Warsawf5256011996-12-09 18:35:56 +0000239 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000240 "first argument must be a sequence");
Guido van Rossumc89705d1992-11-26 08:54:07 +0000241 return NULL;
242 }
Guido van Rossum7df115d1998-05-22 00:53:47 +0000243 /* This is now type safe */
Guido van Rossumc89705d1992-11-26 08:54:07 +0000244 for (i = 0; i < seqlen; i++) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000245 PyObject *item = getitemfunc(seq, i);
246 if (!item || !PyString_Check(item)) {
Barry Warsawf5256011996-12-09 18:35:56 +0000247 PyErr_SetString(PyExc_TypeError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000248 "first argument must be sequence of strings");
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000249 Py_DECREF(res);
250 Py_XDECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000251 return NULL;
252 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000253 slen = PyString_GET_SIZE(item);
254 while (reslen + slen + seplen >= sz) {
Tim Peters5de98422002-04-27 18:44:32 +0000255 if (_PyString_Resize(&res, sz * 2) < 0) {
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000256 Py_DECREF(item);
257 return NULL;
258 }
259 sz *= 2;
260 p = PyString_AsString(res) + reslen;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000261 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000262 if (i > 0) {
263 memcpy(p, sep, seplen);
264 p += seplen;
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000265 reslen += seplen;
Guido van Rossumc89705d1992-11-26 08:54:07 +0000266 }
Barry Warsawd4ff1b91997-01-06 22:48:32 +0000267 memcpy(p, PyString_AS_STRING(item), slen);
268 p += slen;
269 reslen += slen;
270 Py_DECREF(item);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000271 }
Tim Peters5de98422002-04-27 18:44:32 +0000272 _PyString_Resize(&res, reslen);
Guido van Rossumc89705d1992-11-26 08:54:07 +0000273 return res;
274}
275
Guido van Rossum983c9301997-12-29 19:52:29 +0000276
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000277PyDoc_STRVAR(find__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000278"find(s, sub [,start [,end]]) -> in\n"
279"\n"
280"Return the lowest index in s where substring sub is found,\n"
281"such that sub is contained within s[start,end]. Optional\n"
282"arguments start and end are interpreted as in slice notation.\n"
283"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000285
Barry Warsawf5256011996-12-09 18:35:56 +0000286static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000287strop_find(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000288{
289 char *s, *sub;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000290 int len, n, i = 0, last = INT_MAX;
Guido van Rossume270b431992-09-03 20:21:07 +0000291
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000292 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000293 if (!PyArg_ParseTuple(args, "t#t#|ii:find", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000294 return NULL;
295
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000296 if (last > len)
297 last = len;
298 if (last < 0)
299 last += len;
300 if (last < 0)
301 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000302 if (i < 0)
303 i += len;
304 if (i < 0)
Guido van Rossume270b431992-09-03 20:21:07 +0000305 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000306
Guido van Rossum031c6311998-03-24 04:19:22 +0000307 if (n == 0 && i <= last)
Barry Warsawf5256011996-12-09 18:35:56 +0000308 return PyInt_FromLong((long)i);
Guido van Rossume270b431992-09-03 20:21:07 +0000309
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000310 last -= n;
311 for (; i <= last; ++i)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000312 if (s[i] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000313 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000314 return PyInt_FromLong((long)i);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000315
Barry Warsawf5256011996-12-09 18:35:56 +0000316 return PyInt_FromLong(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000317}
318
319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000320PyDoc_STRVAR(rfind__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000321"rfind(s, sub [,start [,end]]) -> int\n"
322"\n"
323"Return the highest index in s where substring sub is found,\n"
324"such that sub is contained within s[start,end]. Optional\n"
325"arguments start and end are interpreted as in slice notation.\n"
326"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327"Return -1 on failure.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000328
Barry Warsawf5256011996-12-09 18:35:56 +0000329static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000330strop_rfind(PyObject *self, PyObject *args)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000331{
332 char *s, *sub;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000333 int len, n, j;
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000334 int i = 0, last = INT_MAX;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000335
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000336 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000337 if (!PyArg_ParseTuple(args, "t#t#|ii:rfind", &s, &len, &sub, &n, &i, &last))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000338 return NULL;
339
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000340 if (last > len)
341 last = len;
342 if (last < 0)
343 last += len;
344 if (last < 0)
345 last = 0;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000346 if (i < 0)
347 i += len;
348 if (i < 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000350
Guido van Rossum031c6311998-03-24 04:19:22 +0000351 if (n == 0 && i <= last)
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000352 return PyInt_FromLong((long)last);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000353
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000354 for (j = last-n; j >= i; --j)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000355 if (s[j] == sub[0] &&
Guido van Rossuma0ca4c41996-10-04 13:39:37 +0000356 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
Barry Warsawf5256011996-12-09 18:35:56 +0000357 return PyInt_FromLong((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000358
Barry Warsawf5256011996-12-09 18:35:56 +0000359 return PyInt_FromLong(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000360}
361
Guido van Rossum983c9301997-12-29 19:52:29 +0000362
Barry Warsawf5256011996-12-09 18:35:56 +0000363static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000364do_strip(PyObject *args, int striptype)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000365{
366 char *s;
367 int len, i, j;
368
369
Neal Norwitz187ae562002-04-02 18:17:57 +0000370 if (PyString_AsStringAndSize(args, &s, &len))
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000371 return NULL;
372
373 i = 0;
374 if (striptype != RIGHTSTRIP) {
375 while (i < len && isspace(Py_CHARMASK(s[i]))) {
376 i++;
377 }
378 }
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000379
380 j = len;
381 if (striptype != LEFTSTRIP) {
382 do {
383 j--;
384 } while (j >= i && isspace(Py_CHARMASK(s[j])));
385 j++;
386 }
387
388 if (i == 0 && j == len) {
Barry Warsawf5256011996-12-09 18:35:56 +0000389 Py_INCREF(args);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000390 return args;
391 }
392 else
Barry Warsawf5256011996-12-09 18:35:56 +0000393 return PyString_FromStringAndSize(s+i, j-i);
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000394}
395
Guido van Rossume270b431992-09-03 20:21:07 +0000396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(strip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000398"strip(s) -> string\n"
399"\n"
400"Return a copy of the string s with leading and trailing\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401"whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000402
Barry Warsawf5256011996-12-09 18:35:56 +0000403static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000404strop_strip(PyObject *self, PyObject *args)
Guido van Rossume270b431992-09-03 20:21:07 +0000405{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000406 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000407 return do_strip(args, BOTHSTRIP);
408}
Guido van Rossume270b431992-09-03 20:21:07 +0000409
Guido van Rossum983c9301997-12-29 19:52:29 +0000410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411PyDoc_STRVAR(lstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000412"lstrip(s) -> string\n"
413"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414"Return a copy of the string s with leading whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000415
Barry Warsawf5256011996-12-09 18:35:56 +0000416static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000417strop_lstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000418{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000419 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000420 return do_strip(args, LEFTSTRIP);
421}
Guido van Rossume270b431992-09-03 20:21:07 +0000422
Guido van Rossum983c9301997-12-29 19:52:29 +0000423
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000424PyDoc_STRVAR(rstrip__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000425"rstrip(s) -> string\n"
426"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427"Return a copy of the string s with trailing whitespace removed.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000428
Barry Warsawf5256011996-12-09 18:35:56 +0000429static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000430strop_rstrip(PyObject *self, PyObject *args)
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000431{
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000432 WARN;
Guido van Rossum7999a5c1996-08-08 19:16:15 +0000433 return do_strip(args, RIGHTSTRIP);
Guido van Rossume270b431992-09-03 20:21:07 +0000434}
435
436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000437PyDoc_STRVAR(lower__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000438"lower(s) -> string\n"
439"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000440"Return a copy of the string s converted to lowercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000441
Barry Warsawf5256011996-12-09 18:35:56 +0000442static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000443strop_lower(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000444{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000445 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000446 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000447 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000448 int changed;
449
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000450 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000451 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000452 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000453 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000454 if (new == NULL)
455 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000456 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000457 changed = 0;
458 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000459 int c = Py_CHARMASK(*s++);
Barry Warsaw04d2d151997-01-03 23:46:51 +0000460 if (isupper(c)) {
Guido van Rossum5c850621992-09-11 23:55:51 +0000461 changed = 1;
Barry Warsaw04d2d151997-01-03 23:46:51 +0000462 *s_new = tolower(c);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000463 } else
464 *s_new = c;
465 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000466 }
467 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000468 Py_DECREF(new);
469 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000470 return args;
471 }
472 return new;
473}
474
475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476PyDoc_STRVAR(upper__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000477"upper(s) -> string\n"
478"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479"Return a copy of the string s converted to uppercase.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000480
Barry Warsawf5256011996-12-09 18:35:56 +0000481static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000482strop_upper(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000483{
Barry Warsaw04d2d151997-01-03 23:46:51 +0000484 char *s, *s_new;
485 int i, n;
486 PyObject *new;
487 int changed;
488
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000489 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000490 if (PyString_AsStringAndSize(args, &s, &n))
Barry Warsaw04d2d151997-01-03 23:46:51 +0000491 return NULL;
492 new = PyString_FromStringAndSize(NULL, n);
493 if (new == NULL)
494 return NULL;
495 s_new = PyString_AsString(new);
496 changed = 0;
497 for (i = 0; i < n; i++) {
498 int c = Py_CHARMASK(*s++);
499 if (islower(c)) {
500 changed = 1;
501 *s_new = toupper(c);
502 } else
503 *s_new = c;
504 s_new++;
505 }
506 if (!changed) {
507 Py_DECREF(new);
508 Py_INCREF(args);
509 return args;
510 }
511 return new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000512}
513
514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515PyDoc_STRVAR(capitalize__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000516"capitalize(s) -> string\n"
517"\n"
518"Return a copy of the string s with only its first character\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519"capitalized.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000520
Barry Warsawf5256011996-12-09 18:35:56 +0000521static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000522strop_capitalize(PyObject *self, PyObject *args)
Guido van Rossum27457531996-06-12 04:24:52 +0000523{
524 char *s, *s_new;
525 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000526 PyObject *new;
Guido van Rossum27457531996-06-12 04:24:52 +0000527 int changed;
528
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000529 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000530 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum27457531996-06-12 04:24:52 +0000531 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000532 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum27457531996-06-12 04:24:52 +0000533 if (new == NULL)
534 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000535 s_new = PyString_AsString(new);
Guido van Rossum27457531996-06-12 04:24:52 +0000536 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000537 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000538 int c = Py_CHARMASK(*s++);
539 if (islower(c)) {
540 changed = 1;
541 *s_new = toupper(c);
542 } else
543 *s_new = c;
544 s_new++;
545 }
546 for (i = 1; i < n; i++) {
547 int c = Py_CHARMASK(*s++);
548 if (isupper(c)) {
549 changed = 1;
550 *s_new = tolower(c);
551 } else
552 *s_new = c;
553 s_new++;
554 }
555 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000556 Py_DECREF(new);
557 Py_INCREF(args);
Guido van Rossum27457531996-06-12 04:24:52 +0000558 return args;
559 }
560 return new;
561}
562
563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000564PyDoc_STRVAR(expandtabs__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000565"expandtabs(string, [tabsize]) -> string\n"
566"\n"
567"Expand tabs in a string, i.e. replace them by one or more spaces,\n"
568"depending on the current column and the given tab size (default 8).\n"
569"The column number is reset to zero after each newline occurring in the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000570"string. This doesn't understand other non-printing characters.");
Guido van Rossum54ec2881999-01-25 22:36:24 +0000571
572static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000573strop_expandtabs(PyObject *self, PyObject *args)
Guido van Rossum54ec2881999-01-25 22:36:24 +0000574{
575 /* Original by Fredrik Lundh */
576 char* e;
577 char* p;
578 char* q;
579 int i, j;
580 PyObject* out;
581 char* string;
582 int stringlen;
583 int tabsize = 8;
584
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000585 WARN;
Guido van Rossum54ec2881999-01-25 22:36:24 +0000586 /* Get arguments */
Guido van Rossum43713e52000-02-29 13:59:29 +0000587 if (!PyArg_ParseTuple(args, "s#|i:expandtabs", &string, &stringlen, &tabsize))
Guido van Rossum54ec2881999-01-25 22:36:24 +0000588 return NULL;
589 if (tabsize < 1) {
590 PyErr_SetString(PyExc_ValueError,
591 "tabsize must be at least 1");
592 return NULL;
593 }
594
595 /* First pass: determine size of output string */
596 i = j = 0; /* j: current column; i: total of previous lines */
597 e = string + stringlen;
598 for (p = string; p < e; p++) {
599 if (*p == '\t')
600 j += tabsize - (j%tabsize);
601 else {
602 j++;
603 if (*p == '\n') {
604 i += j;
605 j = 0;
606 }
607 }
608 }
609
610 /* Second pass: create output string and fill it */
611 out = PyString_FromStringAndSize(NULL, i+j);
612 if (out == NULL)
613 return NULL;
614
615 i = 0;
616 q = PyString_AS_STRING(out);
617
618 for (p = string; p < e; p++) {
619 if (*p == '\t') {
620 j = tabsize - (i%tabsize);
621 i += j;
622 while (j-- > 0)
623 *q++ = ' ';
624 } else {
625 *q++ = *p;
626 i++;
627 if (*p == '\n')
628 i = 0;
629 }
630 }
631
632 return out;
633}
634
635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636PyDoc_STRVAR(count__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000637"count(s, sub[, start[, end]]) -> int\n"
638"\n"
639"Return the number of occurrences of substring sub in string\n"
640"s[start:end]. Optional arguments start and end are\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000641"interpreted as in slice notation.");
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000642
643static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000644strop_count(PyObject *self, PyObject *args)
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000645{
646 char *s, *sub;
Guido van Rossumc5015831998-10-07 16:36:14 +0000647 int len, n;
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000648 int i = 0, last = INT_MAX;
649 int m, r;
650
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000651 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000652 if (!PyArg_ParseTuple(args, "t#t#|ii:count", &s, &len, &sub, &n, &i, &last))
Guido van Rossumd5bcf9a1998-10-06 19:43:14 +0000653 return NULL;
654 if (last > len)
655 last = len;
656 if (last < 0)
657 last += len;
658 if (last < 0)
659 last = 0;
660 if (i < 0)
661 i += len;
662 if (i < 0)
663 i = 0;
664 m = last + 1 - n;
665 if (n == 0)
666 return PyInt_FromLong((long) (m-i));
667
668 r = 0;
669 while (i < m) {
670 if (!memcmp(s+i, sub, n)) {
671 r++;
672 i += n;
673 } else {
674 i++;
675 }
676 }
677 return PyInt_FromLong((long) r);
678}
679
680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000681PyDoc_STRVAR(swapcase__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000682"swapcase(s) -> string\n"
683"\n"
684"Return a copy of the string s with upper case characters\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000685"converted to lowercase and vice versa.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000686
Barry Warsawf5256011996-12-09 18:35:56 +0000687static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000688strop_swapcase(PyObject *self, PyObject *args)
Guido van Rossum5c850621992-09-11 23:55:51 +0000689{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000690 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000691 int i, n;
Barry Warsawf5256011996-12-09 18:35:56 +0000692 PyObject *new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000693 int changed;
694
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000695 WARN;
Neal Norwitz187ae562002-04-02 18:17:57 +0000696 if (PyString_AsStringAndSize(args, &s, &n))
Guido van Rossum5c850621992-09-11 23:55:51 +0000697 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000698 new = PyString_FromStringAndSize(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000699 if (new == NULL)
700 return NULL;
Barry Warsawf5256011996-12-09 18:35:56 +0000701 s_new = PyString_AsString(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000702 changed = 0;
703 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000704 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000705 if (islower(c)) {
706 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000707 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000708 }
709 else if (isupper(c)) {
710 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000711 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000712 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000713 else
714 *s_new = c;
715 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000716 }
717 if (!changed) {
Barry Warsawf5256011996-12-09 18:35:56 +0000718 Py_DECREF(new);
719 Py_INCREF(args);
Guido van Rossum5c850621992-09-11 23:55:51 +0000720 return args;
721 }
722 return new;
723}
724
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(atoi__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000727"atoi(s [,base]) -> int\n"
728"\n"
729"Return the integer represented by the string s in the given\n"
730"base, which defaults to 10. The string s must consist of one\n"
731"or more digits, possibly preceded by a sign. If base is 0, it\n"
732"is chosen from the leading characters of s, 0 for octal, 0x or\n"
733"0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734"accepted.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000735
Barry Warsawf5256011996-12-09 18:35:56 +0000736static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000737strop_atoi(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739 char *s, *end;
740 int base = 10;
741 long x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000742 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000744 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000745 if (!PyArg_ParseTuple(args, "s|i:atoi", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000747
748 if ((base != 0 && base < 2) || base > 36) {
749 PyErr_SetString(PyExc_ValueError, "invalid base for atoi()");
750 return NULL;
751 }
752
Guido van Rossumc35f9331996-09-11 23:30:42 +0000753 while (*s && isspace(Py_CHARMASK(*s)))
754 s++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000755 errno = 0;
756 if (base == 0 && s[0] == '0')
Barry Warsawf5256011996-12-09 18:35:56 +0000757 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 else
Barry Warsawf5256011996-12-09 18:35:56 +0000759 x = PyOS_strtol(s, &end, base);
Neal Norwitz3afb2d22002-03-20 21:32:07 +0000760 if (end == s || !isalnum((int)end[-1]))
Guido van Rossum923fece51998-08-04 15:04:52 +0000761 goto bad;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000762 while (*end && isspace(Py_CHARMASK(*end)))
763 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764 if (*end != '\0') {
Guido van Rossum923fece51998-08-04 15:04:52 +0000765 bad:
Tim Peters885d4572001-11-28 20:27:42 +0000766 PyOS_snprintf(buffer, sizeof(buffer),
767 "invalid literal for atoi(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000768 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 return NULL;
770 }
771 else if (errno != 0) {
Tim Peters75cdad52001-11-28 22:07:30 +0000772 PyOS_snprintf(buffer, sizeof(buffer),
773 "atoi() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000774 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 return NULL;
776 }
Barry Warsawf5256011996-12-09 18:35:56 +0000777 return PyInt_FromLong(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778}
779
780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000781PyDoc_STRVAR(atol__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000782"atol(s [,base]) -> long\n"
783"\n"
784"Return the long integer represented by the string s in the\n"
785"given base, which defaults to 10. The string s must consist\n"
786"of one or more digits, possibly preceded by a sign. If base\n"
787"is 0, it is chosen from the leading characters of s, 0 for\n"
788"octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
789"0x or 0X is accepted. A trailing L or l is not accepted,\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000790"unless base is 0.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000791
Barry Warsawf5256011996-12-09 18:35:56 +0000792static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000793strop_atol(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794{
795 char *s, *end;
796 int base = 10;
Barry Warsawf5256011996-12-09 18:35:56 +0000797 PyObject *x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000798 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000799
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000800 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000801 if (!PyArg_ParseTuple(args, "s|i:atol", &s, &base))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000802 return NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000803
804 if ((base != 0 && base < 2) || base > 36) {
805 PyErr_SetString(PyExc_ValueError, "invalid base for atol()");
806 return NULL;
807 }
808
Guido van Rossumc35f9331996-09-11 23:30:42 +0000809 while (*s && isspace(Py_CHARMASK(*s)))
810 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000811 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000812 PyErr_SetString(PyExc_ValueError, "empty string for atol()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000813 return NULL;
814 }
Barry Warsawf5256011996-12-09 18:35:56 +0000815 x = PyLong_FromString(s, &end, base);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000816 if (x == NULL)
817 return NULL;
818 if (base == 0 && (*end == 'l' || *end == 'L'))
819 end++;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000820 while (*end && isspace(Py_CHARMASK(*end)))
821 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822 if (*end != '\0') {
Tim Peters75cdad52001-11-28 22:07:30 +0000823 PyOS_snprintf(buffer, sizeof(buffer),
824 "invalid literal for atol(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000825 PyErr_SetString(PyExc_ValueError, buffer);
826 Py_DECREF(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827 return NULL;
828 }
829 return x;
830}
831
832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(atof__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000834"atof(s) -> float\n"
835"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000836"Return the floating point number represented by the string s.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000837
Barry Warsawf5256011996-12-09 18:35:56 +0000838static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000839strop_atof(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000840{
Tim Petersdbd9ba62000-07-09 03:09:57 +0000841 extern double strtod(const char *, char **);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000842 char *s, *end;
843 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000844 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000845
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000846 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000847 if (!PyArg_ParseTuple(args, "s:atof", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000848 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000849 while (*s && isspace(Py_CHARMASK(*s)))
850 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000851 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000852 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000853 return NULL;
854 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000855 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000856 PyFPE_START_PROTECT("strop_atof", return 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857 x = strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000858 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000859 while (*end && isspace(Py_CHARMASK(*end)))
860 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000861 if (*end != '\0') {
Tim Peters885d4572001-11-28 20:27:42 +0000862 PyOS_snprintf(buffer, sizeof(buffer),
863 "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000864 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000865 return NULL;
866 }
867 else if (errno != 0) {
Tim Peters885d4572001-11-28 20:27:42 +0000868 PyOS_snprintf(buffer, sizeof(buffer),
869 "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000870 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000871 return NULL;
872 }
Barry Warsawf5256011996-12-09 18:35:56 +0000873 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000874}
875
876
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000877PyDoc_STRVAR(maketrans__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000878"maketrans(frm, to) -> string\n"
879"\n"
880"Return a translation table (a string of 256 bytes long)\n"
881"suitable for use in string.translate. The strings frm and to\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882"must be of the same length.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000883
Guido van Rossumed7253c1996-07-23 18:12:39 +0000884static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000885strop_maketrans(PyObject *self, PyObject *args)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000886{
Guido van Rossume0548b81997-01-06 16:50:09 +0000887 unsigned char *c, *from=NULL, *to=NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000888 int i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000889 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000890
Guido van Rossum43713e52000-02-29 13:59:29 +0000891 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000892 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000893
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000894 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000895 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000896 "maketrans arguments must have same length");
897 return NULL;
898 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000899
900 result = PyString_FromStringAndSize((char *)NULL, 256);
901 if (result == NULL)
902 return NULL;
903 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000904 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000905 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000906 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000907 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000908
Guido van Rossume0548b81997-01-06 16:50:09 +0000909 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000910}
911
912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000913PyDoc_STRVAR(translate__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000914"translate(s,table [,deletechars]) -> string\n"
915"\n"
916"Return a copy of the string s, where all characters occurring\n"
917"in the optional argument deletechars are removed, and the\n"
918"remaining characters have been mapped through the given\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000919"translation table, which must be a string of length 256.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000920
Barry Warsawf5256011996-12-09 18:35:56 +0000921static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000922strop_translate(PyObject *self, PyObject *args)
Guido van Rossuma3127e81995-09-13 17:39:06 +0000923{
Guido van Rossume0548b81997-01-06 16:50:09 +0000924 register char *input, *table, *output;
925 register int i, c, changed = 0;
926 PyObject *input_obj;
927 char *table1, *output_start, *del_table=NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000928 int inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000929 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000930 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000931
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000932 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000933 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
Guido van Rossume0548b81997-01-06 16:50:09 +0000934 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000935 return NULL;
936 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000937 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000938 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000939 return NULL;
940 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000941
Guido van Rossume0548b81997-01-06 16:50:09 +0000942 table = table1;
943 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000944 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000945 if (result == NULL)
946 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000947 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000948 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000949
Guido van Rossume0548b81997-01-06 16:50:09 +0000950 if (dellen == 0) {
951 /* If no deletions are required, use faster code */
952 for (i = inlen; --i >= 0; ) {
953 c = Py_CHARMASK(*input++);
954 if (Py_CHARMASK((*output++ = table[c])) != c)
955 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000956 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000957 if (changed)
958 return result;
959 Py_DECREF(result);
960 Py_INCREF(input_obj);
961 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000962 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000963
964 for (i = 0; i < 256; i++)
965 trans_table[i] = Py_CHARMASK(table[i]);
966
Guido van Rossum983c9301997-12-29 19:52:29 +0000967 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000968 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000969
970 for (i = inlen; --i >= 0; ) {
971 c = Py_CHARMASK(*input++);
972 if (trans_table[c] != -1)
973 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
974 continue;
975 changed = 1;
976 }
977 if (!changed) {
978 Py_DECREF(result);
979 Py_INCREF(input_obj);
980 return input_obj;
981 }
982 /* Fix the size of the resulting string */
Tim Peters5de98422002-04-27 18:44:32 +0000983 if (inlen > 0)
984 _PyString_Resize(&result, output - output_start);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000985 return result;
986}
987
988
Guido van Rossum101923b1997-04-02 06:11:18 +0000989/* What follows is used for implementing replace(). Perry Stoll. */
990
991/*
992 mymemfind
993
994 strstr replacement for arbitrary blocks of memory.
995
Barry Warsaw51ac5802000-03-20 16:36:48 +0000996 Locates the first occurrence in the memory pointed to by MEM of the
Guido van Rossum101923b1997-04-02 06:11:18 +0000997 contents of memory pointed to by PAT. Returns the index into MEM if
998 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +0000999 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +00001000*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001001static int
Tim Peters4cd44ef2001-05-10 00:05:33 +00001002mymemfind(const char *mem, int len, const char *pat, int pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001003{
1004 register int ii;
1005
1006 /* pattern can not occur in the last pat_len-1 chars */
1007 len -= pat_len;
1008
1009 for (ii = 0; ii <= len; ii++) {
1010 if (mem[ii] == pat[0] &&
1011 (pat_len == 1 ||
1012 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1013 return ii;
1014 }
1015 }
1016 return -1;
1017}
1018
1019/*
1020 mymemcnt
1021
1022 Return the number of distinct times PAT is found in MEM.
1023 meaning mem=1111 and pat==11 returns 2.
1024 mem=11111 and pat==11 also return 2.
1025 */
Tim Peters0f8b4942001-05-09 22:15:03 +00001026static int
Tim Peters4cd44ef2001-05-10 00:05:33 +00001027mymemcnt(const char *mem, int len, const char *pat, int pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001028{
1029 register int offset = 0;
1030 int nfound = 0;
1031
1032 while (len >= 0) {
1033 offset = mymemfind(mem, len, pat, pat_len);
1034 if (offset == -1)
1035 break;
1036 mem += offset + pat_len;
1037 len -= offset + pat_len;
1038 nfound++;
1039 }
1040 return nfound;
1041}
1042
Guido van Rossum983c9301997-12-29 19:52:29 +00001043/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001044 mymemreplace
1045
Thomas Wouters7e474022000-07-16 12:04:32 +00001046 Return a string in which all occurrences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001047 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001048
Thomas Wouters7e474022000-07-16 12:04:32 +00001049 If length of PAT is less than length of STR or there are no occurrences
Guido van Rossum101923b1997-04-02 06:11:18 +00001050 of PAT in STR, then the original string is returned. Otherwise, a new
1051 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001052
Guido van Rossum101923b1997-04-02 06:11:18 +00001053 on return, out_len is:
1054 the length of output string, or
1055 -1 if the input string is returned, or
1056 unchanged if an error occurs (no memory).
1057
1058 return value is:
1059 the new string allocated locally, or
1060 NULL if an error occurred.
1061*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001062static char *
Tim Peters4cd44ef2001-05-10 00:05:33 +00001063mymemreplace(const char *str, int len, /* input string */
1064 const char *pat, int pat_len, /* pattern string to find */
1065 const char *sub, int sub_len, /* substitution string */
1066 int count, /* number of replacements */
1067 int *out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001068{
1069 char *out_s;
1070 char *new_s;
1071 int nfound, offset, new_len;
1072
1073 if (len == 0 || pat_len > len)
1074 goto return_same;
1075
1076 /* find length of output string */
1077 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters9c012af2001-05-10 00:32:57 +00001078 if (count < 0)
1079 count = INT_MAX;
1080 else if (nfound > count)
1081 nfound = count;
Guido van Rossum101923b1997-04-02 06:11:18 +00001082 if (nfound == 0)
1083 goto return_same;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001084
Guido van Rossum101923b1997-04-02 06:11:18 +00001085 new_len = len + nfound*(sub_len - pat_len);
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001086 if (new_len == 0) {
Tim Peters4cd44ef2001-05-10 00:05:33 +00001087 /* Have to allocate something for the caller to free(). */
1088 out_s = (char *)PyMem_MALLOC(1);
Tim Peters9c012af2001-05-10 00:32:57 +00001089 if (out_s == NULL)
Tim Peters4cd44ef2001-05-10 00:05:33 +00001090 return NULL;
1091 out_s[0] = '\0';
Guido van Rossum101923b1997-04-02 06:11:18 +00001092 }
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001093 else {
1094 assert(new_len > 0);
1095 new_s = (char *)PyMem_MALLOC(new_len);
1096 if (new_s == NULL)
1097 return NULL;
1098 out_s = new_s;
1099
Tim Peters9c012af2001-05-10 00:32:57 +00001100 for (; count > 0 && len > 0; --count) {
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001101 /* find index of next instance of pattern */
1102 offset = mymemfind(str, len, pat, pat_len);
1103 if (offset == -1)
1104 break;
1105
1106 /* copy non matching part of input string */
1107 memcpy(new_s, str, offset);
1108 str += offset + pat_len;
1109 len -= offset + pat_len;
1110
1111 /* copy substitute into the output string */
1112 new_s += offset;
1113 memcpy(new_s, sub, sub_len);
1114 new_s += sub_len;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001115 }
1116 /* copy any remaining values into output string */
1117 if (len > 0)
1118 memcpy(new_s, str, len);
1119 }
1120 *out_len = new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001121 return out_s;
1122
1123 return_same:
1124 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001125 return (char *)str; /* cast away const */
Guido van Rossum101923b1997-04-02 06:11:18 +00001126}
1127
1128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001129PyDoc_STRVAR(replace__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00001130"replace (str, old, new[, maxsplit]) -> string\n"
1131"\n"
1132"Return a copy of string str with all occurrences of substring\n"
1133"old replaced by new. If the optional argument maxsplit is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134"given, only the first maxsplit occurrences are replaced.");
Guido van Rossum983c9301997-12-29 19:52:29 +00001135
1136static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +00001137strop_replace(PyObject *self, PyObject *args)
Guido van Rossum101923b1997-04-02 06:11:18 +00001138{
1139 char *str, *pat,*sub,*new_s;
1140 int len,pat_len,sub_len,out_len;
Tim Petersda45d552001-05-10 00:59:45 +00001141 int count = -1;
Guido van Rossum101923b1997-04-02 06:11:18 +00001142 PyObject *new;
1143
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001144 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +00001145 if (!PyArg_ParseTuple(args, "t#t#t#|i:replace",
Barry Warsawf577c081997-11-29 00:10:07 +00001146 &str, &len, &pat, &pat_len, &sub, &sub_len,
1147 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001148 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001149 if (pat_len <= 0) {
1150 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1151 return NULL;
1152 }
Tim Peters1ee77d92001-05-10 01:23:39 +00001153 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1154 * current (2.1) string.py and string methods. Preserve this for
1155 * ... well, hard to say for what <wink>.
1156 */
1157 if (count == 0)
1158 count = -1;
Barry Warsawf577c081997-11-29 00:10:07 +00001159 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001160 if (new_s == NULL) {
1161 PyErr_NoMemory();
1162 return NULL;
1163 }
1164 if (out_len == -1) {
1165 /* we're returning another reference to the input string */
1166 new = PyTuple_GetItem(args, 0);
1167 Py_XINCREF(new);
1168 }
1169 else {
1170 new = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001171 PyMem_FREE(new_s);
Guido van Rossum101923b1997-04-02 06:11:18 +00001172 }
1173 return new;
1174}
1175
1176
Guido van Rossume270b431992-09-03 20:21:07 +00001177/* List of functions defined in the module */
1178
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001179static PyMethodDef
1180strop_methods[] = {
Tim Peters0f8b4942001-05-09 22:15:03 +00001181 {"atof", strop_atof, METH_VARARGS, atof__doc__},
1182 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
1183 {"atol", strop_atol, METH_VARARGS, atol__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001184 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001185 {"count", strop_count, METH_VARARGS, count__doc__},
1186 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
1187 {"find", strop_find, METH_VARARGS, find__doc__},
1188 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
1189 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001190 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
1191 {"lower", strop_lower, METH_O, lower__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001192 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
1193 {"replace", strop_replace, METH_VARARGS, replace__doc__},
1194 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001195 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001196 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
1197 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001198 {"strip", strop_strip, METH_O, strip__doc__},
1199 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001200 {"translate", strop_translate, METH_VARARGS, translate__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001201 {"upper", strop_upper, METH_O, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001202 {NULL, NULL} /* sentinel */
1203};
1204
1205
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001206PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001207initstrop(void)
Guido van Rossume270b431992-09-03 20:21:07 +00001208{
Fred Drake4baedc12002-04-01 14:53:37 +00001209 PyObject *m, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001210 char buf[256];
1211 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001212 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1213 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossume22e6441993-07-09 10:51:31 +00001214
1215 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001216 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001217 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001218 if (isspace(c))
1219 buf[n++] = c;
1220 }
Barry Warsawf5256011996-12-09 18:35:56 +00001221 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001222 if (s)
1223 PyModule_AddObject(m, "whitespace", s);
1224
Guido van Rossume22e6441993-07-09 10:51:31 +00001225 /* Create 'lowercase' object */
1226 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001227 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001228 if (islower(c))
1229 buf[n++] = c;
1230 }
Barry Warsawf5256011996-12-09 18:35:56 +00001231 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001232 if (s)
1233 PyModule_AddObject(m, "lowercase", s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001234
1235 /* Create 'uppercase' object */
1236 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001237 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001238 if (isupper(c))
1239 buf[n++] = c;
1240 }
Barry Warsawf5256011996-12-09 18:35:56 +00001241 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001242 if (s)
1243 PyModule_AddObject(m, "uppercase", s);
Guido van Rossume270b431992-09-03 20:21:07 +00001244}