blob: 7e44e0ee6320e364f32abcfb5dd7dfc698fb5b95 [file] [log] [blame]
Guido van Rossume270b431992-09-03 20:21:07 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossume270b431992-09-03 20:21:07 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* strop module */
26
27#include "allobjects.h"
28#include "modsupport.h"
29
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000030#include <ctype.h>
Guido van Rossume22e6441993-07-09 10:51:31 +000031/* XXX This file assumes that the <ctype.h> is*() functions
32 XXX are defined for all 8-bit characters! */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +000033
Guido van Rossumb6775db1994-08-01 11:34:53 +000034#include <errno.h>
35
Guido van Rossume270b431992-09-03 20:21:07 +000036
37static object *
Guido van Rossum009e79b1995-05-03 17:40:23 +000038split_whitespace(s, len)
Guido van Rossume270b431992-09-03 20:21:07 +000039 char *s;
Guido van Rossum009e79b1995-05-03 17:40:23 +000040 int len;
41{
42 int i, j, err;
Guido van Rossume270b431992-09-03 20:21:07 +000043 object *list, *item;
44
Guido van Rossume270b431992-09-03 20:21:07 +000045 list = newlistobject(0);
46 if (list == NULL)
47 return NULL;
48
49 i = 0;
50 while (i < len) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +000051 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000052 i = i+1;
53 }
54 j = i;
Guido van Rossumee1813d1995-02-14 00:58:59 +000055 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +000056 i = i+1;
57 }
58 if (j < i) {
59 item = newsizedstringobject(s+j, (int)(i-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +000060 if (item == NULL) {
61 DECREF(list);
62 return NULL;
63 }
64 err = addlistitem(list, item);
65 DECREF(item);
66 if (err < 0) {
Guido van Rossume270b431992-09-03 20:21:07 +000067 DECREF(list);
68 return NULL;
69 }
70 }
71 }
72
73 return list;
74}
75
76
77static object *
78strop_splitfields(self, args)
79 object *self; /* Not used */
80 object *args;
81{
Guido van Rossum572d2d91993-11-05 10:14:49 +000082 int len, n, i, j, err;
Guido van Rossume270b431992-09-03 20:21:07 +000083 char *s, *sub;
Guido van Rossume270b431992-09-03 20:21:07 +000084 object *list, *item;
85
Guido van Rossum009e79b1995-05-03 17:40:23 +000086 sub = NULL;
87 n = 0;
88 if (!newgetargs(args, "s#|z#", &s, &len, &sub, &n))
Guido van Rossume270b431992-09-03 20:21:07 +000089 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +000090 if (sub == NULL)
91 return split_whitespace(s, len);
Guido van Rossume270b431992-09-03 20:21:07 +000092 if (n == 0) {
93 err_setstr(ValueError, "empty separator");
94 return NULL;
95 }
96
97 list = newlistobject(0);
98 if (list == NULL)
99 return NULL;
100
101 i = j = 0;
102 while (i+n <= len) {
103 if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
104 item = newsizedstringobject(s+j, (int)(i-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000105 if (item == NULL)
106 goto fail;
107 err = addlistitem(list, item);
108 DECREF(item);
109 if (err < 0)
110 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000111 i = j = i + n;
112 }
113 else
114 i++;
115 }
116 item = newsizedstringobject(s+j, (int)(len-j));
Guido van Rossum572d2d91993-11-05 10:14:49 +0000117 if (item == NULL)
118 goto fail;
119 err = addlistitem(list, item);
120 DECREF(item);
121 if (err < 0)
122 goto fail;
Guido van Rossume270b431992-09-03 20:21:07 +0000123
124 return list;
Guido van Rossum572d2d91993-11-05 10:14:49 +0000125
126 fail:
127 DECREF(list);
128 return NULL;
Guido van Rossume270b431992-09-03 20:21:07 +0000129}
130
131
132static object *
Guido van Rossumc89705d1992-11-26 08:54:07 +0000133strop_joinfields(self, args)
134 object *self; /* Not used */
135 object *args;
136{
137 object *seq, *item, *res;
138 object * (*getitem) FPROTO((object *, int));
139 char *sep, *p;
140 int seplen, seqlen, reslen, itemlen, i;
141
Guido van Rossum009e79b1995-05-03 17:40:23 +0000142 sep = NULL;
143 seplen = 0;
144 if (!newgetargs(args, "O|s#", &seq, &sep, &seplen))
Guido van Rossumc89705d1992-11-26 08:54:07 +0000145 return NULL;
Guido van Rossum009e79b1995-05-03 17:40:23 +0000146 if (sep == NULL) {
147 sep = " ";
148 seplen = 1;
149 }
Guido van Rossumc89705d1992-11-26 08:54:07 +0000150 if (is_listobject(seq)) {
151 getitem = getlistitem;
152 seqlen = getlistsize(seq);
153 }
154 else if (is_tupleobject(seq)) {
155 getitem = gettupleitem;
156 seqlen = gettuplesize(seq);
157 }
158 else {
159 err_setstr(TypeError, "first argument must be list/tuple");
160 return NULL;
161 }
162 reslen = 0;
163 for (i = 0; i < seqlen; i++) {
164 item = getitem(seq, i);
165 if (!is_stringobject(item)) {
166 err_setstr(TypeError,
167 "first argument must be list/tuple of strings");
168 return NULL;
169 }
170 if (i > 0)
171 reslen = reslen + seplen;
172 reslen = reslen + getstringsize(item);
173 }
174 if (seqlen == 1) {
175 /* Optimization if there's only one item */
176 item = getitem(seq, 0);
177 INCREF(item);
178 return item;
179 }
180 res = newsizedstringobject((char *)NULL, reslen);
181 if (res == NULL)
182 return NULL;
183 p = getstringvalue(res);
184 for (i = 0; i < seqlen; i++) {
185 item = getitem(seq, i);
186 if (i > 0) {
187 memcpy(p, sep, seplen);
188 p += seplen;
189 }
190 itemlen = getstringsize(item);
191 memcpy(p, getstringvalue(item), itemlen);
192 p += itemlen;
193 }
194 if (p != getstringvalue(res) + reslen) {
195 err_setstr(SystemError, "strop.joinfields: assertion failed");
196 return NULL;
197 }
198 return res;
199}
200
201
202static object *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000203strop_find(self, args)
Guido van Rossume270b431992-09-03 20:21:07 +0000204 object *self; /* Not used */
205 object *args;
206{
207 char *s, *sub;
208 int len, n, i;
209
210 if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
Guido van Rossumee9012f1993-10-26 15:23:55 +0000211 if (i < 0)
212 i += len;
Guido van Rossum602099a1994-09-14 13:32:22 +0000213 if (i < 0)
214 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000215 }
216 else {
217 err_clear();
218 if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
219 return NULL;
220 i = 0;
221 }
222
223 if (n == 0)
224 return newintobject((long)i);
225
226 len -= n;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000227 for (; i <= len; ++i)
228 if (s[i] == sub[0] &&
229 (n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
230 return newintobject((long)i);
231
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000232 return newintobject(-1L);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000233}
234
235
236static object *
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000237strop_rfind(self, args)
Guido van Rossumee9012f1993-10-26 15:23:55 +0000238 object *self; /* Not used */
239 object *args;
240{
241 char *s, *sub;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242 int len, n, i, j;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000243
244 if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
245 if (i < 0)
246 i += len;
Guido van Rossum602099a1994-09-14 13:32:22 +0000247 if (i < 0)
248 i = 0;
Guido van Rossume270b431992-09-03 20:21:07 +0000249 }
Guido van Rossumee9012f1993-10-26 15:23:55 +0000250 else {
251 err_clear();
252 if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
253 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254 i = 0;
Guido van Rossumee9012f1993-10-26 15:23:55 +0000255 }
256
257 if (n == 0)
Guido van Rossumc65a5251994-08-05 13:44:50 +0000258 return newintobject((long)len);
Guido van Rossumee9012f1993-10-26 15:23:55 +0000259
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260 for (j = len-n; j >= i; --j)
261 if (s[j] == sub[0] &&
262 (n == 1 || strncmp(&s[j+1], &sub[1], n-1) == 0))
263 return newintobject((long)j);
Guido van Rossume270b431992-09-03 20:21:07 +0000264
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000265 return newintobject(-1L);
Guido van Rossume270b431992-09-03 20:21:07 +0000266}
267
268
269static object *
270strop_strip(self, args)
271 object *self; /* Not used */
272 object *args;
273{
274 char *s;
275 int len, i, j;
Guido van Rossume270b431992-09-03 20:21:07 +0000276
277 if (!getargs(args, "s#", &s, &len))
278 return NULL;
279
280 i = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000281 while (i < len && isspace(Py_CHARMASK(s[i]))) {
Guido van Rossume270b431992-09-03 20:21:07 +0000282 i++;
283 }
284
285 j = len;
286 do {
287 j--;
Guido van Rossumee1813d1995-02-14 00:58:59 +0000288 } while (j >= i && isspace(Py_CHARMASK(s[j])));
Guido van Rossume270b431992-09-03 20:21:07 +0000289 j++;
290
291 if (i == 0 && j == len) {
292 INCREF(args);
293 return args;
294 }
295 else
296 return newsizedstringobject(s+i, j-i);
297}
298
299
Guido van Rossum5c850621992-09-11 23:55:51 +0000300static object *
301strop_lower(self, args)
302 object *self; /* Not used */
303 object *args;
304{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000305 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000306 int i, n;
307 object *new;
308 int changed;
309
310 if (!getargs(args, "s#", &s, &n))
311 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000312 new = newsizedstringobject(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000313 if (new == NULL)
314 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000315 s_new = getstringvalue(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000316 changed = 0;
317 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000318 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000319 if (isupper(c)) {
320 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000321 *s_new = tolower(c);
322 } else
323 *s_new = c;
324 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000325 }
326 if (!changed) {
327 DECREF(new);
328 INCREF(args);
329 return args;
330 }
331 return new;
332}
333
334
335static object *
336strop_upper(self, args)
337 object *self; /* Not used */
338 object *args;
339{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000340 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000341 int i, n;
342 object *new;
343 int changed;
344
345 if (!getargs(args, "s#", &s, &n))
346 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000347 new = newsizedstringobject(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000348 if (new == NULL)
349 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000350 s_new = getstringvalue(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000351 changed = 0;
352 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000353 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000354 if (islower(c)) {
355 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000356 *s_new = toupper(c);
357 } else
358 *s_new = c;
359 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000360 }
361 if (!changed) {
362 DECREF(new);
363 INCREF(args);
364 return args;
365 }
366 return new;
367}
368
369
370static object *
Guido van Rossum27457531996-06-12 04:24:52 +0000371strop_capitalize(self, args)
372 object *self; /* Not used */
373 object *args;
374{
375 char *s, *s_new;
376 int i, n;
377 object *new;
378 int changed;
379
380 if (!getargs(args, "s#", &s, &n))
381 return NULL;
382 new = newsizedstringobject(NULL, n);
383 if (new == NULL)
384 return NULL;
385 s_new = getstringvalue(new);
386 changed = 0;
Guido van Rossum529c9631996-06-17 16:59:33 +0000387 if (0 < n) {
Guido van Rossum27457531996-06-12 04:24:52 +0000388 int c = Py_CHARMASK(*s++);
389 if (islower(c)) {
390 changed = 1;
391 *s_new = toupper(c);
392 } else
393 *s_new = c;
394 s_new++;
395 }
396 for (i = 1; i < n; i++) {
397 int c = Py_CHARMASK(*s++);
398 if (isupper(c)) {
399 changed = 1;
400 *s_new = tolower(c);
401 } else
402 *s_new = c;
403 s_new++;
404 }
405 if (!changed) {
406 DECREF(new);
407 INCREF(args);
408 return args;
409 }
410 return new;
411}
412
413
414static object *
Guido van Rossum5c850621992-09-11 23:55:51 +0000415strop_swapcase(self, args)
416 object *self; /* Not used */
417 object *args;
418{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000419 char *s, *s_new;
Guido van Rossum5c850621992-09-11 23:55:51 +0000420 int i, n;
421 object *new;
422 int changed;
423
424 if (!getargs(args, "s#", &s, &n))
425 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000426 new = newsizedstringobject(NULL, n);
Guido van Rossum5c850621992-09-11 23:55:51 +0000427 if (new == NULL)
428 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000429 s_new = getstringvalue(new);
Guido van Rossum5c850621992-09-11 23:55:51 +0000430 changed = 0;
431 for (i = 0; i < n; i++) {
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000432 int c = Py_CHARMASK(*s++);
Guido van Rossum5c850621992-09-11 23:55:51 +0000433 if (islower(c)) {
434 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000435 *s_new = toupper(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000436 }
437 else if (isupper(c)) {
438 changed = 1;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000439 *s_new = tolower(c);
Guido van Rossum5c850621992-09-11 23:55:51 +0000440 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000441 else
442 *s_new = c;
443 s_new++;
Guido van Rossum5c850621992-09-11 23:55:51 +0000444 }
445 if (!changed) {
446 DECREF(new);
447 INCREF(args);
448 return args;
449 }
450 return new;
451}
452
453
Guido van Rossumb6775db1994-08-01 11:34:53 +0000454static object *
455strop_atoi(self, args)
456 object *self; /* Not used */
457 object *args;
458{
459 extern long mystrtol PROTO((const char *, char **, int));
460 extern unsigned long mystrtoul PROTO((const char *, char **, int));
461 char *s, *end;
462 int base = 10;
463 long x;
464
465 if (args != NULL && is_tupleobject(args)) {
466 if (!getargs(args, "(si)", &s, &base))
467 return NULL;
468 if (base != 0 && base < 2 || base > 36) {
469 err_setstr(ValueError, "invalid base for atoi()");
470 return NULL;
471 }
472 }
473 else if (!getargs(args, "s", &s))
474 return NULL;
475 errno = 0;
476 if (base == 0 && s[0] == '0')
477 x = (long) mystrtoul(s, &end, base);
478 else
479 x = mystrtol(s, &end, base);
480 if (*end != '\0') {
481 err_setstr(ValueError, "invalid literal for atoi()");
482 return NULL;
483 }
484 else if (errno != 0) {
485 err_setstr(OverflowError, "atoi() literal too large");
486 return NULL;
487 }
488 return newintobject(x);
489}
490
491
492static object *
493strop_atol(self, args)
494 object *self; /* Not used */
495 object *args;
496{
497 char *s, *end;
498 int base = 10;
499 object *x;
500
501 if (args != NULL && is_tupleobject(args)) {
502 if (!getargs(args, "(si)", &s, &base))
503 return NULL;
504 if (base != 0 && base < 2 || base > 36) {
505 err_setstr(ValueError, "invalid base for atol()");
506 return NULL;
507 }
508 }
509 else if (!getargs(args, "s", &s))
510 return NULL;
511 x = long_escan(s, &end, base);
512 if (x == NULL)
513 return NULL;
514 if (base == 0 && (*end == 'l' || *end == 'L'))
515 end++;
516 if (*end != '\0') {
517 err_setstr(ValueError, "invalid literal for atol()");
518 DECREF(x);
519 return NULL;
520 }
521 return x;
522}
523
524
525static object *
526strop_atof(self, args)
527 object *self; /* Not used */
528 object *args;
529{
530 extern double strtod PROTO((const char *, char **));
531 char *s, *end;
532 double x;
533
534 if (!getargs(args, "s", &s))
535 return NULL;
536 errno = 0;
537 x = strtod(s, &end);
538 if (*end != '\0') {
539 err_setstr(ValueError, "invalid literal for atof()");
540 return NULL;
541 }
542 else if (errno != 0) {
543 err_setstr(OverflowError, "atof() literal too large");
544 return NULL;
545 }
546 return newfloatobject(x);
547}
548
549
Guido van Rossumed7253c1996-07-23 18:12:39 +0000550static PyObject *
551strop_maketrans(self, args)
552 PyObject *self; /* Not used */
553 PyObject *args;
554{
555 unsigned char c[256], *from=NULL, *to=NULL;
556 int i, fromlen=0, tolen=0;
557
558 if (PyTuple_Size(args)!=0) {
559 if (!PyArg_ParseTuple(args, "s#s#", &from, &fromlen,
560 &to, &tolen))
561 return NULL;
562 }
563
564 if (fromlen!=tolen) {
565 PyErr_SetString(ValueError,
566 "maketrans arguments must have same length");
567 return NULL;
568 }
569 for(i=0; i<256; i++)
570 c[i]=(unsigned char)i;
571 for(i=0; i<fromlen; i++) {
572 c[from[i]]=to[i];
573 }
574 return PyString_FromStringAndSize((char *)c, 256);
575}
576
577
Guido van Rossuma3127e81995-09-13 17:39:06 +0000578static object *
579strop_translate(self, args)
580 object *self;
581 object *args;
582{
Guido van Rossumed7253c1996-07-23 18:12:39 +0000583 char *input, *table, *output, *output_start, *delete=NULL;
584 int inlen, tablen, dellen;
585 PyObject *result;
586 int i, trans_table[256];
Guido van Rossuma3127e81995-09-13 17:39:06 +0000587
Guido van Rossumed7253c1996-07-23 18:12:39 +0000588 if (!PyArg_ParseTuple(args, "s#s#|s#", &input, &inlen,
589 &table, &tablen, &delete, &dellen))
Guido van Rossuma3127e81995-09-13 17:39:06 +0000590 return NULL;
591 if (tablen != 256) {
Guido van Rossumed7253c1996-07-23 18:12:39 +0000592 PyErr_SetString(ValueError,
Guido van Rossuma3127e81995-09-13 17:39:06 +0000593 "translation table must be 256 characters long");
594 return NULL;
595 }
Guido van Rossumed7253c1996-07-23 18:12:39 +0000596 for(i=0; i<256; i++)
597 trans_table[i]=Py_CHARMASK(table[i]);
598 if (delete!=NULL) {
599 for(i=0; i<dellen; i++)
600 trans_table[delete[i]]=-1;
601 }
602
603 result = PyString_FromStringAndSize((char *)NULL, inlen);
Guido van Rossuma3127e81995-09-13 17:39:06 +0000604 if (result == NULL)
605 return NULL;
Guido van Rossumed7253c1996-07-23 18:12:39 +0000606 output_start = output = PyString_AsString(result);
607 if (delete!=NULL && dellen!=0) {
608 for (i = 0; i < inlen; i++) {
609 int c = Py_CHARMASK(*input++);
610 if (trans_table[c]!=-1)
611 *output++ = (char)trans_table[c];
612 }
613 /* Fix the size of the resulting string */
614 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
615 return NULL;
616 } else {
617 /* If no deletions are required, use a faster loop */
618 for (i = 0; i < inlen; i++) {
619 int c = Py_CHARMASK(*input++);
620 *output++ = (char)trans_table[c];
621 }
Guido van Rossuma3127e81995-09-13 17:39:06 +0000622 }
623 return result;
624}
625
626
Guido van Rossume270b431992-09-03 20:21:07 +0000627/* List of functions defined in the module */
628
629static struct methodlist strop_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000630 {"atof", strop_atof},
631 {"atoi", strop_atoi},
632 {"atol", strop_atol},
Guido van Rossum27457531996-06-12 04:24:52 +0000633 {"capitalize", strop_capitalize},
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000634 {"find", strop_find},
Guido van Rossum009e79b1995-05-03 17:40:23 +0000635 {"join", strop_joinfields, 1},
636 {"joinfields", strop_joinfields, 1},
Guido van Rossum5c850621992-09-11 23:55:51 +0000637 {"lower", strop_lower},
Guido van Rossum5806a4f1994-08-17 13:15:46 +0000638 {"rfind", strop_rfind},
Guido van Rossum009e79b1995-05-03 17:40:23 +0000639 {"split", strop_splitfields, 1},
640 {"splitfields", strop_splitfields, 1},
Guido van Rossume270b431992-09-03 20:21:07 +0000641 {"strip", strop_strip},
Guido van Rossum5c850621992-09-11 23:55:51 +0000642 {"swapcase", strop_swapcase},
Guido van Rossumed7253c1996-07-23 18:12:39 +0000643 {"maketrans", strop_maketrans, 1},
Guido van Rossuma3127e81995-09-13 17:39:06 +0000644 {"translate", strop_translate, 1},
Guido van Rossum5c850621992-09-11 23:55:51 +0000645 {"upper", strop_upper},
Guido van Rossume270b431992-09-03 20:21:07 +0000646 {NULL, NULL} /* sentinel */
647};
648
649
Guido van Rossume270b431992-09-03 20:21:07 +0000650void
651initstrop()
652{
Guido van Rossumd05eb8b1993-07-08 11:12:36 +0000653 object *m, *d, *s;
654 char buf[256];
655 int c, n;
656 m = initmodule("strop", strop_methods);
657 d = getmoduledict(m);
Guido van Rossume22e6441993-07-09 10:51:31 +0000658
659 /* Create 'whitespace' object */
Guido van Rossumd05eb8b1993-07-08 11:12:36 +0000660 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000661 for (c = 0; c < 256; c++) {
Guido van Rossumd05eb8b1993-07-08 11:12:36 +0000662 if (isspace(c))
663 buf[n++] = c;
664 }
Sjoerd Mullenderd96ec441993-09-14 08:37:39 +0000665 s = newsizedstringobject(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +0000666 if (s) {
Guido van Rossume22e6441993-07-09 10:51:31 +0000667 dictinsert(d, "whitespace", s);
668 DECREF(s);
669 }
670 /* Create 'lowercase' object */
671 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000672 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +0000673 if (islower(c))
674 buf[n++] = c;
675 }
Guido van Rossumd05eb8b1993-07-08 11:12:36 +0000676 s = newsizedstringobject(buf, n);
Guido van Rossume22e6441993-07-09 10:51:31 +0000677 if (s) {
678 dictinsert(d, "lowercase", s);
679 DECREF(s);
680 }
681
682 /* Create 'uppercase' object */
683 n = 0;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000684 for (c = 0; c < 256; c++) {
Guido van Rossume22e6441993-07-09 10:51:31 +0000685 if (isupper(c))
686 buf[n++] = c;
687 }
688 s = newsizedstringobject(buf, n);
689 if (s) {
690 dictinsert(d, "uppercase", s);
691 DECREF(s);
692 }
693
694 if (err_occurred())
695 fatal("can't initialize module strop");
Guido van Rossume270b431992-09-03 20:21:07 +0000696}