blob: ce19a053a09856dcdc2fae033f8cc7e27b1957c2 [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{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000841 char *s, *end;
842 double x;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000843 char buffer[256]; /* For errors */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000845 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000846 if (!PyArg_ParseTuple(args, "s:atof", &s))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000847 return NULL;
Guido van Rossumc35f9331996-09-11 23:30:42 +0000848 while (*s && isspace(Py_CHARMASK(*s)))
849 s++;
Guido van Rossum171191e1996-08-21 20:02:25 +0000850 if (s[0] == '\0') {
Barry Warsawf5256011996-12-09 18:35:56 +0000851 PyErr_SetString(PyExc_ValueError, "empty string for atof()");
Guido van Rossum171191e1996-08-21 20:02:25 +0000852 return NULL;
853 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000855 PyFPE_START_PROTECT("strop_atof", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000856 x = PyOS_ascii_strtod(s, &end);
Guido van Rossum7b7c5781997-03-14 04:13:56 +0000857 PyFPE_END_PROTECT(x)
Guido van Rossumc35f9331996-09-11 23:30:42 +0000858 while (*end && isspace(Py_CHARMASK(*end)))
859 end++;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 if (*end != '\0') {
Tim Peters885d4572001-11-28 20:27:42 +0000861 PyOS_snprintf(buffer, sizeof(buffer),
862 "invalid literal for atof(): %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000863 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000864 return NULL;
865 }
866 else if (errno != 0) {
Tim Peters885d4572001-11-28 20:27:42 +0000867 PyOS_snprintf(buffer, sizeof(buffer),
868 "atof() literal too large: %.200s", s);
Barry Warsawf5256011996-12-09 18:35:56 +0000869 PyErr_SetString(PyExc_ValueError, buffer);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000870 return NULL;
871 }
Barry Warsawf5256011996-12-09 18:35:56 +0000872 return PyFloat_FromDouble(x);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873}
874
875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(maketrans__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000877"maketrans(frm, to) -> string\n"
878"\n"
879"Return a translation table (a string of 256 bytes long)\n"
880"suitable for use in string.translate. The strings frm and to\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000881"must be of the same length.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000882
Guido van Rossumed7253c1996-07-23 18:12:39 +0000883static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000884strop_maketrans(PyObject *self, PyObject *args)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000885{
Guido van Rossume0548b81997-01-06 16:50:09 +0000886 unsigned char *c, *from=NULL, *to=NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000887 int i, fromlen=0, tolen=0;
Guido van Rossume0548b81997-01-06 16:50:09 +0000888 PyObject *result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000889
Guido van Rossum43713e52000-02-29 13:59:29 +0000890 if (!PyArg_ParseTuple(args, "t#t#:maketrans", &from, &fromlen, &to, &tolen))
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000891 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000892
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000893 if (fromlen != tolen) {
Barry Warsawf5256011996-12-09 18:35:56 +0000894 PyErr_SetString(PyExc_ValueError,
Guido van Rossumed7253c1996-07-23 18:12:39 +0000895 "maketrans arguments must have same length");
896 return NULL;
897 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000898
899 result = PyString_FromStringAndSize((char *)NULL, 256);
900 if (result == NULL)
901 return NULL;
902 c = (unsigned char *) PyString_AS_STRING((PyStringObject *)result);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000903 for (i = 0; i < 256; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000904 c[i]=(unsigned char)i;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000905 for (i = 0; i < fromlen; i++)
Guido van Rossumed7253c1996-07-23 18:12:39 +0000906 c[from[i]]=to[i];
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000907
Guido van Rossume0548b81997-01-06 16:50:09 +0000908 return result;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000909}
910
911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912PyDoc_STRVAR(translate__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +0000913"translate(s,table [,deletechars]) -> string\n"
914"\n"
915"Return a copy of the string s, where all characters occurring\n"
916"in the optional argument deletechars are removed, and the\n"
917"remaining characters have been mapped through the given\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000918"translation table, which must be a string of length 256.");
Guido van Rossum983c9301997-12-29 19:52:29 +0000919
Barry Warsawf5256011996-12-09 18:35:56 +0000920static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +0000921strop_translate(PyObject *self, PyObject *args)
Guido van Rossuma3127e81995-09-13 17:39:06 +0000922{
Guido van Rossume0548b81997-01-06 16:50:09 +0000923 register char *input, *table, *output;
924 register int i, c, changed = 0;
925 PyObject *input_obj;
926 char *table1, *output_start, *del_table=NULL;
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000927 int inlen, tablen, dellen = 0;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000928 PyObject *result;
Guido van Rossume0548b81997-01-06 16:50:09 +0000929 int trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000930
Guido van Rossum2e0a6542001-05-15 02:14:44 +0000931 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +0000932 if (!PyArg_ParseTuple(args, "St#|t#:translate", &input_obj,
Guido van Rossume0548b81997-01-06 16:50:09 +0000933 &table1, &tablen, &del_table, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000934 return NULL;
935 if (tablen != 256) {
Barry Warsawf5256011996-12-09 18:35:56 +0000936 PyErr_SetString(PyExc_ValueError,
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000937 "translation table must be 256 characters long");
Guido van Rossuma3127e81995-09-13 17:39:06 +0000938 return NULL;
939 }
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000940
Guido van Rossume0548b81997-01-06 16:50:09 +0000941 table = table1;
942 inlen = PyString_Size(input_obj);
Guido van Rossumed7253c1996-07-23 18:12:39 +0000943 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000944 if (result == NULL)
945 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000946 output_start = output = PyString_AsString(result);
Guido van Rossume0548b81997-01-06 16:50:09 +0000947 input = PyString_AsString(input_obj);
Barry Warsawe8fc29c1997-01-03 22:45:34 +0000948
Guido van Rossume0548b81997-01-06 16:50:09 +0000949 if (dellen == 0) {
950 /* If no deletions are required, use faster code */
951 for (i = inlen; --i >= 0; ) {
952 c = Py_CHARMASK(*input++);
953 if (Py_CHARMASK((*output++ = table[c])) != c)
954 changed = 1;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000955 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000956 if (changed)
957 return result;
958 Py_DECREF(result);
959 Py_INCREF(input_obj);
960 return input_obj;
Guido van Rossuma3127e81995-09-13 17:39:06 +0000961 }
Guido van Rossume0548b81997-01-06 16:50:09 +0000962
963 for (i = 0; i < 256; i++)
964 trans_table[i] = Py_CHARMASK(table[i]);
965
Guido van Rossum983c9301997-12-29 19:52:29 +0000966 for (i = 0; i < dellen; i++)
Guido van Rossum1ed5e571997-04-29 21:34:16 +0000967 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
Guido van Rossume0548b81997-01-06 16:50:09 +0000968
969 for (i = inlen; --i >= 0; ) {
970 c = Py_CHARMASK(*input++);
971 if (trans_table[c] != -1)
972 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
973 continue;
974 changed = 1;
975 }
976 if (!changed) {
977 Py_DECREF(result);
978 Py_INCREF(input_obj);
979 return input_obj;
980 }
981 /* Fix the size of the resulting string */
Tim Peters5de98422002-04-27 18:44:32 +0000982 if (inlen > 0)
983 _PyString_Resize(&result, output - output_start);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000984 return result;
985}
986
987
Guido van Rossum101923b1997-04-02 06:11:18 +0000988/* What follows is used for implementing replace(). Perry Stoll. */
989
990/*
991 mymemfind
992
993 strstr replacement for arbitrary blocks of memory.
994
Barry Warsaw51ac5802000-03-20 16:36:48 +0000995 Locates the first occurrence in the memory pointed to by MEM of the
Guido van Rossum101923b1997-04-02 06:11:18 +0000996 contents of memory pointed to by PAT. Returns the index into MEM if
997 found, or -1 if not found. If len of PAT is greater than length of
Guido van Rossum983c9301997-12-29 19:52:29 +0000998 MEM, the function returns -1.
Guido van Rossum101923b1997-04-02 06:11:18 +0000999*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001000static int
Tim Peters4cd44ef2001-05-10 00:05:33 +00001001mymemfind(const char *mem, int len, const char *pat, int pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001002{
1003 register int ii;
1004
1005 /* pattern can not occur in the last pat_len-1 chars */
1006 len -= pat_len;
1007
1008 for (ii = 0; ii <= len; ii++) {
1009 if (mem[ii] == pat[0] &&
1010 (pat_len == 1 ||
1011 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1012 return ii;
1013 }
1014 }
1015 return -1;
1016}
1017
1018/*
1019 mymemcnt
1020
1021 Return the number of distinct times PAT is found in MEM.
1022 meaning mem=1111 and pat==11 returns 2.
1023 mem=11111 and pat==11 also return 2.
1024 */
Tim Peters0f8b4942001-05-09 22:15:03 +00001025static int
Tim Peters4cd44ef2001-05-10 00:05:33 +00001026mymemcnt(const char *mem, int len, const char *pat, int pat_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001027{
1028 register int offset = 0;
1029 int nfound = 0;
1030
1031 while (len >= 0) {
1032 offset = mymemfind(mem, len, pat, pat_len);
1033 if (offset == -1)
1034 break;
1035 mem += offset + pat_len;
1036 len -= offset + pat_len;
1037 nfound++;
1038 }
1039 return nfound;
1040}
1041
Guido van Rossum983c9301997-12-29 19:52:29 +00001042/*
Guido van Rossum101923b1997-04-02 06:11:18 +00001043 mymemreplace
1044
Thomas Wouters7e474022000-07-16 12:04:32 +00001045 Return a string in which all occurrences of PAT in memory STR are
Guido van Rossum983c9301997-12-29 19:52:29 +00001046 replaced with SUB.
Guido van Rossum101923b1997-04-02 06:11:18 +00001047
Thomas Wouters7e474022000-07-16 12:04:32 +00001048 If length of PAT is less than length of STR or there are no occurrences
Guido van Rossum101923b1997-04-02 06:11:18 +00001049 of PAT in STR, then the original string is returned. Otherwise, a new
1050 string is allocated here and returned.
Guido van Rossum983c9301997-12-29 19:52:29 +00001051
Guido van Rossum101923b1997-04-02 06:11:18 +00001052 on return, out_len is:
1053 the length of output string, or
1054 -1 if the input string is returned, or
1055 unchanged if an error occurs (no memory).
1056
1057 return value is:
1058 the new string allocated locally, or
1059 NULL if an error occurred.
1060*/
Tim Peters0f8b4942001-05-09 22:15:03 +00001061static char *
Tim Peters4cd44ef2001-05-10 00:05:33 +00001062mymemreplace(const char *str, int len, /* input string */
1063 const char *pat, int pat_len, /* pattern string to find */
1064 const char *sub, int sub_len, /* substitution string */
1065 int count, /* number of replacements */
1066 int *out_len)
Guido van Rossum101923b1997-04-02 06:11:18 +00001067{
1068 char *out_s;
1069 char *new_s;
1070 int nfound, offset, new_len;
1071
1072 if (len == 0 || pat_len > len)
1073 goto return_same;
1074
1075 /* find length of output string */
1076 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters9c012af2001-05-10 00:32:57 +00001077 if (count < 0)
1078 count = INT_MAX;
1079 else if (nfound > count)
1080 nfound = count;
Guido van Rossum101923b1997-04-02 06:11:18 +00001081 if (nfound == 0)
1082 goto return_same;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001083
Guido van Rossum101923b1997-04-02 06:11:18 +00001084 new_len = len + nfound*(sub_len - pat_len);
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001085 if (new_len == 0) {
Tim Peters4cd44ef2001-05-10 00:05:33 +00001086 /* Have to allocate something for the caller to free(). */
1087 out_s = (char *)PyMem_MALLOC(1);
Tim Peters9c012af2001-05-10 00:32:57 +00001088 if (out_s == NULL)
Tim Peters4cd44ef2001-05-10 00:05:33 +00001089 return NULL;
1090 out_s[0] = '\0';
Guido van Rossum101923b1997-04-02 06:11:18 +00001091 }
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001092 else {
1093 assert(new_len > 0);
1094 new_s = (char *)PyMem_MALLOC(new_len);
1095 if (new_s == NULL)
1096 return NULL;
1097 out_s = new_s;
1098
Tim Peters9c012af2001-05-10 00:32:57 +00001099 for (; count > 0 && len > 0; --count) {
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001100 /* find index of next instance of pattern */
1101 offset = mymemfind(str, len, pat, pat_len);
1102 if (offset == -1)
1103 break;
1104
1105 /* copy non matching part of input string */
1106 memcpy(new_s, str, offset);
1107 str += offset + pat_len;
1108 len -= offset + pat_len;
1109
1110 /* copy substitute into the output string */
1111 new_s += offset;
1112 memcpy(new_s, sub, sub_len);
1113 new_s += sub_len;
Tim Peters1a7b3ee2001-05-09 23:00:26 +00001114 }
1115 /* copy any remaining values into output string */
1116 if (len > 0)
1117 memcpy(new_s, str, len);
1118 }
1119 *out_len = new_len;
Guido van Rossum101923b1997-04-02 06:11:18 +00001120 return out_s;
1121
1122 return_same:
1123 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001124 return (char *)str; /* cast away const */
Guido van Rossum101923b1997-04-02 06:11:18 +00001125}
1126
1127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001128PyDoc_STRVAR(replace__doc__,
Tim Peters0f8b4942001-05-09 22:15:03 +00001129"replace (str, old, new[, maxsplit]) -> string\n"
1130"\n"
1131"Return a copy of string str with all occurrences of substring\n"
1132"old replaced by new. If the optional argument maxsplit is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133"given, only the first maxsplit occurrences are replaced.");
Guido van Rossum983c9301997-12-29 19:52:29 +00001134
1135static PyObject *
Peter Schneider-Kamp8235f1c2000-07-10 09:43:24 +00001136strop_replace(PyObject *self, PyObject *args)
Guido van Rossum101923b1997-04-02 06:11:18 +00001137{
1138 char *str, *pat,*sub,*new_s;
1139 int len,pat_len,sub_len,out_len;
Tim Petersda45d552001-05-10 00:59:45 +00001140 int count = -1;
Guido van Rossum101923b1997-04-02 06:11:18 +00001141 PyObject *new;
1142
Guido van Rossum2e0a6542001-05-15 02:14:44 +00001143 WARN;
Guido van Rossum43713e52000-02-29 13:59:29 +00001144 if (!PyArg_ParseTuple(args, "t#t#t#|i:replace",
Barry Warsawf577c081997-11-29 00:10:07 +00001145 &str, &len, &pat, &pat_len, &sub, &sub_len,
1146 &count))
Guido van Rossum101923b1997-04-02 06:11:18 +00001147 return NULL;
Guido van Rossum4ccda151998-05-14 02:36:29 +00001148 if (pat_len <= 0) {
1149 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1150 return NULL;
1151 }
Tim Peters1ee77d92001-05-10 01:23:39 +00001152 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1153 * current (2.1) string.py and string methods. Preserve this for
1154 * ... well, hard to say for what <wink>.
1155 */
1156 if (count == 0)
1157 count = -1;
Barry Warsawf577c081997-11-29 00:10:07 +00001158 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
Guido van Rossum101923b1997-04-02 06:11:18 +00001159 if (new_s == NULL) {
1160 PyErr_NoMemory();
1161 return NULL;
1162 }
1163 if (out_len == -1) {
1164 /* we're returning another reference to the input string */
1165 new = PyTuple_GetItem(args, 0);
1166 Py_XINCREF(new);
1167 }
1168 else {
1169 new = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001170 PyMem_FREE(new_s);
Guido van Rossum101923b1997-04-02 06:11:18 +00001171 }
1172 return new;
1173}
1174
1175
Guido van Rossume270b431992-09-03 20:21:07 +00001176/* List of functions defined in the module */
1177
Barry Warsawe8fc29c1997-01-03 22:45:34 +00001178static PyMethodDef
1179strop_methods[] = {
Tim Peters0f8b4942001-05-09 22:15:03 +00001180 {"atof", strop_atof, METH_VARARGS, atof__doc__},
1181 {"atoi", strop_atoi, METH_VARARGS, atoi__doc__},
1182 {"atol", strop_atol, METH_VARARGS, atol__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001183 {"capitalize", strop_capitalize, METH_O, capitalize__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001184 {"count", strop_count, METH_VARARGS, count__doc__},
1185 {"expandtabs", strop_expandtabs, METH_VARARGS, expandtabs__doc__},
1186 {"find", strop_find, METH_VARARGS, find__doc__},
1187 {"join", strop_joinfields, METH_VARARGS, joinfields__doc__},
1188 {"joinfields", strop_joinfields, METH_VARARGS, joinfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001189 {"lstrip", strop_lstrip, METH_O, lstrip__doc__},
1190 {"lower", strop_lower, METH_O, lower__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001191 {"maketrans", strop_maketrans, METH_VARARGS, maketrans__doc__},
1192 {"replace", strop_replace, METH_VARARGS, replace__doc__},
1193 {"rfind", strop_rfind, METH_VARARGS, rfind__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001194 {"rstrip", strop_rstrip, METH_O, rstrip__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001195 {"split", strop_splitfields, METH_VARARGS, splitfields__doc__},
1196 {"splitfields", strop_splitfields, METH_VARARGS, splitfields__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001197 {"strip", strop_strip, METH_O, strip__doc__},
1198 {"swapcase", strop_swapcase, METH_O, swapcase__doc__},
Tim Peters0f8b4942001-05-09 22:15:03 +00001199 {"translate", strop_translate, METH_VARARGS, translate__doc__},
Neal Norwitz187ae562002-04-02 18:17:57 +00001200 {"upper", strop_upper, METH_O, upper__doc__},
Guido van Rossume270b431992-09-03 20:21:07 +00001201 {NULL, NULL} /* sentinel */
1202};
1203
1204
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001205PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001206initstrop(void)
Guido van Rossume270b431992-09-03 20:21:07 +00001207{
Fred Drake4baedc12002-04-01 14:53:37 +00001208 PyObject *m, *s;
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001209 char buf[256];
1210 int c, n;
Guido van Rossum983c9301997-12-29 19:52:29 +00001211 m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
1212 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossume22e6441993-07-09 10:51:31 +00001213
1214 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001215 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001216 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +00001217 if (isspace(c))
1218 buf[n++] = c;
1219 }
Barry Warsawf5256011996-12-09 18:35:56 +00001220 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001221 if (s)
1222 PyModule_AddObject(m, "whitespace", s);
1223
Guido van Rossume22e6441993-07-09 10:51:31 +00001224 /* Create 'lowercase' object */
1225 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001226 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001227 if (islower(c))
1228 buf[n++] = c;
1229 }
Barry Warsawf5256011996-12-09 18:35:56 +00001230 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001231 if (s)
1232 PyModule_AddObject(m, "lowercase", s);
Guido van Rossume22e6441993-07-09 10:51:31 +00001233
1234 /* Create 'uppercase' object */
1235 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001236 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +00001237 if (isupper(c))
1238 buf[n++] = c;
1239 }
Barry Warsawf5256011996-12-09 18:35:56 +00001240 s = PyString_FromStringAndSize(buf, n);
Fred Drake4baedc12002-04-01 14:53:37 +00001241 if (s)
1242 PyModule_AddObject(m, "uppercase", s);
Guido van Rossume270b431992-09-03 20:21:07 +00001243}