1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
34 static char strop_module__doc__
[] =
35 "Common string manipulations, optimized for speed.\n\
37 Always use \"import string\" rather than referencing\n\
38 this module directly.";
45 #define INT_MAX 2147483647
49 /* XXX This file assumes that the <ctype.h> is*() functions
50 XXX are defined for all 8-bit characters! */
52 /* The lstrip(), rstrip() and strip() functions are implemented
53 in do_strip(), which uses an additional parameter to indicate what
54 type of strip should occur. */
62 split_whitespace(s
, len
, maxsplit
)
70 PyObject
*list
= PyList_New(0);
76 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
80 while (i
< len
&& !isspace(Py_CHARMASK(s
[i
]))) {
84 item
= PyString_FromStringAndSize(s
+j
, (int)(i
-j
));
88 err
= PyList_Append(list
, item
);
94 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
97 if (maxsplit
&& (countsplit
>= maxsplit
) && i
< len
) {
98 item
= PyString_FromStringAndSize(
103 err
= PyList_Append(list
, item
);
119 static char splitfields__doc__
[] =
120 "split(str [,sep [,maxsplit]]) -> list of strings\n\
121 splitfields(str [,sep [,maxsplit]]) -> list of strings\n\
123 Return a list of the words in the string s, using sep as the\n\
124 delimiter string. If maxsplit is nonzero, splits into at most\n\
125 maxsplit words If sep is not specified, any whitespace string\n\
126 is a separator. Maxsplit defaults to 0.\n\
128 (split and splitfields are synonymous)";
131 strop_splitfields(self
, args
)
132 PyObject
*self
; /* Not used */
135 int len
, n
, i
, j
, err
;
136 int splitcount
, maxsplit
;
138 PyObject
*list
, *item
;
144 if (!PyArg_ParseTuple(args
, "s#|z#i", &s
, &len
, &sub
, &n
, &maxsplit
))
147 return split_whitespace(s
, len
, maxsplit
);
149 PyErr_SetString(PyExc_ValueError
, "empty separator");
153 list
= PyList_New(0);
159 if (s
[i
] == sub
[0] && (n
== 1 || memcmp(s
+i
, sub
, n
) == 0)) {
160 item
= PyString_FromStringAndSize(s
+j
, (int)(i
-j
));
163 err
= PyList_Append(list
, item
);
169 if (maxsplit
&& (splitcount
>= maxsplit
))
175 item
= PyString_FromStringAndSize(s
+j
, (int)(len
-j
));
178 err
= PyList_Append(list
, item
);
191 static char joinfields__doc__
[] =
192 "join(list [,sep]) -> string\n\
193 joinfields(list [,sep]) -> string\n\
195 Return a string composed of the words in list, with\n\
196 intervening occurences of sep. Sep defaults to a single\n\
199 (join and joinfields are synonymous)";
202 strop_joinfields(self
, args
)
203 PyObject
*self
; /* Not used */
208 int seqlen
, seplen
= 0;
209 int i
, reslen
= 0, slen
= 0, sz
= 100;
210 PyObject
*res
= NULL
;
212 intargfunc getitemfunc
;
214 if (!PyArg_ParseTuple(args
, "O|s#", &seq
, &sep
, &seplen
))
221 seqlen
= PySequence_Length(seq
);
222 if (seqlen
< 0 && PyErr_Occurred())
226 /* Optimization if there's only one item */
227 PyObject
*item
= PySequence_GetItem(seq
, 0);
228 if (item
&& !PyString_Check(item
)) {
229 PyErr_SetString(PyExc_TypeError
,
230 "first argument must be sequence of strings");
236 if (!(res
= PyString_FromStringAndSize((char*)NULL
, sz
)))
238 p
= PyString_AsString(res
);
240 /* optimize for lists, since it's the most common case. all others
241 * (tuples and arbitrary sequences) just use the sequence abstract
244 if (PyList_Check(seq
)) {
245 for (i
= 0; i
< seqlen
; i
++) {
246 PyObject
*item
= PyList_GET_ITEM(seq
, i
);
247 if (!PyString_Check(item
)) {
248 PyErr_SetString(PyExc_TypeError
,
249 "first argument must be sequence of strings");
253 slen
= PyString_GET_SIZE(item
);
254 while (reslen
+ slen
+ seplen
>= sz
) {
255 if (_PyString_Resize(&res
, sz
* 2)) {
260 p
= PyString_AsString(res
) + reslen
;
263 memcpy(p
, sep
, seplen
);
267 memcpy(p
, PyString_AS_STRING(item
), slen
);
271 if (_PyString_Resize(&res
, reslen
)) {
278 if (seq
->ob_type
->tp_as_sequence
== NULL
||
279 (getitemfunc
= seq
->ob_type
->tp_as_sequence
->sq_item
) == NULL
)
281 PyErr_SetString(PyExc_TypeError
,
282 "first argument must be a sequence");
285 /* This is now type safe */
286 for (i
= 0; i
< seqlen
; i
++) {
287 PyObject
*item
= getitemfunc(seq
, i
);
288 if (!item
|| !PyString_Check(item
)) {
289 PyErr_SetString(PyExc_TypeError
,
290 "first argument must be sequence of strings");
295 slen
= PyString_GET_SIZE(item
);
296 while (reslen
+ slen
+ seplen
>= sz
) {
297 if (_PyString_Resize(&res
, sz
* 2)) {
303 p
= PyString_AsString(res
) + reslen
;
306 memcpy(p
, sep
, seplen
);
310 memcpy(p
, PyString_AS_STRING(item
), slen
);
315 if (_PyString_Resize(&res
, reslen
)) {
323 static char find__doc__
[] =
324 "find(s, sub [,start [,end]]) -> in\n\
326 Return the lowest index in s where substring sub is found,\n\
327 such that sub is contained within s[start,end]. Optional\n\
328 arguments start and end are interpreted as in slice notation.\n\
330 Return -1 on failure.";
333 strop_find(self
, args
)
334 PyObject
*self
; /* Not used */
338 int len
, n
, i
= 0, last
= INT_MAX
;
340 if (!PyArg_ParseTuple(args
, "s#s#|ii", &s
, &len
, &sub
, &n
, &i
, &last
))
354 if (n
== 0 && i
<= last
)
355 return PyInt_FromLong((long)i
);
358 for (; i
<= last
; ++i
)
359 if (s
[i
] == sub
[0] &&
360 (n
== 1 || memcmp(&s
[i
+1], &sub
[1], n
-1) == 0))
361 return PyInt_FromLong((long)i
);
363 return PyInt_FromLong(-1L);
367 static char rfind__doc__
[] =
368 "rfind(s, sub [,start [,end]]) -> int\n\
370 Return the highest index in s where substring sub is found,\n\
371 such that sub is contained within s[start,end]. Optional\n\
372 arguments start and end are interpreted as in slice notation.\n\
374 Return -1 on failure.";
377 strop_rfind(self
, args
)
378 PyObject
*self
; /* Not used */
383 int i
= 0, last
= INT_MAX
;
385 if (!PyArg_ParseTuple(args
, "s#s#|ii", &s
, &len
, &sub
, &n
, &i
, &last
))
399 if (n
== 0 && i
<= last
)
400 return PyInt_FromLong((long)last
);
402 for (j
= last
-n
; j
>= i
; --j
)
403 if (s
[j
] == sub
[0] &&
404 (n
== 1 || memcmp(&s
[j
+1], &sub
[1], n
-1) == 0))
405 return PyInt_FromLong((long)j
);
407 return PyInt_FromLong(-1L);
412 do_strip(args
, striptype
)
420 if (!PyArg_Parse(args
, "s#", &s
, &len
))
424 if (striptype
!= RIGHTSTRIP
) {
425 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
431 if (striptype
!= LEFTSTRIP
) {
434 } while (j
>= i
&& isspace(Py_CHARMASK(s
[j
])));
438 if (i
== 0 && j
== len
) {
443 return PyString_FromStringAndSize(s
+i
, j
-i
);
447 static char strip__doc__
[] =
448 "strip(s) -> string\n\
450 Return a copy of the string s with leading and trailing\n\
451 whitespace removed.";
454 strop_strip(self
, args
)
455 PyObject
*self
; /* Not used */
458 return do_strip(args
, BOTHSTRIP
);
462 static char lstrip__doc__
[] =
463 "lstrip(s) -> string\n\
465 Return a copy of the string s with leading whitespace removed.";
468 strop_lstrip(self
, args
)
469 PyObject
*self
; /* Not used */
472 return do_strip(args
, LEFTSTRIP
);
476 static char rstrip__doc__
[] =
477 "rstrip(s) -> string\n\
479 Return a copy of the string s with trailing whitespace removed.";
482 strop_rstrip(self
, args
)
483 PyObject
*self
; /* Not used */
486 return do_strip(args
, RIGHTSTRIP
);
490 static char lower__doc__
[] =
491 "lower(s) -> string\n\
493 Return a copy of the string s converted to lowercase.";
496 strop_lower(self
, args
)
497 PyObject
*self
; /* Not used */
505 if (!PyArg_Parse(args
, "s#", &s
, &n
))
507 new = PyString_FromStringAndSize(NULL
, n
);
510 s_new
= PyString_AsString(new);
512 for (i
= 0; i
< n
; i
++) {
513 int c
= Py_CHARMASK(*s
++);
530 static char upper__doc__
[] =
531 "upper(s) -> string\n\
533 Return a copy of the string s converted to uppercase.";
536 strop_upper(self
, args
)
537 PyObject
*self
; /* Not used */
545 if (!PyArg_Parse(args
, "s#", &s
, &n
))
547 new = PyString_FromStringAndSize(NULL
, n
);
550 s_new
= PyString_AsString(new);
552 for (i
= 0; i
< n
; i
++) {
553 int c
= Py_CHARMASK(*s
++);
570 static char capitalize__doc__
[] =
571 "capitalize(s) -> string\n\
573 Return a copy of the string s with only its first character\n\
577 strop_capitalize(self
, args
)
578 PyObject
*self
; /* Not used */
586 if (!PyArg_Parse(args
, "s#", &s
, &n
))
588 new = PyString_FromStringAndSize(NULL
, n
);
591 s_new
= PyString_AsString(new);
594 int c
= Py_CHARMASK(*s
++);
602 for (i
= 1; i
< n
; i
++) {
603 int c
= Py_CHARMASK(*s
++);
620 static char swapcase__doc__
[] =
621 "swapcase(s) -> string\n\
623 Return a copy of the string s with upper case characters\n\
624 converted to lowercase and vice versa.";
627 strop_swapcase(self
, args
)
628 PyObject
*self
; /* Not used */
636 if (!PyArg_Parse(args
, "s#", &s
, &n
))
638 new = PyString_FromStringAndSize(NULL
, n
);
641 s_new
= PyString_AsString(new);
643 for (i
= 0; i
< n
; i
++) {
644 int c
= Py_CHARMASK(*s
++);
649 else if (isupper(c
)) {
666 static char atoi__doc__
[] =
667 "atoi(s [,base]) -> int\n\
669 Return the integer represented by the string s in the given\n\
670 base, which defaults to 10. The string s must consist of one\n\
671 or more digits, possibly preceded by a sign. If base is 0, it\n\
672 is chosen from the leading characters of s, 0 for octal, 0x or\n\
673 0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n\
677 strop_atoi(self
, args
)
678 PyObject
*self
; /* Not used */
681 extern long PyOS_strtol
Py_PROTO((const char *, char **, int));
683 PyOS_strtoul
Py_PROTO((const char *, char **, int));
687 char buffer
[256]; /* For errors */
689 if (!PyArg_ParseTuple(args
, "s|i", &s
, &base
))
692 if ((base
!= 0 && base
< 2) || base
> 36) {
693 PyErr_SetString(PyExc_ValueError
, "invalid base for atoi()");
697 while (*s
&& isspace(Py_CHARMASK(*s
)))
700 if (base
== 0 && s
[0] == '0')
701 x
= (long) PyOS_strtoul(s
, &end
, base
);
703 x
= PyOS_strtol(s
, &end
, base
);
704 if (end
== s
|| !isxdigit(end
[-1]))
706 while (*end
&& isspace(Py_CHARMASK(*end
)))
710 sprintf(buffer
, "invalid literal for atoi(): %.200s", s
);
711 PyErr_SetString(PyExc_ValueError
, buffer
);
714 else if (errno
!= 0) {
715 sprintf(buffer
, "atoi() literal too large: %.200s", s
);
716 PyErr_SetString(PyExc_ValueError
, buffer
);
719 return PyInt_FromLong(x
);
723 static char atol__doc__
[] =
724 "atol(s [,base]) -> long\n\
726 Return the long integer represented by the string s in the\n\
727 given base, which defaults to 10. The string s must consist\n\
728 of one or more digits, possibly preceded by a sign. If base\n\
729 is 0, it is chosen from the leading characters of s, 0 for\n\
730 octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n\
731 0x or 0X is accepted. A trailing L or l is not accepted,\n\
735 strop_atol(self
, args
)
736 PyObject
*self
; /* Not used */
742 char buffer
[256]; /* For errors */
744 if (!PyArg_ParseTuple(args
, "s|i", &s
, &base
))
747 if ((base
!= 0 && base
< 2) || base
> 36) {
748 PyErr_SetString(PyExc_ValueError
, "invalid base for atol()");
752 while (*s
&& isspace(Py_CHARMASK(*s
)))
755 PyErr_SetString(PyExc_ValueError
, "empty string for atol()");
758 x
= PyLong_FromString(s
, &end
, base
);
761 if (base
== 0 && (*end
== 'l' || *end
== 'L'))
763 while (*end
&& isspace(Py_CHARMASK(*end
)))
766 sprintf(buffer
, "invalid literal for atol(): %.200s", s
);
767 PyErr_SetString(PyExc_ValueError
, buffer
);
775 static char atof__doc__
[] =
778 Return the floating point number represented by the string s.";
781 strop_atof(self
, args
)
782 PyObject
*self
; /* Not used */
785 extern double strtod
Py_PROTO((const char *, char **));
788 char buffer
[256]; /* For errors */
790 if (!PyArg_ParseTuple(args
, "s", &s
))
792 while (*s
&& isspace(Py_CHARMASK(*s
)))
795 PyErr_SetString(PyExc_ValueError
, "empty string for atof()");
799 PyFPE_START_PROTECT("strop_atof", return 0)
802 while (*end
&& isspace(Py_CHARMASK(*end
)))
805 sprintf(buffer
, "invalid literal for atof(): %.200s", s
);
806 PyErr_SetString(PyExc_ValueError
, buffer
);
809 else if (errno
!= 0) {
810 sprintf(buffer
, "atof() literal too large: %.200s", s
);
811 PyErr_SetString(PyExc_ValueError
, buffer
);
814 return PyFloat_FromDouble(x
);
818 static char maketrans__doc__
[] =
819 "maketrans(frm, to) -> string\n\
821 Return a translation table (a string of 256 bytes long)\n\
822 suitable for use in string.translate. The strings frm and to\n\
823 must be of the same length.";
826 strop_maketrans(self
, args
)
827 PyObject
*self
; /* Not used */
830 unsigned char *c
, *from
=NULL
, *to
=NULL
;
831 int i
, fromlen
=0, tolen
=0;
834 if (!PyArg_ParseTuple(args
, "s#s#", &from
, &fromlen
, &to
, &tolen
))
837 if (fromlen
!= tolen
) {
838 PyErr_SetString(PyExc_ValueError
,
839 "maketrans arguments must have same length");
843 result
= PyString_FromStringAndSize((char *)NULL
, 256);
846 c
= (unsigned char *) PyString_AS_STRING((PyStringObject
*)result
);
847 for (i
= 0; i
< 256; i
++)
848 c
[i
]=(unsigned char)i
;
849 for (i
= 0; i
< fromlen
; i
++)
856 static char translate__doc__
[] =
857 "translate(s,table [,deletechars]) -> string\n\
859 Return a copy of the string s, where all characters occurring\n\
860 in the optional argument deletechars are removed, and the\n\
861 remaining characters have been mapped through the given\n\
862 translation table, which must be a string of length 256.";
865 strop_translate(self
, args
)
869 register char *input
, *table
, *output
;
870 register int i
, c
, changed
= 0;
872 char *table1
, *output_start
, *del_table
=NULL
;
873 int inlen
, tablen
, dellen
= 0;
875 int trans_table
[256];
877 if (!PyArg_ParseTuple(args
, "Ss#|s#", &input_obj
,
878 &table1
, &tablen
, &del_table
, &dellen
))
881 PyErr_SetString(PyExc_ValueError
,
882 "translation table must be 256 characters long");
887 inlen
= PyString_Size(input_obj
);
888 result
= PyString_FromStringAndSize((char *)NULL
, inlen
);
891 output_start
= output
= PyString_AsString(result
);
892 input
= PyString_AsString(input_obj
);
895 /* If no deletions are required, use faster code */
896 for (i
= inlen
; --i
>= 0; ) {
897 c
= Py_CHARMASK(*input
++);
898 if (Py_CHARMASK((*output
++ = table
[c
])) != c
)
904 Py_INCREF(input_obj
);
908 for (i
= 0; i
< 256; i
++)
909 trans_table
[i
] = Py_CHARMASK(table
[i
]);
911 for (i
= 0; i
< dellen
; i
++)
912 trans_table
[(int) Py_CHARMASK(del_table
[i
])] = -1;
914 for (i
= inlen
; --i
>= 0; ) {
915 c
= Py_CHARMASK(*input
++);
916 if (trans_table
[c
] != -1)
917 if (Py_CHARMASK(*output
++ = (char)trans_table
[c
]) == c
)
923 Py_INCREF(input_obj
);
926 /* Fix the size of the resulting string */
927 if (inlen
> 0 &&_PyString_Resize(&result
, output
-output_start
))
933 /* What follows is used for implementing replace(). Perry Stoll. */
938 strstr replacement for arbitrary blocks of memory.
940 Locates the first occurance in the memory pointed to by MEM of the
941 contents of memory pointed to by PAT. Returns the index into MEM if
942 found, or -1 if not found. If len of PAT is greater than length of
943 MEM, the function returns -1.
945 static int mymemfind(mem
, len
, pat
, pat_len
)
953 /* pattern can not occur in the last pat_len-1 chars */
956 for (ii
= 0; ii
<= len
; ii
++) {
957 if (mem
[ii
] == pat
[0] &&
959 memcmp(&mem
[ii
+1], &pat
[1], pat_len
-1) == 0)) {
969 Return the number of distinct times PAT is found in MEM.
970 meaning mem=1111 and pat==11 returns 2.
971 mem=11111 and pat==11 also return 2.
973 static int mymemcnt(mem
, len
, pat
, pat_len
)
979 register int offset
= 0;
983 offset
= mymemfind(mem
, len
, pat
, pat_len
);
986 mem
+= offset
+ pat_len
;
987 len
-= offset
+ pat_len
;
996 Return a string in which all occurences of PAT in memory STR are
999 If length of PAT is less than length of STR or there are no occurences
1000 of PAT in STR, then the original string is returned. Otherwise, a new
1001 string is allocated here and returned.
1003 on return, out_len is:
1004 the length of output string, or
1005 -1 if the input string is returned, or
1006 unchanged if an error occurs (no memory).
1009 the new string allocated locally, or
1010 NULL if an error occurred.
1012 static char *mymemreplace(str
, len
, pat
, pat_len
, sub
, sub_len
, count
, out_len
)
1014 int len
; /* input string */
1016 int pat_len
; /* pattern string to find */
1018 int sub_len
; /* substitution string */
1019 int count
; /* number of replacements, 0 == all */
1025 int nfound
, offset
, new_len
;
1027 if (len
== 0 || pat_len
> len
)
1030 /* find length of output string */
1031 nfound
= mymemcnt(str
, len
, pat
, pat_len
);
1033 nfound
= nfound
> count
? count
: nfound
;
1036 new_len
= len
+ nfound
*(sub_len
- pat_len
);
1038 new_s
= (char *)malloc(new_len
);
1039 if (new_s
== NULL
) return NULL
;
1045 /* find index of next instance of pattern */
1046 offset
= mymemfind(str
, len
, pat
, pat_len
);
1047 /* if not found, break out of loop */
1048 if (offset
== -1) break;
1050 /* copy non matching part of input string */
1051 memcpy(new_s
, str
, offset
); /* copy part of str before pat */
1052 str
+= offset
+ pat_len
; /* move str past pattern */
1053 len
-= offset
+ pat_len
; /* reduce length of str remaining */
1055 /* copy substitute into the output string */
1056 new_s
+= offset
; /* move new_s to dest for sub string */
1057 memcpy(new_s
, sub
, sub_len
); /* copy substring into new_s */
1058 new_s
+= sub_len
; /* offset new_s past sub string */
1060 /* break when we've done count replacements */
1061 if (--count
== 0) break;
1063 /* copy any remaining values into output string */
1065 memcpy(new_s
, str
, len
);
1074 static char replace__doc__
[] =
1075 "replace (str, old, new[, maxsplit]) -> string\n\
1077 Return a copy of string str with all occurrences of substring\n\
1078 old replaced by new. If the optional argument maxsplit is\n\
1079 given, only the first maxsplit occurrences are replaced.";
1082 strop_replace(self
, args
)
1083 PyObject
*self
; /* Not used */
1086 char *str
, *pat
,*sub
,*new_s
;
1087 int len
,pat_len
,sub_len
,out_len
;
1091 if (!PyArg_ParseTuple(args
, "s#s#s#|i",
1092 &str
, &len
, &pat
, &pat_len
, &sub
, &sub_len
,
1096 PyErr_SetString(PyExc_ValueError
, "empty pattern string");
1099 new_s
= mymemreplace(str
,len
,pat
,pat_len
,sub
,sub_len
,count
,&out_len
);
1100 if (new_s
== NULL
) {
1104 if (out_len
== -1) {
1105 /* we're returning another reference to the input string */
1106 new = PyTuple_GetItem(args
, 0);
1110 new = PyString_FromStringAndSize(new_s
, out_len
);
1117 /* List of functions defined in the module */
1121 {"atof", strop_atof
, 1, atof__doc__
},
1122 {"atoi", strop_atoi
, 1, atoi__doc__
},
1123 {"atol", strop_atol
, 1, atol__doc__
},
1124 {"capitalize", strop_capitalize
, 0, capitalize__doc__
},
1125 {"find", strop_find
, 1, find__doc__
},
1126 {"join", strop_joinfields
, 1, joinfields__doc__
},
1127 {"joinfields", strop_joinfields
, 1, joinfields__doc__
},
1128 {"lstrip", strop_lstrip
, 0, lstrip__doc__
},
1129 {"lower", strop_lower
, 0, lower__doc__
},
1130 {"maketrans", strop_maketrans
, 1, maketrans__doc__
},
1131 {"replace", strop_replace
, 1, replace__doc__
},
1132 {"rfind", strop_rfind
, 1, rfind__doc__
},
1133 {"rstrip", strop_rstrip
, 0, rstrip__doc__
},
1134 {"split", strop_splitfields
, 1, splitfields__doc__
},
1135 {"splitfields", strop_splitfields
, 1, splitfields__doc__
},
1136 {"strip", strop_strip
, 0, strip__doc__
},
1137 {"swapcase", strop_swapcase
, 0, swapcase__doc__
},
1138 {"translate", strop_translate
, 1, translate__doc__
},
1139 {"upper", strop_upper
, 0, upper__doc__
},
1140 {NULL
, NULL
} /* sentinel */
1147 PyObject
*m
, *d
, *s
;
1150 m
= Py_InitModule4("strop", strop_methods
, strop_module__doc__
,
1151 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1152 d
= PyModule_GetDict(m
);
1154 /* Create 'whitespace' object */
1156 for (c
= 0; c
< 256; c
++) {
1160 s
= PyString_FromStringAndSize(buf
, n
);
1162 PyDict_SetItemString(d
, "whitespace", s
);
1165 /* Create 'lowercase' object */
1167 for (c
= 0; c
< 256; c
++) {
1171 s
= PyString_FromStringAndSize(buf
, n
);
1173 PyDict_SetItemString(d
, "lowercase", s
);
1177 /* Create 'uppercase' object */
1179 for (c
= 0; c
< 256; c
++) {
1183 s
= PyString_FromStringAndSize(buf
, n
);
1185 PyDict_SetItemString(d
, "uppercase", s
);
1189 if (PyErr_Occurred())
1190 Py_FatalError("can't initialize module strop");