3 #define PY_SSIZE_T_CLEAN
7 PyDoc_STRVAR(strop_module__doc__
,
8 "Common string manipulations, optimized for speed.\n"
10 "Always use \"import string\" rather than referencing\n"
11 "this module directly.");
13 /* XXX This file assumes that the <ctype.h> is*() functions
14 XXX are defined for all 8-bit characters! */
16 #define WARN if (PyErr_Warn(PyExc_DeprecationWarning, \
17 "strop functions are obsolete; use string methods")) \
20 /* The lstrip(), rstrip() and strip() functions are implemented
21 in do_strip(), which uses an additional parameter to indicate what
22 type of strip should occur. */
30 split_whitespace(char *s
, Py_ssize_t len
, Py_ssize_t maxsplit
)
34 Py_ssize_t countsplit
= 0;
36 PyObject
*list
= PyList_New(0);
42 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
46 while (i
< len
&& !isspace(Py_CHARMASK(s
[i
]))) {
50 item
= PyString_FromStringAndSize(s
+j
, i
-j
);
54 err
= PyList_Append(list
, item
);
60 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
63 if (maxsplit
&& (countsplit
>= maxsplit
) && i
< len
) {
64 item
= PyString_FromStringAndSize(
69 err
= PyList_Append(list
, item
);
85 PyDoc_STRVAR(splitfields__doc__
,
86 "split(s [,sep [,maxsplit]]) -> list of strings\n"
87 "splitfields(s [,sep [,maxsplit]]) -> list of strings\n"
89 "Return a list of the words in the string s, using sep as the\n"
90 "delimiter string. If maxsplit is nonzero, splits into at most\n"
91 "maxsplit words. If sep is not specified, any whitespace string\n"
92 "is a separator. Maxsplit defaults to 0.\n"
94 "(split and splitfields are synonymous)");
97 strop_splitfields(PyObject
*self
, PyObject
*args
)
99 Py_ssize_t len
, n
, i
, j
, err
;
100 Py_ssize_t splitcount
, maxsplit
;
102 PyObject
*list
, *item
;
109 if (!PyArg_ParseTuple(args
, "t#|z#n:split", &s
, &len
, &sub
, &n
, &maxsplit
))
112 return split_whitespace(s
, len
, maxsplit
);
114 PyErr_SetString(PyExc_ValueError
, "empty separator");
118 list
= PyList_New(0);
124 if (s
[i
] == sub
[0] && (n
== 1 || memcmp(s
+i
, sub
, n
) == 0)) {
125 item
= PyString_FromStringAndSize(s
+j
, i
-j
);
128 err
= PyList_Append(list
, item
);
134 if (maxsplit
&& (splitcount
>= maxsplit
))
140 item
= PyString_FromStringAndSize(s
+j
, len
-j
);
143 err
= PyList_Append(list
, item
);
156 PyDoc_STRVAR(joinfields__doc__
,
157 "join(list [,sep]) -> string\n"
158 "joinfields(list [,sep]) -> string\n"
160 "Return a string composed of the words in list, with\n"
161 "intervening occurrences of sep. Sep defaults to a single\n"
164 "(join and joinfields are synonymous)");
167 strop_joinfields(PyObject
*self
, PyObject
*args
)
171 Py_ssize_t seqlen
, seplen
= 0;
172 Py_ssize_t i
, reslen
= 0, slen
= 0, sz
= 100;
173 PyObject
*res
= NULL
;
175 ssizeargfunc getitemfunc
;
178 if (!PyArg_ParseTuple(args
, "O|t#:join", &seq
, &sep
, &seplen
))
185 seqlen
= PySequence_Size(seq
);
186 if (seqlen
< 0 && PyErr_Occurred())
190 /* Optimization if there's only one item */
191 PyObject
*item
= PySequence_GetItem(seq
, 0);
192 if (item
&& !PyString_Check(item
)) {
193 PyErr_SetString(PyExc_TypeError
,
194 "first argument must be sequence of strings");
201 if (!(res
= PyString_FromStringAndSize((char*)NULL
, sz
)))
203 p
= PyString_AsString(res
);
205 /* optimize for lists, since it's the most common case. all others
206 * (tuples and arbitrary sequences) just use the sequence abstract
209 if (PyList_Check(seq
)) {
210 for (i
= 0; i
< seqlen
; i
++) {
211 PyObject
*item
= PyList_GET_ITEM(seq
, i
);
212 if (!PyString_Check(item
)) {
213 PyErr_SetString(PyExc_TypeError
,
214 "first argument must be sequence of strings");
218 slen
= PyString_GET_SIZE(item
);
219 while (reslen
+ slen
+ seplen
>= sz
) {
220 if (_PyString_Resize(&res
, sz
* 2) < 0)
223 p
= PyString_AsString(res
) + reslen
;
226 memcpy(p
, sep
, seplen
);
230 memcpy(p
, PyString_AS_STRING(item
), slen
);
234 _PyString_Resize(&res
, reslen
);
238 if (seq
->ob_type
->tp_as_sequence
== NULL
||
239 (getitemfunc
= seq
->ob_type
->tp_as_sequence
->sq_item
) == NULL
)
241 PyErr_SetString(PyExc_TypeError
,
242 "first argument must be a sequence");
245 /* This is now type safe */
246 for (i
= 0; i
< seqlen
; i
++) {
247 PyObject
*item
= getitemfunc(seq
, i
);
248 if (!item
|| !PyString_Check(item
)) {
249 PyErr_SetString(PyExc_TypeError
,
250 "first argument must be sequence of strings");
255 slen
= PyString_GET_SIZE(item
);
256 while (reslen
+ slen
+ seplen
>= sz
) {
257 if (_PyString_Resize(&res
, sz
* 2) < 0) {
262 p
= PyString_AsString(res
) + reslen
;
265 memcpy(p
, sep
, seplen
);
269 memcpy(p
, PyString_AS_STRING(item
), slen
);
274 _PyString_Resize(&res
, reslen
);
279 PyDoc_STRVAR(find__doc__
,
280 "find(s, sub [,start [,end]]) -> in\n"
282 "Return the lowest index in s where substring sub is found,\n"
283 "such that sub is contained within s[start,end]. Optional\n"
284 "arguments start and end are interpreted as in slice notation.\n"
286 "Return -1 on failure.");
289 strop_find(PyObject
*self
, PyObject
*args
)
292 Py_ssize_t len
, n
, i
= 0, last
= PY_SSIZE_T_MAX
;
295 if (!PyArg_ParseTuple(args
, "t#t#|nn:find", &s
, &len
, &sub
, &n
, &i
, &last
))
309 if (n
== 0 && i
<= last
)
310 return PyInt_FromLong((long)i
);
313 for (; i
<= last
; ++i
)
314 if (s
[i
] == sub
[0] &&
315 (n
== 1 || memcmp(&s
[i
+1], &sub
[1], n
-1) == 0))
316 return PyInt_FromLong((long)i
);
318 return PyInt_FromLong(-1L);
322 PyDoc_STRVAR(rfind__doc__
,
323 "rfind(s, sub [,start [,end]]) -> int\n"
325 "Return the highest index in s where substring sub is found,\n"
326 "such that sub is contained within s[start,end]. Optional\n"
327 "arguments start and end are interpreted as in slice notation.\n"
329 "Return -1 on failure.");
332 strop_rfind(PyObject
*self
, PyObject
*args
)
335 Py_ssize_t len
, n
, j
;
336 Py_ssize_t i
= 0, last
= PY_SSIZE_T_MAX
;
339 if (!PyArg_ParseTuple(args
, "t#t#|nn:rfind", &s
, &len
, &sub
, &n
, &i
, &last
))
353 if (n
== 0 && i
<= last
)
354 return PyInt_FromLong((long)last
);
356 for (j
= last
-n
; j
>= i
; --j
)
357 if (s
[j
] == sub
[0] &&
358 (n
== 1 || memcmp(&s
[j
+1], &sub
[1], n
-1) == 0))
359 return PyInt_FromLong((long)j
);
361 return PyInt_FromLong(-1L);
366 do_strip(PyObject
*args
, int striptype
)
369 Py_ssize_t len
, i
, j
;
372 if (PyString_AsStringAndSize(args
, &s
, &len
))
376 if (striptype
!= RIGHTSTRIP
) {
377 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
383 if (striptype
!= LEFTSTRIP
) {
386 } while (j
>= i
&& isspace(Py_CHARMASK(s
[j
])));
390 if (i
== 0 && j
== len
) {
395 return PyString_FromStringAndSize(s
+i
, j
-i
);
399 PyDoc_STRVAR(strip__doc__
,
400 "strip(s) -> string\n"
402 "Return a copy of the string s with leading and trailing\n"
403 "whitespace removed.");
406 strop_strip(PyObject
*self
, PyObject
*args
)
409 return do_strip(args
, BOTHSTRIP
);
413 PyDoc_STRVAR(lstrip__doc__
,
414 "lstrip(s) -> string\n"
416 "Return a copy of the string s with leading whitespace removed.");
419 strop_lstrip(PyObject
*self
, PyObject
*args
)
422 return do_strip(args
, LEFTSTRIP
);
426 PyDoc_STRVAR(rstrip__doc__
,
427 "rstrip(s) -> string\n"
429 "Return a copy of the string s with trailing whitespace removed.");
432 strop_rstrip(PyObject
*self
, PyObject
*args
)
435 return do_strip(args
, RIGHTSTRIP
);
439 PyDoc_STRVAR(lower__doc__
,
440 "lower(s) -> string\n"
442 "Return a copy of the string s converted to lowercase.");
445 strop_lower(PyObject
*self
, PyObject
*args
)
453 if (PyString_AsStringAndSize(args
, &s
, &n
))
455 newstr
= PyString_FromStringAndSize(NULL
, n
);
458 s_new
= PyString_AsString(newstr
);
460 for (i
= 0; i
< n
; i
++) {
461 int c
= Py_CHARMASK(*s
++);
478 PyDoc_STRVAR(upper__doc__
,
479 "upper(s) -> string\n"
481 "Return a copy of the string s converted to uppercase.");
484 strop_upper(PyObject
*self
, PyObject
*args
)
492 if (PyString_AsStringAndSize(args
, &s
, &n
))
494 newstr
= PyString_FromStringAndSize(NULL
, n
);
497 s_new
= PyString_AsString(newstr
);
499 for (i
= 0; i
< n
; i
++) {
500 int c
= Py_CHARMASK(*s
++);
517 PyDoc_STRVAR(capitalize__doc__
,
518 "capitalize(s) -> string\n"
520 "Return a copy of the string s with only its first character\n"
524 strop_capitalize(PyObject
*self
, PyObject
*args
)
532 if (PyString_AsStringAndSize(args
, &s
, &n
))
534 newstr
= PyString_FromStringAndSize(NULL
, n
);
537 s_new
= PyString_AsString(newstr
);
540 int c
= Py_CHARMASK(*s
++);
548 for (i
= 1; i
< n
; i
++) {
549 int c
= Py_CHARMASK(*s
++);
566 PyDoc_STRVAR(expandtabs__doc__
,
567 "expandtabs(string, [tabsize]) -> string\n"
569 "Expand tabs in a string, i.e. replace them by one or more spaces,\n"
570 "depending on the current column and the given tab size (default 8).\n"
571 "The column number is reset to zero after each newline occurring in the\n"
572 "string. This doesn't understand other non-printing characters.");
575 strop_expandtabs(PyObject
*self
, PyObject
*args
)
577 /* Original by Fredrik Lundh */
584 Py_ssize_t stringlen
;
589 if (!PyArg_ParseTuple(args
, "s#|i:expandtabs", &string
, &stringlen
, &tabsize
))
592 PyErr_SetString(PyExc_ValueError
,
593 "tabsize must be at least 1");
597 /* First pass: determine size of output string */
598 i
= j
= 0; /* j: current column; i: total of previous lines */
599 e
= string
+ stringlen
;
600 for (p
= string
; p
< e
; p
++) {
602 j
+= tabsize
- (j
%tabsize
);
612 /* Second pass: create output string and fill it */
613 out
= PyString_FromStringAndSize(NULL
, i
+j
);
618 q
= PyString_AS_STRING(out
);
620 for (p
= string
; p
< e
; p
++) {
622 j
= tabsize
- (i
%tabsize
);
638 PyDoc_STRVAR(count__doc__
,
639 "count(s, sub[, start[, end]]) -> int\n"
641 "Return the number of occurrences of substring sub in string\n"
642 "s[start:end]. Optional arguments start and end are\n"
643 "interpreted as in slice notation.");
646 strop_count(PyObject
*self
, PyObject
*args
)
650 Py_ssize_t i
= 0, last
= PY_SSIZE_T_MAX
;
654 if (!PyArg_ParseTuple(args
, "t#t#|nn:count", &s
, &len
, &sub
, &n
, &i
, &last
))
668 return PyInt_FromLong((long) (m
-i
));
672 if (!memcmp(s
+i
, sub
, n
)) {
679 return PyInt_FromLong((long) r
);
683 PyDoc_STRVAR(swapcase__doc__
,
684 "swapcase(s) -> string\n"
686 "Return a copy of the string s with upper case characters\n"
687 "converted to lowercase and vice versa.");
690 strop_swapcase(PyObject
*self
, PyObject
*args
)
698 if (PyString_AsStringAndSize(args
, &s
, &n
))
700 newstr
= PyString_FromStringAndSize(NULL
, n
);
703 s_new
= PyString_AsString(newstr
);
705 for (i
= 0; i
< n
; i
++) {
706 int c
= Py_CHARMASK(*s
++);
711 else if (isupper(c
)) {
728 PyDoc_STRVAR(atoi__doc__
,
729 "atoi(s [,base]) -> int\n"
731 "Return the integer represented by the string s in the given\n"
732 "base, which defaults to 10. The string s must consist of one\n"
733 "or more digits, possibly preceded by a sign. If base is 0, it\n"
734 "is chosen from the leading characters of s, 0 for octal, 0x or\n"
735 "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
739 strop_atoi(PyObject
*self
, PyObject
*args
)
744 char buffer
[256]; /* For errors */
747 if (!PyArg_ParseTuple(args
, "s|i:atoi", &s
, &base
))
750 if ((base
!= 0 && base
< 2) || base
> 36) {
751 PyErr_SetString(PyExc_ValueError
, "invalid base for atoi()");
755 while (*s
&& isspace(Py_CHARMASK(*s
)))
758 if (base
== 0 && s
[0] == '0')
759 x
= (long) PyOS_strtoul(s
, &end
, base
);
761 x
= PyOS_strtol(s
, &end
, base
);
762 if (end
== s
|| !isalnum(Py_CHARMASK(end
[-1])))
764 while (*end
&& isspace(Py_CHARMASK(*end
)))
768 PyOS_snprintf(buffer
, sizeof(buffer
),
769 "invalid literal for atoi(): %.200s", s
);
770 PyErr_SetString(PyExc_ValueError
, buffer
);
773 else if (errno
!= 0) {
774 PyOS_snprintf(buffer
, sizeof(buffer
),
775 "atoi() literal too large: %.200s", s
);
776 PyErr_SetString(PyExc_ValueError
, buffer
);
779 return PyInt_FromLong(x
);
783 PyDoc_STRVAR(atol__doc__
,
784 "atol(s [,base]) -> long\n"
786 "Return the long integer represented by the string s in the\n"
787 "given base, which defaults to 10. The string s must consist\n"
788 "of one or more digits, possibly preceded by a sign. If base\n"
789 "is 0, it is chosen from the leading characters of s, 0 for\n"
790 "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
791 "0x or 0X is accepted. A trailing L or l is not accepted,\n"
792 "unless base is 0.");
795 strop_atol(PyObject
*self
, PyObject
*args
)
800 char buffer
[256]; /* For errors */
803 if (!PyArg_ParseTuple(args
, "s|i:atol", &s
, &base
))
806 if ((base
!= 0 && base
< 2) || base
> 36) {
807 PyErr_SetString(PyExc_ValueError
, "invalid base for atol()");
811 while (*s
&& isspace(Py_CHARMASK(*s
)))
814 PyErr_SetString(PyExc_ValueError
, "empty string for atol()");
817 x
= PyLong_FromString(s
, &end
, base
);
820 if (base
== 0 && (*end
== 'l' || *end
== 'L'))
822 while (*end
&& isspace(Py_CHARMASK(*end
)))
825 PyOS_snprintf(buffer
, sizeof(buffer
),
826 "invalid literal for atol(): %.200s", s
);
827 PyErr_SetString(PyExc_ValueError
, buffer
);
835 PyDoc_STRVAR(atof__doc__
,
838 "Return the floating point number represented by the string s.");
841 strop_atof(PyObject
*self
, PyObject
*args
)
845 char buffer
[256]; /* For errors */
848 if (!PyArg_ParseTuple(args
, "s:atof", &s
))
850 while (*s
&& isspace(Py_CHARMASK(*s
)))
853 PyErr_SetString(PyExc_ValueError
, "empty string for atof()");
857 PyFPE_START_PROTECT("strop_atof", return 0)
858 x
= PyOS_ascii_strtod(s
, &end
);
860 while (*end
&& isspace(Py_CHARMASK(*end
)))
863 PyOS_snprintf(buffer
, sizeof(buffer
),
864 "invalid literal for atof(): %.200s", s
);
865 PyErr_SetString(PyExc_ValueError
, buffer
);
868 else if (errno
!= 0) {
869 PyOS_snprintf(buffer
, sizeof(buffer
),
870 "atof() literal too large: %.200s", s
);
871 PyErr_SetString(PyExc_ValueError
, buffer
);
874 return PyFloat_FromDouble(x
);
878 PyDoc_STRVAR(maketrans__doc__
,
879 "maketrans(frm, to) -> string\n"
881 "Return a translation table (a string of 256 bytes long)\n"
882 "suitable for use in string.translate. The strings frm and to\n"
883 "must be of the same length.");
886 strop_maketrans(PyObject
*self
, PyObject
*args
)
888 unsigned char *c
, *from
=NULL
, *to
=NULL
;
889 Py_ssize_t i
, fromlen
=0, tolen
=0;
892 if (!PyArg_ParseTuple(args
, "t#t#:maketrans", &from
, &fromlen
, &to
, &tolen
))
895 if (fromlen
!= tolen
) {
896 PyErr_SetString(PyExc_ValueError
,
897 "maketrans arguments must have same length");
901 result
= PyString_FromStringAndSize((char *)NULL
, 256);
904 c
= (unsigned char *) PyString_AS_STRING((PyStringObject
*)result
);
905 for (i
= 0; i
< 256; i
++)
906 c
[i
]=(unsigned char)i
;
907 for (i
= 0; i
< fromlen
; i
++)
914 PyDoc_STRVAR(translate__doc__
,
915 "translate(s,table [,deletechars]) -> string\n"
917 "Return a copy of the string s, where all characters occurring\n"
918 "in the optional argument deletechars are removed, and the\n"
919 "remaining characters have been mapped through the given\n"
920 "translation table, which must be a string of length 256.");
923 strop_translate(PyObject
*self
, PyObject
*args
)
925 register char *input
, *table
, *output
;
929 char *table1
, *output_start
, *del_table
=NULL
;
930 Py_ssize_t inlen
, tablen
, dellen
= 0;
932 int trans_table
[256];
935 if (!PyArg_ParseTuple(args
, "St#|t#:translate", &input_obj
,
936 &table1
, &tablen
, &del_table
, &dellen
))
939 PyErr_SetString(PyExc_ValueError
,
940 "translation table must be 256 characters long");
945 inlen
= PyString_GET_SIZE(input_obj
);
946 result
= PyString_FromStringAndSize((char *)NULL
, inlen
);
949 output_start
= output
= PyString_AsString(result
);
950 input
= PyString_AsString(input_obj
);
953 /* If no deletions are required, use faster code */
954 for (i
= inlen
; --i
>= 0; ) {
955 c
= Py_CHARMASK(*input
++);
956 if (Py_CHARMASK((*output
++ = table
[c
])) != c
)
962 Py_INCREF(input_obj
);
966 for (i
= 0; i
< 256; i
++)
967 trans_table
[i
] = Py_CHARMASK(table
[i
]);
969 for (i
= 0; i
< dellen
; i
++)
970 trans_table
[(int) Py_CHARMASK(del_table
[i
])] = -1;
972 for (i
= inlen
; --i
>= 0; ) {
973 c
= Py_CHARMASK(*input
++);
974 if (trans_table
[c
] != -1)
975 if (Py_CHARMASK(*output
++ = (char)trans_table
[c
]) == c
)
981 Py_INCREF(input_obj
);
984 /* Fix the size of the resulting string */
986 _PyString_Resize(&result
, output
- output_start
);
991 /* What follows is used for implementing replace(). Perry Stoll. */
996 strstr replacement for arbitrary blocks of memory.
998 Locates the first occurrence in the memory pointed to by MEM of the
999 contents of memory pointed to by PAT. Returns the index into MEM if
1000 found, or -1 if not found. If len of PAT is greater than length of
1001 MEM, the function returns -1.
1004 mymemfind(const char *mem
, Py_ssize_t len
, const char *pat
, Py_ssize_t pat_len
)
1006 register Py_ssize_t ii
;
1008 /* pattern can not occur in the last pat_len-1 chars */
1011 for (ii
= 0; ii
<= len
; ii
++) {
1012 if (mem
[ii
] == pat
[0] &&
1014 memcmp(&mem
[ii
+1], &pat
[1], pat_len
-1) == 0)) {
1024 Return the number of distinct times PAT is found in MEM.
1025 meaning mem=1111 and pat==11 returns 2.
1026 mem=11111 and pat==11 also return 2.
1029 mymemcnt(const char *mem
, Py_ssize_t len
, const char *pat
, Py_ssize_t pat_len
)
1031 register Py_ssize_t offset
= 0;
1032 Py_ssize_t nfound
= 0;
1035 offset
= mymemfind(mem
, len
, pat
, pat_len
);
1038 mem
+= offset
+ pat_len
;
1039 len
-= offset
+ pat_len
;
1048 Return a string in which all occurrences of PAT in memory STR are
1051 If length of PAT is less than length of STR or there are no occurrences
1052 of PAT in STR, then the original string is returned. Otherwise, a new
1053 string is allocated here and returned.
1055 on return, out_len is:
1056 the length of output string, or
1057 -1 if the input string is returned, or
1058 unchanged if an error occurs (no memory).
1061 the new string allocated locally, or
1062 NULL if an error occurred.
1065 mymemreplace(const char *str
, Py_ssize_t len
, /* input string */
1066 const char *pat
, Py_ssize_t pat_len
, /* pattern string to find */
1067 const char *sub
, Py_ssize_t sub_len
, /* substitution string */
1068 Py_ssize_t count
, /* number of replacements */
1069 Py_ssize_t
*out_len
)
1073 Py_ssize_t nfound
, offset
, new_len
;
1075 if (len
== 0 || pat_len
> len
)
1078 /* find length of output string */
1079 nfound
= mymemcnt(str
, len
, pat
, pat_len
);
1081 count
= PY_SSIZE_T_MAX
;
1082 else if (nfound
> count
)
1087 new_len
= len
+ nfound
*(sub_len
- pat_len
);
1089 /* Have to allocate something for the caller to free(). */
1090 out_s
= (char *)PyMem_MALLOC(1);
1096 assert(new_len
> 0);
1097 new_s
= (char *)PyMem_MALLOC(new_len
);
1102 for (; count
> 0 && len
> 0; --count
) {
1103 /* find index of next instance of pattern */
1104 offset
= mymemfind(str
, len
, pat
, pat_len
);
1108 /* copy non matching part of input string */
1109 memcpy(new_s
, str
, offset
);
1110 str
+= offset
+ pat_len
;
1111 len
-= offset
+ pat_len
;
1113 /* copy substitute into the output string */
1115 memcpy(new_s
, sub
, sub_len
);
1118 /* copy any remaining values into output string */
1120 memcpy(new_s
, str
, len
);
1127 return (char *)str
; /* cast away const */
1131 PyDoc_STRVAR(replace__doc__
,
1132 "replace (str, old, new[, maxsplit]) -> string\n"
1134 "Return a copy of string str with all occurrences of substring\n"
1135 "old replaced by new. If the optional argument maxsplit is\n"
1136 "given, only the first maxsplit occurrences are replaced.");
1139 strop_replace(PyObject
*self
, PyObject
*args
)
1141 char *str
, *pat
,*sub
,*new_s
;
1142 Py_ssize_t len
,pat_len
,sub_len
,out_len
;
1143 Py_ssize_t count
= -1;
1147 if (!PyArg_ParseTuple(args
, "t#t#t#|n:replace",
1148 &str
, &len
, &pat
, &pat_len
, &sub
, &sub_len
,
1152 PyErr_SetString(PyExc_ValueError
, "empty pattern string");
1155 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1156 * current (2.1) string.py and string methods. Preserve this for
1157 * ... well, hard to say for what <wink>.
1161 new_s
= mymemreplace(str
,len
,pat
,pat_len
,sub
,sub_len
,count
,&out_len
);
1162 if (new_s
== NULL
) {
1166 if (out_len
== -1) {
1167 /* we're returning another reference to the input string */
1168 newstr
= PyTuple_GetItem(args
, 0);
1172 newstr
= PyString_FromStringAndSize(new_s
, out_len
);
1179 /* List of functions defined in the module */
1183 {"atof", strop_atof
, METH_VARARGS
, atof__doc__
},
1184 {"atoi", strop_atoi
, METH_VARARGS
, atoi__doc__
},
1185 {"atol", strop_atol
, METH_VARARGS
, atol__doc__
},
1186 {"capitalize", strop_capitalize
, METH_O
, capitalize__doc__
},
1187 {"count", strop_count
, METH_VARARGS
, count__doc__
},
1188 {"expandtabs", strop_expandtabs
, METH_VARARGS
, expandtabs__doc__
},
1189 {"find", strop_find
, METH_VARARGS
, find__doc__
},
1190 {"join", strop_joinfields
, METH_VARARGS
, joinfields__doc__
},
1191 {"joinfields", strop_joinfields
, METH_VARARGS
, joinfields__doc__
},
1192 {"lstrip", strop_lstrip
, METH_O
, lstrip__doc__
},
1193 {"lower", strop_lower
, METH_O
, lower__doc__
},
1194 {"maketrans", strop_maketrans
, METH_VARARGS
, maketrans__doc__
},
1195 {"replace", strop_replace
, METH_VARARGS
, replace__doc__
},
1196 {"rfind", strop_rfind
, METH_VARARGS
, rfind__doc__
},
1197 {"rstrip", strop_rstrip
, METH_O
, rstrip__doc__
},
1198 {"split", strop_splitfields
, METH_VARARGS
, splitfields__doc__
},
1199 {"splitfields", strop_splitfields
, METH_VARARGS
, splitfields__doc__
},
1200 {"strip", strop_strip
, METH_O
, strip__doc__
},
1201 {"swapcase", strop_swapcase
, METH_O
, swapcase__doc__
},
1202 {"translate", strop_translate
, METH_VARARGS
, translate__doc__
},
1203 {"upper", strop_upper
, METH_O
, upper__doc__
},
1204 {NULL
, NULL
} /* sentinel */
1214 m
= Py_InitModule4("strop", strop_methods
, strop_module__doc__
,
1215 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1219 /* Create 'whitespace' object */
1221 for (c
= 0; c
< 256; c
++) {
1225 s
= PyString_FromStringAndSize(buf
, n
);
1227 PyModule_AddObject(m
, "whitespace", s
);
1229 /* Create 'lowercase' object */
1231 for (c
= 0; c
< 256; c
++) {
1235 s
= PyString_FromStringAndSize(buf
, n
);
1237 PyModule_AddObject(m
, "lowercase", s
);
1239 /* Create 'uppercase' object */
1241 for (c
= 0; c
< 256; c
++) {
1245 s
= PyString_FromStringAndSize(buf
, n
);
1247 PyModule_AddObject(m
, "uppercase", s
);