]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Modules/stropmodule.c
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 if (slen
> PY_SSIZE_T_MAX
- reslen
||
220 seplen
> PY_SSIZE_T_MAX
- reslen
- seplen
) {
221 PyErr_SetString(PyExc_OverflowError
,
226 while (reslen
+ slen
+ seplen
>= sz
) {
227 if (_PyString_Resize(&res
, sz
* 2) < 0)
230 p
= PyString_AsString(res
) + reslen
;
233 memcpy(p
, sep
, seplen
);
237 memcpy(p
, PyString_AS_STRING(item
), slen
);
241 _PyString_Resize(&res
, reslen
);
245 if (seq
->ob_type
->tp_as_sequence
== NULL
||
246 (getitemfunc
= seq
->ob_type
->tp_as_sequence
->sq_item
) == NULL
)
248 PyErr_SetString(PyExc_TypeError
,
249 "first argument must be a sequence");
252 /* This is now type safe */
253 for (i
= 0; i
< seqlen
; i
++) {
254 PyObject
*item
= getitemfunc(seq
, i
);
255 if (!item
|| !PyString_Check(item
)) {
256 PyErr_SetString(PyExc_TypeError
,
257 "first argument must be sequence of strings");
262 slen
= PyString_GET_SIZE(item
);
263 if (slen
> PY_SSIZE_T_MAX
- reslen
||
264 seplen
> PY_SSIZE_T_MAX
- reslen
- seplen
) {
265 PyErr_SetString(PyExc_OverflowError
,
271 while (reslen
+ slen
+ seplen
>= sz
) {
272 if (_PyString_Resize(&res
, sz
* 2) < 0) {
277 p
= PyString_AsString(res
) + reslen
;
280 memcpy(p
, sep
, seplen
);
284 memcpy(p
, PyString_AS_STRING(item
), slen
);
289 _PyString_Resize(&res
, reslen
);
294 PyDoc_STRVAR(find__doc__
,
295 "find(s, sub [,start [,end]]) -> in\n"
297 "Return the lowest index in s where substring sub is found,\n"
298 "such that sub is contained within s[start,end]. Optional\n"
299 "arguments start and end are interpreted as in slice notation.\n"
301 "Return -1 on failure.");
304 strop_find(PyObject
*self
, PyObject
*args
)
307 Py_ssize_t len
, n
, i
= 0, last
= PY_SSIZE_T_MAX
;
310 if (!PyArg_ParseTuple(args
, "t#t#|nn:find", &s
, &len
, &sub
, &n
, &i
, &last
))
324 if (n
== 0 && i
<= last
)
325 return PyInt_FromLong((long)i
);
328 for (; i
<= last
; ++i
)
329 if (s
[i
] == sub
[0] &&
330 (n
== 1 || memcmp(&s
[i
+1], &sub
[1], n
-1) == 0))
331 return PyInt_FromLong((long)i
);
333 return PyInt_FromLong(-1L);
337 PyDoc_STRVAR(rfind__doc__
,
338 "rfind(s, sub [,start [,end]]) -> int\n"
340 "Return the highest index in s where substring sub is found,\n"
341 "such that sub is contained within s[start,end]. Optional\n"
342 "arguments start and end are interpreted as in slice notation.\n"
344 "Return -1 on failure.");
347 strop_rfind(PyObject
*self
, PyObject
*args
)
350 Py_ssize_t len
, n
, j
;
351 Py_ssize_t i
= 0, last
= PY_SSIZE_T_MAX
;
354 if (!PyArg_ParseTuple(args
, "t#t#|nn:rfind", &s
, &len
, &sub
, &n
, &i
, &last
))
368 if (n
== 0 && i
<= last
)
369 return PyInt_FromLong((long)last
);
371 for (j
= last
-n
; j
>= i
; --j
)
372 if (s
[j
] == sub
[0] &&
373 (n
== 1 || memcmp(&s
[j
+1], &sub
[1], n
-1) == 0))
374 return PyInt_FromLong((long)j
);
376 return PyInt_FromLong(-1L);
381 do_strip(PyObject
*args
, int striptype
)
384 Py_ssize_t len
, i
, j
;
387 if (PyString_AsStringAndSize(args
, &s
, &len
))
391 if (striptype
!= RIGHTSTRIP
) {
392 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
398 if (striptype
!= LEFTSTRIP
) {
401 } while (j
>= i
&& isspace(Py_CHARMASK(s
[j
])));
405 if (i
== 0 && j
== len
) {
410 return PyString_FromStringAndSize(s
+i
, j
-i
);
414 PyDoc_STRVAR(strip__doc__
,
415 "strip(s) -> string\n"
417 "Return a copy of the string s with leading and trailing\n"
418 "whitespace removed.");
421 strop_strip(PyObject
*self
, PyObject
*args
)
424 return do_strip(args
, BOTHSTRIP
);
428 PyDoc_STRVAR(lstrip__doc__
,
429 "lstrip(s) -> string\n"
431 "Return a copy of the string s with leading whitespace removed.");
434 strop_lstrip(PyObject
*self
, PyObject
*args
)
437 return do_strip(args
, LEFTSTRIP
);
441 PyDoc_STRVAR(rstrip__doc__
,
442 "rstrip(s) -> string\n"
444 "Return a copy of the string s with trailing whitespace removed.");
447 strop_rstrip(PyObject
*self
, PyObject
*args
)
450 return do_strip(args
, RIGHTSTRIP
);
454 PyDoc_STRVAR(lower__doc__
,
455 "lower(s) -> string\n"
457 "Return a copy of the string s converted to lowercase.");
460 strop_lower(PyObject
*self
, PyObject
*args
)
468 if (PyString_AsStringAndSize(args
, &s
, &n
))
470 newstr
= PyString_FromStringAndSize(NULL
, n
);
473 s_new
= PyString_AsString(newstr
);
475 for (i
= 0; i
< n
; i
++) {
476 int c
= Py_CHARMASK(*s
++);
493 PyDoc_STRVAR(upper__doc__
,
494 "upper(s) -> string\n"
496 "Return a copy of the string s converted to uppercase.");
499 strop_upper(PyObject
*self
, PyObject
*args
)
507 if (PyString_AsStringAndSize(args
, &s
, &n
))
509 newstr
= PyString_FromStringAndSize(NULL
, n
);
512 s_new
= PyString_AsString(newstr
);
514 for (i
= 0; i
< n
; i
++) {
515 int c
= Py_CHARMASK(*s
++);
532 PyDoc_STRVAR(capitalize__doc__
,
533 "capitalize(s) -> string\n"
535 "Return a copy of the string s with only its first character\n"
539 strop_capitalize(PyObject
*self
, PyObject
*args
)
547 if (PyString_AsStringAndSize(args
, &s
, &n
))
549 newstr
= PyString_FromStringAndSize(NULL
, n
);
552 s_new
= PyString_AsString(newstr
);
555 int c
= Py_CHARMASK(*s
++);
563 for (i
= 1; i
< n
; i
++) {
564 int c
= Py_CHARMASK(*s
++);
581 PyDoc_STRVAR(expandtabs__doc__
,
582 "expandtabs(string, [tabsize]) -> string\n"
584 "Expand tabs in a string, i.e. replace them by one or more spaces,\n"
585 "depending on the current column and the given tab size (default 8).\n"
586 "The column number is reset to zero after each newline occurring in the\n"
587 "string. This doesn't understand other non-printing characters.");
590 strop_expandtabs(PyObject
*self
, PyObject
*args
)
592 /* Original by Fredrik Lundh */
599 Py_ssize_t stringlen
;
604 if (!PyArg_ParseTuple(args
, "s#|i:expandtabs", &string
, &stringlen
, &tabsize
))
607 PyErr_SetString(PyExc_ValueError
,
608 "tabsize must be at least 1");
612 /* First pass: determine size of output string */
613 i
= j
= 0; /* j: current column; i: total of previous lines */
614 e
= string
+ stringlen
;
615 for (p
= string
; p
< e
; p
++) {
617 Py_ssize_t incr
= tabsize
- (j
%tabsize
);
618 if (j
> PY_SSIZE_T_MAX
- incr
)
622 if (j
> PY_SSIZE_T_MAX
- 1)
626 if (i
> PY_SSIZE_T_MAX
- j
)
634 if (i
> PY_SSIZE_T_MAX
- j
)
637 /* Second pass: create output string and fill it */
638 out
= PyString_FromStringAndSize(NULL
, i
+j
);
643 q
= PyString_AS_STRING(out
);
645 for (p
= string
; p
< e
; p
++) {
647 j
= tabsize
- (i
%tabsize
);
661 PyErr_SetString(PyExc_OverflowError
, "result is too long");
666 PyDoc_STRVAR(count__doc__
,
667 "count(s, sub[, start[, end]]) -> int\n"
669 "Return the number of occurrences of substring sub in string\n"
670 "s[start:end]. Optional arguments start and end are\n"
671 "interpreted as in slice notation.");
674 strop_count(PyObject
*self
, PyObject
*args
)
678 Py_ssize_t i
= 0, last
= PY_SSIZE_T_MAX
;
682 if (!PyArg_ParseTuple(args
, "t#t#|nn:count", &s
, &len
, &sub
, &n
, &i
, &last
))
696 return PyInt_FromLong((long) (m
-i
));
700 if (!memcmp(s
+i
, sub
, n
)) {
707 return PyInt_FromLong((long) r
);
711 PyDoc_STRVAR(swapcase__doc__
,
712 "swapcase(s) -> string\n"
714 "Return a copy of the string s with upper case characters\n"
715 "converted to lowercase and vice versa.");
718 strop_swapcase(PyObject
*self
, PyObject
*args
)
726 if (PyString_AsStringAndSize(args
, &s
, &n
))
728 newstr
= PyString_FromStringAndSize(NULL
, n
);
731 s_new
= PyString_AsString(newstr
);
733 for (i
= 0; i
< n
; i
++) {
734 int c
= Py_CHARMASK(*s
++);
739 else if (isupper(c
)) {
756 PyDoc_STRVAR(atoi__doc__
,
757 "atoi(s [,base]) -> int\n"
759 "Return the integer represented by the string s in the given\n"
760 "base, which defaults to 10. The string s must consist of one\n"
761 "or more digits, possibly preceded by a sign. If base is 0, it\n"
762 "is chosen from the leading characters of s, 0 for octal, 0x or\n"
763 "0X for hexadecimal. If base is 16, a preceding 0x or 0X is\n"
767 strop_atoi(PyObject
*self
, PyObject
*args
)
772 char buffer
[256]; /* For errors */
775 if (!PyArg_ParseTuple(args
, "s|i:atoi", &s
, &base
))
778 if ((base
!= 0 && base
< 2) || base
> 36) {
779 PyErr_SetString(PyExc_ValueError
, "invalid base for atoi()");
783 while (*s
&& isspace(Py_CHARMASK(*s
)))
786 if (base
== 0 && s
[0] == '0')
787 x
= (long) PyOS_strtoul(s
, &end
, base
);
789 x
= PyOS_strtol(s
, &end
, base
);
790 if (end
== s
|| !isalnum(Py_CHARMASK(end
[-1])))
792 while (*end
&& isspace(Py_CHARMASK(*end
)))
796 PyOS_snprintf(buffer
, sizeof(buffer
),
797 "invalid literal for atoi(): %.200s", s
);
798 PyErr_SetString(PyExc_ValueError
, buffer
);
801 else if (errno
!= 0) {
802 PyOS_snprintf(buffer
, sizeof(buffer
),
803 "atoi() literal too large: %.200s", s
);
804 PyErr_SetString(PyExc_ValueError
, buffer
);
807 return PyInt_FromLong(x
);
811 PyDoc_STRVAR(atol__doc__
,
812 "atol(s [,base]) -> long\n"
814 "Return the long integer represented by the string s in the\n"
815 "given base, which defaults to 10. The string s must consist\n"
816 "of one or more digits, possibly preceded by a sign. If base\n"
817 "is 0, it is chosen from the leading characters of s, 0 for\n"
818 "octal, 0x or 0X for hexadecimal. If base is 16, a preceding\n"
819 "0x or 0X is accepted. A trailing L or l is not accepted,\n"
820 "unless base is 0.");
823 strop_atol(PyObject
*self
, PyObject
*args
)
828 char buffer
[256]; /* For errors */
831 if (!PyArg_ParseTuple(args
, "s|i:atol", &s
, &base
))
834 if ((base
!= 0 && base
< 2) || base
> 36) {
835 PyErr_SetString(PyExc_ValueError
, "invalid base for atol()");
839 while (*s
&& isspace(Py_CHARMASK(*s
)))
842 PyErr_SetString(PyExc_ValueError
, "empty string for atol()");
845 x
= PyLong_FromString(s
, &end
, base
);
848 if (base
== 0 && (*end
== 'l' || *end
== 'L'))
850 while (*end
&& isspace(Py_CHARMASK(*end
)))
853 PyOS_snprintf(buffer
, sizeof(buffer
),
854 "invalid literal for atol(): %.200s", s
);
855 PyErr_SetString(PyExc_ValueError
, buffer
);
863 PyDoc_STRVAR(atof__doc__
,
866 "Return the floating point number represented by the string s.");
869 strop_atof(PyObject
*self
, PyObject
*args
)
873 char buffer
[256]; /* For errors */
876 if (!PyArg_ParseTuple(args
, "s:atof", &s
))
878 while (*s
&& isspace(Py_CHARMASK(*s
)))
881 PyErr_SetString(PyExc_ValueError
, "empty string for atof()");
885 PyFPE_START_PROTECT("strop_atof", return 0)
886 x
= PyOS_string_to_double(s
, &end
, PyExc_OverflowError
);
888 if (x
== -1 && PyErr_Occurred())
890 while (*end
&& isspace(Py_CHARMASK(*end
)))
893 PyOS_snprintf(buffer
, sizeof(buffer
),
894 "invalid literal for atof(): %.200s", s
);
895 PyErr_SetString(PyExc_ValueError
, buffer
);
898 return PyFloat_FromDouble(x
);
902 PyDoc_STRVAR(maketrans__doc__
,
903 "maketrans(frm, to) -> string\n"
905 "Return a translation table (a string of 256 bytes long)\n"
906 "suitable for use in string.translate. The strings frm and to\n"
907 "must be of the same length.");
910 strop_maketrans(PyObject
*self
, PyObject
*args
)
912 unsigned char *c
, *from
=NULL
, *to
=NULL
;
913 Py_ssize_t i
, fromlen
=0, tolen
=0;
916 if (!PyArg_ParseTuple(args
, "t#t#:maketrans", &from
, &fromlen
, &to
, &tolen
))
919 if (fromlen
!= tolen
) {
920 PyErr_SetString(PyExc_ValueError
,
921 "maketrans arguments must have same length");
925 result
= PyString_FromStringAndSize((char *)NULL
, 256);
928 c
= (unsigned char *) PyString_AS_STRING((PyStringObject
*)result
);
929 for (i
= 0; i
< 256; i
++)
930 c
[i
]=(unsigned char)i
;
931 for (i
= 0; i
< fromlen
; i
++)
938 PyDoc_STRVAR(translate__doc__
,
939 "translate(s,table [,deletechars]) -> string\n"
941 "Return a copy of the string s, where all characters occurring\n"
942 "in the optional argument deletechars are removed, and the\n"
943 "remaining characters have been mapped through the given\n"
944 "translation table, which must be a string of length 256.");
947 strop_translate(PyObject
*self
, PyObject
*args
)
949 register char *input
, *table
, *output
;
953 char *table1
, *output_start
, *del_table
=NULL
;
954 Py_ssize_t inlen
, tablen
, dellen
= 0;
956 int trans_table
[256];
959 if (!PyArg_ParseTuple(args
, "St#|t#:translate", &input_obj
,
960 &table1
, &tablen
, &del_table
, &dellen
))
963 PyErr_SetString(PyExc_ValueError
,
964 "translation table must be 256 characters long");
969 inlen
= PyString_GET_SIZE(input_obj
);
970 result
= PyString_FromStringAndSize((char *)NULL
, inlen
);
973 output_start
= output
= PyString_AsString(result
);
974 input
= PyString_AsString(input_obj
);
977 /* If no deletions are required, use faster code */
978 for (i
= inlen
; --i
>= 0; ) {
979 c
= Py_CHARMASK(*input
++);
980 if (Py_CHARMASK((*output
++ = table
[c
])) != c
)
986 Py_INCREF(input_obj
);
990 for (i
= 0; i
< 256; i
++)
991 trans_table
[i
] = Py_CHARMASK(table
[i
]);
993 for (i
= 0; i
< dellen
; i
++)
994 trans_table
[(int) Py_CHARMASK(del_table
[i
])] = -1;
996 for (i
= inlen
; --i
>= 0; ) {
997 c
= Py_CHARMASK(*input
++);
998 if (trans_table
[c
] != -1)
999 if (Py_CHARMASK(*output
++ = (char)trans_table
[c
]) == c
)
1005 Py_INCREF(input_obj
);
1008 /* Fix the size of the resulting string */
1010 _PyString_Resize(&result
, output
- output_start
);
1015 /* What follows is used for implementing replace(). Perry Stoll. */
1020 strstr replacement for arbitrary blocks of memory.
1022 Locates the first occurrence in the memory pointed to by MEM of the
1023 contents of memory pointed to by PAT. Returns the index into MEM if
1024 found, or -1 if not found. If len of PAT is greater than length of
1025 MEM, the function returns -1.
1028 mymemfind(const char *mem
, Py_ssize_t len
, const char *pat
, Py_ssize_t pat_len
)
1030 register Py_ssize_t ii
;
1032 /* pattern can not occur in the last pat_len-1 chars */
1035 for (ii
= 0; ii
<= len
; ii
++) {
1036 if (mem
[ii
] == pat
[0] &&
1038 memcmp(&mem
[ii
+1], &pat
[1], pat_len
-1) == 0)) {
1048 Return the number of distinct times PAT is found in MEM.
1049 meaning mem=1111 and pat==11 returns 2.
1050 mem=11111 and pat==11 also return 2.
1053 mymemcnt(const char *mem
, Py_ssize_t len
, const char *pat
, Py_ssize_t pat_len
)
1055 register Py_ssize_t offset
= 0;
1056 Py_ssize_t nfound
= 0;
1059 offset
= mymemfind(mem
, len
, pat
, pat_len
);
1062 mem
+= offset
+ pat_len
;
1063 len
-= offset
+ pat_len
;
1072 Return a string in which all occurrences of PAT in memory STR are
1075 If length of PAT is less than length of STR or there are no occurrences
1076 of PAT in STR, then the original string is returned. Otherwise, a new
1077 string is allocated here and returned.
1079 on return, out_len is:
1080 the length of output string, or
1081 -1 if the input string is returned, or
1082 unchanged if an error occurs (no memory).
1085 the new string allocated locally, or
1086 NULL if an error occurred.
1089 mymemreplace(const char *str
, Py_ssize_t len
, /* input string */
1090 const char *pat
, Py_ssize_t pat_len
, /* pattern string to find */
1091 const char *sub
, Py_ssize_t sub_len
, /* substitution string */
1092 Py_ssize_t count
, /* number of replacements */
1093 Py_ssize_t
*out_len
)
1097 Py_ssize_t nfound
, offset
, new_len
;
1099 if (len
== 0 || pat_len
> len
)
1102 /* find length of output string */
1103 nfound
= mymemcnt(str
, len
, pat
, pat_len
);
1105 count
= PY_SSIZE_T_MAX
;
1106 else if (nfound
> count
)
1111 new_len
= len
+ nfound
*(sub_len
- pat_len
);
1113 /* Have to allocate something for the caller to free(). */
1114 out_s
= (char *)PyMem_MALLOC(1);
1120 assert(new_len
> 0);
1121 new_s
= (char *)PyMem_MALLOC(new_len
);
1126 for (; count
> 0 && len
> 0; --count
) {
1127 /* find index of next instance of pattern */
1128 offset
= mymemfind(str
, len
, pat
, pat_len
);
1132 /* copy non matching part of input string */
1133 memcpy(new_s
, str
, offset
);
1134 str
+= offset
+ pat_len
;
1135 len
-= offset
+ pat_len
;
1137 /* copy substitute into the output string */
1139 memcpy(new_s
, sub
, sub_len
);
1142 /* copy any remaining values into output string */
1144 memcpy(new_s
, str
, len
);
1151 return (char *)str
; /* cast away const */
1155 PyDoc_STRVAR(replace__doc__
,
1156 "replace (str, old, new[, maxsplit]) -> string\n"
1158 "Return a copy of string str with all occurrences of substring\n"
1159 "old replaced by new. If the optional argument maxsplit is\n"
1160 "given, only the first maxsplit occurrences are replaced.");
1163 strop_replace(PyObject
*self
, PyObject
*args
)
1165 char *str
, *pat
,*sub
,*new_s
;
1166 Py_ssize_t len
,pat_len
,sub_len
,out_len
;
1167 Py_ssize_t count
= -1;
1171 if (!PyArg_ParseTuple(args
, "t#t#t#|n:replace",
1172 &str
, &len
, &pat
, &pat_len
, &sub
, &sub_len
,
1176 PyErr_SetString(PyExc_ValueError
, "empty pattern string");
1179 /* CAUTION: strop treats a replace count of 0 as infinity, unlke
1180 * current (2.1) string.py and string methods. Preserve this for
1181 * ... well, hard to say for what <wink>.
1185 new_s
= mymemreplace(str
,len
,pat
,pat_len
,sub
,sub_len
,count
,&out_len
);
1186 if (new_s
== NULL
) {
1190 if (out_len
== -1) {
1191 /* we're returning another reference to the input string */
1192 newstr
= PyTuple_GetItem(args
, 0);
1196 newstr
= PyString_FromStringAndSize(new_s
, out_len
);
1203 /* List of functions defined in the module */
1207 {"atof", strop_atof
, METH_VARARGS
, atof__doc__
},
1208 {"atoi", strop_atoi
, METH_VARARGS
, atoi__doc__
},
1209 {"atol", strop_atol
, METH_VARARGS
, atol__doc__
},
1210 {"capitalize", strop_capitalize
, METH_O
, capitalize__doc__
},
1211 {"count", strop_count
, METH_VARARGS
, count__doc__
},
1212 {"expandtabs", strop_expandtabs
, METH_VARARGS
, expandtabs__doc__
},
1213 {"find", strop_find
, METH_VARARGS
, find__doc__
},
1214 {"join", strop_joinfields
, METH_VARARGS
, joinfields__doc__
},
1215 {"joinfields", strop_joinfields
, METH_VARARGS
, joinfields__doc__
},
1216 {"lstrip", strop_lstrip
, METH_O
, lstrip__doc__
},
1217 {"lower", strop_lower
, METH_O
, lower__doc__
},
1218 {"maketrans", strop_maketrans
, METH_VARARGS
, maketrans__doc__
},
1219 {"replace", strop_replace
, METH_VARARGS
, replace__doc__
},
1220 {"rfind", strop_rfind
, METH_VARARGS
, rfind__doc__
},
1221 {"rstrip", strop_rstrip
, METH_O
, rstrip__doc__
},
1222 {"split", strop_splitfields
, METH_VARARGS
, splitfields__doc__
},
1223 {"splitfields", strop_splitfields
, METH_VARARGS
, splitfields__doc__
},
1224 {"strip", strop_strip
, METH_O
, strip__doc__
},
1225 {"swapcase", strop_swapcase
, METH_O
, swapcase__doc__
},
1226 {"translate", strop_translate
, METH_VARARGS
, translate__doc__
},
1227 {"upper", strop_upper
, METH_O
, upper__doc__
},
1228 {NULL
, NULL
} /* sentinel */
1238 m
= Py_InitModule4("strop", strop_methods
, strop_module__doc__
,
1239 (PyObject
*)NULL
, PYTHON_API_VERSION
);
1243 /* Create 'whitespace' object */
1245 for (c
= 0; c
< 256; c
++) {
1249 s
= PyString_FromStringAndSize(buf
, n
);
1251 PyModule_AddObject(m
, "whitespace", s
);
1253 /* Create 'lowercase' object */
1255 for (c
= 0; c
< 256; c
++) {
1259 s
= PyString_FromStringAndSize(buf
, n
);
1261 PyModule_AddObject(m
, "lowercase", s
);
1263 /* Create 'uppercase' object */
1265 for (c
= 0; c
< 256; c
++) {
1269 s
= PyString_FromStringAndSize(buf
, n
);
1271 PyModule_AddObject(m
, "uppercase", s
);