1 /* String object implementation */
3 #define PY_SSIZE_T_CLEAN
10 int null_strings
, one_strings
;
13 static PyStringObject
*characters
[UCHAR_MAX
+ 1];
14 static PyStringObject
*nullstring
;
16 /* This dictionary holds all interned strings. Note that references to
17 strings in this dictionary are *not* counted in the string's ob_refcnt.
18 When the interned string reaches a refcnt of 0 the string deallocation
19 function will delete the reference from this dictionary.
21 Another way to look at this is that to say that the actual reference
22 count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
24 static PyObject
*interned
;
27 For both PyString_FromString() and PyString_FromStringAndSize(), the
28 parameter `size' denotes number of characters to allocate, not counting any
29 null terminating character.
31 For PyString_FromString(), the parameter `str' points to a null-terminated
32 string containing exactly `size' bytes.
34 For PyString_FromStringAndSize(), the parameter the parameter `str' is
35 either NULL or else points to a string containing at least `size' bytes.
36 For PyString_FromStringAndSize(), the string in the `str' parameter does
37 not have to be null-terminated. (Therefore it is safe to construct a
38 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.)
39 If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1'
40 bytes (setting the last byte to the null terminating character) and you can
41 fill in the data yourself. If `str' is non-NULL then the resulting
42 PyString object must be treated as immutable and you must not fill in nor
43 alter the data yourself, since the strings may be shared.
45 The PyObject member `op->ob_size', which denotes the number of "extra
46 items" in a variable-size object, will contain the number of bytes
47 allocated for string data, not counting the null terminating character. It
48 is therefore equal to the equal to the `size' parameter (for
49 PyString_FromStringAndSize()) or the length of the string in the `str'
50 parameter (for PyString_FromString()).
53 PyString_FromStringAndSize(const char *str
, Py_ssize_t size
)
55 register PyStringObject
*op
;
57 if (size
== 0 && (op
= nullstring
) != NULL
) {
62 return (PyObject
*)op
;
64 if (size
== 1 && str
!= NULL
&&
65 (op
= characters
[*str
& UCHAR_MAX
]) != NULL
)
71 return (PyObject
*)op
;
74 /* Inline PyObject_NewVar */
75 op
= (PyStringObject
*)PyObject_MALLOC(sizeof(PyStringObject
) + size
);
77 return PyErr_NoMemory();
78 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
80 op
->ob_sstate
= SSTATE_NOT_INTERNED
;
82 Py_MEMCPY(op
->ob_sval
, str
, size
);
83 op
->ob_sval
[size
] = '\0';
84 /* share short strings */
86 PyObject
*t
= (PyObject
*)op
;
87 PyString_InternInPlace(&t
);
88 op
= (PyStringObject
*)t
;
91 } else if (size
== 1 && str
!= NULL
) {
92 PyObject
*t
= (PyObject
*)op
;
93 PyString_InternInPlace(&t
);
94 op
= (PyStringObject
*)t
;
95 characters
[*str
& UCHAR_MAX
] = op
;
98 return (PyObject
*) op
;
102 PyString_FromString(const char *str
)
104 register size_t size
;
105 register PyStringObject
*op
;
109 if (size
> PY_SSIZE_T_MAX
) {
110 PyErr_SetString(PyExc_OverflowError
,
111 "string is too long for a Python string");
114 if (size
== 0 && (op
= nullstring
) != NULL
) {
119 return (PyObject
*)op
;
121 if (size
== 1 && (op
= characters
[*str
& UCHAR_MAX
]) != NULL
) {
126 return (PyObject
*)op
;
129 /* Inline PyObject_NewVar */
130 op
= (PyStringObject
*)PyObject_MALLOC(sizeof(PyStringObject
) + size
);
132 return PyErr_NoMemory();
133 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
135 op
->ob_sstate
= SSTATE_NOT_INTERNED
;
136 Py_MEMCPY(op
->ob_sval
, str
, size
+1);
137 /* share short strings */
139 PyObject
*t
= (PyObject
*)op
;
140 PyString_InternInPlace(&t
);
141 op
= (PyStringObject
*)t
;
144 } else if (size
== 1) {
145 PyObject
*t
= (PyObject
*)op
;
146 PyString_InternInPlace(&t
);
147 op
= (PyStringObject
*)t
;
148 characters
[*str
& UCHAR_MAX
] = op
;
151 return (PyObject
*) op
;
155 PyString_FromFormatV(const char *format
, va_list vargs
)
163 #ifdef VA_LIST_IS_ARRAY
164 Py_MEMCPY(count
, vargs
, sizeof(va_list));
167 __va_copy(count
, vargs
);
172 /* step 1: figure out how large a buffer we need */
173 for (f
= format
; *f
; f
++) {
176 while (*++f
&& *f
!= '%' && !isalpha(Py_CHARMASK(*f
)))
179 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
180 * they don't affect the amount of space we reserve.
182 if ((*f
== 'l' || *f
== 'z') &&
183 (f
[1] == 'd' || f
[1] == 'u'))
188 (void)va_arg(count
, int);
189 /* fall through... */
193 case 'd': case 'u': case 'i': case 'x':
194 (void) va_arg(count
, int);
195 /* 20 bytes is enough to hold a 64-bit
196 integer. Decimal takes the most space.
197 This isn't enough for octal. */
201 s
= va_arg(count
, char*);
205 (void) va_arg(count
, int);
206 /* maximum 64-bit pointer representation:
208 * so 19 characters is enough.
209 * XXX I count 18 -- what's the extra for?
214 /* if we stumble upon an unknown
215 formatting code, copy the rest of
216 the format string to the output
217 string. (we cannot just skip the
218 code, since there's no way to know
219 what's in the argument list) */
227 /* step 2: fill the buffer */
228 /* Since we've analyzed how much space we need for the worst case,
229 use sprintf directly instead of the slower PyOS_snprintf. */
230 string
= PyString_FromStringAndSize(NULL
, n
);
234 s
= PyString_AsString(string
);
236 for (f
= format
; *f
; f
++) {
242 /* parse the width.precision part (we're only
243 interested in the precision value, if any) */
245 while (isdigit(Py_CHARMASK(*f
)))
246 n
= (n
*10) + *f
++ - '0';
250 while (isdigit(Py_CHARMASK(*f
)))
251 n
= (n
*10) + *f
++ - '0';
253 while (*f
&& *f
!= '%' && !isalpha(Py_CHARMASK(*f
)))
255 /* handle the long flag, but only for %ld and %lu.
256 others can be added when necessary. */
257 if (*f
== 'l' && (f
[1] == 'd' || f
[1] == 'u')) {
261 /* handle the size_t flag. */
262 if (*f
== 'z' && (f
[1] == 'd' || f
[1] == 'u')) {
269 *s
++ = va_arg(vargs
, int);
273 sprintf(s
, "%ld", va_arg(vargs
, long));
275 sprintf(s
, "%" PY_FORMAT_SIZE_T
"d",
276 va_arg(vargs
, Py_ssize_t
));
278 sprintf(s
, "%d", va_arg(vargs
, int));
284 va_arg(vargs
, unsigned long));
286 sprintf(s
, "%" PY_FORMAT_SIZE_T
"u",
287 va_arg(vargs
, size_t));
290 va_arg(vargs
, unsigned int));
294 sprintf(s
, "%i", va_arg(vargs
, int));
298 sprintf(s
, "%x", va_arg(vargs
, int));
302 p
= va_arg(vargs
, char*);
310 sprintf(s
, "%p", va_arg(vargs
, void*));
311 /* %p is ill-defined: ensure leading 0x. */
314 else if (s
[1] != 'x') {
315 memmove(s
+2, s
, strlen(s
)+1);
334 _PyString_Resize(&string
, s
- PyString_AS_STRING(string
));
339 PyString_FromFormat(const char *format
, ...)
344 #ifdef HAVE_STDARG_PROTOTYPES
345 va_start(vargs
, format
);
349 ret
= PyString_FromFormatV(format
, vargs
);
355 PyObject
*PyString_Decode(const char *s
,
357 const char *encoding
,
362 str
= PyString_FromStringAndSize(s
, size
);
365 v
= PyString_AsDecodedString(str
, encoding
, errors
);
370 PyObject
*PyString_AsDecodedObject(PyObject
*str
,
371 const char *encoding
,
376 if (!PyString_Check(str
)) {
381 if (encoding
== NULL
) {
382 #ifdef Py_USING_UNICODE
383 encoding
= PyUnicode_GetDefaultEncoding();
385 PyErr_SetString(PyExc_ValueError
, "no encoding specified");
390 /* Decode via the codec registry */
391 v
= PyCodec_Decode(str
, encoding
, errors
);
401 PyObject
*PyString_AsDecodedString(PyObject
*str
,
402 const char *encoding
,
407 v
= PyString_AsDecodedObject(str
, encoding
, errors
);
411 #ifdef Py_USING_UNICODE
412 /* Convert Unicode to a string using the default encoding */
413 if (PyUnicode_Check(v
)) {
415 v
= PyUnicode_AsEncodedString(v
, NULL
, NULL
);
421 if (!PyString_Check(v
)) {
422 PyErr_Format(PyExc_TypeError
,
423 "decoder did not return a string object (type=%.400s)",
424 Py_Type(v
)->tp_name
);
435 PyObject
*PyString_Encode(const char *s
,
437 const char *encoding
,
442 str
= PyString_FromStringAndSize(s
, size
);
445 v
= PyString_AsEncodedString(str
, encoding
, errors
);
450 PyObject
*PyString_AsEncodedObject(PyObject
*str
,
451 const char *encoding
,
456 if (!PyString_Check(str
)) {
461 if (encoding
== NULL
) {
462 #ifdef Py_USING_UNICODE
463 encoding
= PyUnicode_GetDefaultEncoding();
465 PyErr_SetString(PyExc_ValueError
, "no encoding specified");
470 /* Encode via the codec registry */
471 v
= PyCodec_Encode(str
, encoding
, errors
);
481 PyObject
*PyString_AsEncodedString(PyObject
*str
,
482 const char *encoding
,
487 v
= PyString_AsEncodedObject(str
, encoding
, errors
);
491 #ifdef Py_USING_UNICODE
492 /* Convert Unicode to a string using the default encoding */
493 if (PyUnicode_Check(v
)) {
495 v
= PyUnicode_AsEncodedString(v
, NULL
, NULL
);
501 if (!PyString_Check(v
)) {
502 PyErr_Format(PyExc_TypeError
,
503 "encoder did not return a string object (type=%.400s)",
504 Py_Type(v
)->tp_name
);
516 string_dealloc(PyObject
*op
)
518 switch (PyString_CHECK_INTERNED(op
)) {
519 case SSTATE_NOT_INTERNED
:
522 case SSTATE_INTERNED_MORTAL
:
523 /* revive dead object temporarily for DelItem */
525 if (PyDict_DelItem(interned
, op
) != 0)
527 "deletion of interned string failed");
530 case SSTATE_INTERNED_IMMORTAL
:
531 Py_FatalError("Immortal interned string died.");
534 Py_FatalError("Inconsistent interned string state.");
536 Py_Type(op
)->tp_free(op
);
539 /* Unescape a backslash-escaped string. If unicode is non-zero,
540 the string is a u-literal. If recode_encoding is non-zero,
541 the string is UTF-8 encoded and should be re-encoded in the
542 specified encoding. */
544 PyObject
*PyString_DecodeEscape(const char *s
,
548 const char *recode_encoding
)
554 Py_ssize_t newlen
= recode_encoding
? 4*len
:len
;
555 v
= PyString_FromStringAndSize((char *)NULL
, newlen
);
558 p
= buf
= PyString_AsString(v
);
563 #ifdef Py_USING_UNICODE
564 if (recode_encoding
&& (*s
& 0x80)) {
570 /* Decode non-ASCII bytes as UTF-8. */
571 while (t
< end
&& (*t
& 0x80)) t
++;
572 u
= PyUnicode_DecodeUTF8(s
, t
- s
, errors
);
575 /* Recode them in target encoding. */
576 w
= PyUnicode_AsEncodedString(
577 u
, recode_encoding
, errors
);
581 /* Append bytes to output buffer. */
582 assert(PyString_Check(w
));
583 r
= PyString_AS_STRING(w
);
584 rn
= PyString_GET_SIZE(w
);
599 PyErr_SetString(PyExc_ValueError
,
600 "Trailing \\ in string");
604 /* XXX This assumes ASCII! */
606 case '\\': *p
++ = '\\'; break;
607 case '\'': *p
++ = '\''; break;
608 case '\"': *p
++ = '\"'; break;
609 case 'b': *p
++ = '\b'; break;
610 case 'f': *p
++ = '\014'; break; /* FF */
611 case 't': *p
++ = '\t'; break;
612 case 'n': *p
++ = '\n'; break;
613 case 'r': *p
++ = '\r'; break;
614 case 'v': *p
++ = '\013'; break; /* VT */
615 case 'a': *p
++ = '\007'; break; /* BEL, not classic C */
616 case '0': case '1': case '2': case '3':
617 case '4': case '5': case '6': case '7':
619 if ('0' <= *s
&& *s
<= '7') {
620 c
= (c
<<3) + *s
++ - '0';
621 if ('0' <= *s
&& *s
<= '7')
622 c
= (c
<<3) + *s
++ - '0';
627 if (isxdigit(Py_CHARMASK(s
[0]))
628 && isxdigit(Py_CHARMASK(s
[1]))) {
650 if (!errors
|| strcmp(errors
, "strict") == 0) {
651 PyErr_SetString(PyExc_ValueError
,
652 "invalid \\x escape");
655 if (strcmp(errors
, "replace") == 0) {
657 } else if (strcmp(errors
, "ignore") == 0)
660 PyErr_Format(PyExc_ValueError
,
662 "unknown error handling code: %.400s",
666 #ifndef Py_USING_UNICODE
671 PyErr_SetString(PyExc_ValueError
,
672 "Unicode escapes not legal "
673 "when Unicode disabled");
680 goto non_esc
; /* an arbitry number of unescaped
681 UTF-8 bytes may follow. */
685 _PyString_Resize(&v
, p
- buf
);
692 /* -------------------------------------------------------------------- */
696 string_getsize(register PyObject
*op
)
700 if (PyString_AsStringAndSize(op
, &s
, &len
))
705 static /*const*/ char *
706 string_getbuffer(register PyObject
*op
)
710 if (PyString_AsStringAndSize(op
, &s
, &len
))
716 PyString_Size(register PyObject
*op
)
718 if (!PyString_Check(op
))
719 return string_getsize(op
);
724 PyString_AsString(register PyObject
*op
)
726 if (!PyString_Check(op
))
727 return string_getbuffer(op
);
728 return ((PyStringObject
*)op
) -> ob_sval
;
732 PyString_AsStringAndSize(register PyObject
*obj
,
734 register Py_ssize_t
*len
)
737 PyErr_BadInternalCall();
741 if (!PyString_Check(obj
)) {
742 #ifdef Py_USING_UNICODE
743 if (PyUnicode_Check(obj
)) {
744 obj
= _PyUnicode_AsDefaultEncodedString(obj
, NULL
);
751 PyErr_Format(PyExc_TypeError
,
752 "expected string or Unicode object, "
753 "%.200s found", Py_Type(obj
)->tp_name
);
758 *s
= PyString_AS_STRING(obj
);
760 *len
= PyString_GET_SIZE(obj
);
761 else if (strlen(*s
) != (size_t)PyString_GET_SIZE(obj
)) {
762 PyErr_SetString(PyExc_TypeError
,
763 "expected string without null bytes");
769 /* -------------------------------------------------------------------- */
772 #define STRINGLIB_CHAR char
774 #define STRINGLIB_CMP memcmp
775 #define STRINGLIB_LEN PyString_GET_SIZE
776 #define STRINGLIB_NEW PyString_FromStringAndSize
777 #define STRINGLIB_STR PyString_AS_STRING
779 #define STRINGLIB_EMPTY nullstring
781 #include "stringlib/fastsearch.h"
783 #include "stringlib/count.h"
784 #include "stringlib/find.h"
785 #include "stringlib/partition.h"
789 string_print(PyStringObject
*op
, FILE *fp
, int flags
)
791 Py_ssize_t i
, str_len
;
795 /* XXX Ought to check for interrupts when writing long strings */
796 if (! PyString_CheckExact(op
)) {
798 /* A str subclass may have its own __str__ method. */
799 op
= (PyStringObject
*) PyObject_Str((PyObject
*)op
);
802 ret
= string_print(op
, fp
, flags
);
806 if (flags
& Py_PRINT_RAW
) {
807 char *data
= op
->ob_sval
;
808 Py_ssize_t size
= Py_Size(op
);
809 Py_BEGIN_ALLOW_THREADS
810 while (size
> INT_MAX
) {
811 /* Very long strings cannot be written atomically.
812 * But don't write exactly INT_MAX bytes at a time
813 * to avoid memory aligment issues.
815 const int chunk_size
= INT_MAX
& ~0x3FFF;
816 fwrite(data
, 1, chunk_size
, fp
);
821 if (size
) fwrite(data
, (int)size
, 1, fp
);
823 fwrite(data
, 1, (int)size
, fp
);
829 /* figure out which quote to use; single is preferred */
831 if (memchr(op
->ob_sval
, '\'', Py_Size(op
)) &&
832 !memchr(op
->ob_sval
, '"', Py_Size(op
)))
835 str_len
= Py_Size(op
);
836 Py_BEGIN_ALLOW_THREADS
838 for (i
= 0; i
< str_len
; i
++) {
839 /* Since strings are immutable and the caller should have a
840 reference, accessing the interal buffer should not be an issue
841 with the GIL released. */
843 if (c
== quote
|| c
== '\\')
844 fprintf(fp
, "\\%c", c
);
851 else if (c
< ' ' || c
>= 0x7f)
852 fprintf(fp
, "\\x%02x", c
& 0xff);
862 PyString_Repr(PyObject
*obj
, int smartquotes
)
864 register PyStringObject
* op
= (PyStringObject
*) obj
;
865 size_t newsize
= 2 + 4 * Py_Size(op
);
867 if (newsize
> PY_SSIZE_T_MAX
|| newsize
/ 4 != Py_Size(op
)) {
868 PyErr_SetString(PyExc_OverflowError
,
869 "string is too large to make repr");
871 v
= PyString_FromStringAndSize((char *)NULL
, newsize
);
876 register Py_ssize_t i
;
881 /* figure out which quote to use; single is preferred */
884 memchr(op
->ob_sval
, '\'', Py_Size(op
)) &&
885 !memchr(op
->ob_sval
, '"', Py_Size(op
)))
888 p
= PyString_AS_STRING(v
);
890 for (i
= 0; i
< Py_Size(op
); i
++) {
891 /* There's at least enough room for a hex escape
892 and a closing quote. */
893 assert(newsize
- (p
- PyString_AS_STRING(v
)) >= 5);
895 if (c
== quote
|| c
== '\\')
896 *p
++ = '\\', *p
++ = c
;
898 *p
++ = '\\', *p
++ = 't';
900 *p
++ = '\\', *p
++ = 'n';
902 *p
++ = '\\', *p
++ = 'r';
903 else if (c
< ' ' || c
>= 0x7f) {
904 /* For performance, we don't want to call
905 PyOS_snprintf here (extra layers of
907 sprintf(p
, "\\x%02x", c
& 0xff);
913 assert(newsize
- (p
- PyString_AS_STRING(v
)) >= 1);
917 &v
, (p
- PyString_AS_STRING(v
)));
923 string_repr(PyObject
*op
)
925 return PyString_Repr(op
, 1);
929 string_str(PyObject
*s
)
931 assert(PyString_Check(s
));
932 if (PyString_CheckExact(s
)) {
937 /* Subtype -- return genuine string with the same value. */
938 PyStringObject
*t
= (PyStringObject
*) s
;
939 return PyString_FromStringAndSize(t
->ob_sval
, Py_Size(t
));
944 string_length(PyStringObject
*a
)
950 string_concat(register PyStringObject
*a
, register PyObject
*bb
)
952 register Py_ssize_t size
;
953 register PyStringObject
*op
;
954 if (!PyString_Check(bb
)) {
955 #ifdef Py_USING_UNICODE
956 if (PyUnicode_Check(bb
))
957 return PyUnicode_Concat((PyObject
*)a
, bb
);
959 PyErr_Format(PyExc_TypeError
,
960 "cannot concatenate 'str' and '%.200s' objects",
961 Py_Type(bb
)->tp_name
);
964 #define b ((PyStringObject *)bb)
965 /* Optimize cases with empty left or right operand */
966 if ((Py_Size(a
) == 0 || Py_Size(b
) == 0) &&
967 PyString_CheckExact(a
) && PyString_CheckExact(b
)) {
968 if (Py_Size(a
) == 0) {
973 return (PyObject
*)a
;
975 size
= Py_Size(a
) + Py_Size(b
);
977 PyErr_SetString(PyExc_OverflowError
,
978 "strings are too large to concat");
982 /* Inline PyObject_NewVar */
983 op
= (PyStringObject
*)PyObject_MALLOC(sizeof(PyStringObject
) + size
);
985 return PyErr_NoMemory();
986 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
988 op
->ob_sstate
= SSTATE_NOT_INTERNED
;
989 Py_MEMCPY(op
->ob_sval
, a
->ob_sval
, Py_Size(a
));
990 Py_MEMCPY(op
->ob_sval
+ Py_Size(a
), b
->ob_sval
, Py_Size(b
));
991 op
->ob_sval
[size
] = '\0';
992 return (PyObject
*) op
;
997 string_repeat(register PyStringObject
*a
, register Py_ssize_t n
)
999 register Py_ssize_t i
;
1000 register Py_ssize_t j
;
1001 register Py_ssize_t size
;
1002 register PyStringObject
*op
;
1006 /* watch out for overflows: the size can overflow int,
1007 * and the # of bytes needed can overflow size_t
1009 size
= Py_Size(a
) * n
;
1010 if (n
&& size
/ n
!= Py_Size(a
)) {
1011 PyErr_SetString(PyExc_OverflowError
,
1012 "repeated string is too long");
1015 if (size
== Py_Size(a
) && PyString_CheckExact(a
)) {
1017 return (PyObject
*)a
;
1019 nbytes
= (size_t)size
;
1020 if (nbytes
+ sizeof(PyStringObject
) <= nbytes
) {
1021 PyErr_SetString(PyExc_OverflowError
,
1022 "repeated string is too long");
1025 op
= (PyStringObject
*)
1026 PyObject_MALLOC(sizeof(PyStringObject
) + nbytes
);
1028 return PyErr_NoMemory();
1029 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
1031 op
->ob_sstate
= SSTATE_NOT_INTERNED
;
1032 op
->ob_sval
[size
] = '\0';
1033 if (Py_Size(a
) == 1 && n
> 0) {
1034 memset(op
->ob_sval
, a
->ob_sval
[0] , n
);
1035 return (PyObject
*) op
;
1039 Py_MEMCPY(op
->ob_sval
, a
->ob_sval
, Py_Size(a
));
1043 j
= (i
<= size
-i
) ? i
: size
-i
;
1044 Py_MEMCPY(op
->ob_sval
+i
, op
->ob_sval
, j
);
1047 return (PyObject
*) op
;
1050 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
1053 string_slice(register PyStringObject
*a
, register Py_ssize_t i
,
1054 register Py_ssize_t j
)
1055 /* j -- may be negative! */
1060 j
= 0; /* Avoid signed/unsigned bug in next line */
1063 if (i
== 0 && j
== Py_Size(a
) && PyString_CheckExact(a
)) {
1064 /* It's the same as a */
1066 return (PyObject
*)a
;
1070 return PyString_FromStringAndSize(a
->ob_sval
+ i
, j
-i
);
1074 string_contains(PyObject
*str_obj
, PyObject
*sub_obj
)
1076 if (!PyString_CheckExact(sub_obj
)) {
1077 #ifdef Py_USING_UNICODE
1078 if (PyUnicode_Check(sub_obj
))
1079 return PyUnicode_Contains(str_obj
, sub_obj
);
1081 if (!PyString_Check(sub_obj
)) {
1082 PyErr_Format(PyExc_TypeError
,
1083 "'in <string>' requires string as left operand, "
1084 "not %.200s", Py_Type(sub_obj
)->tp_name
);
1089 return stringlib_contains_obj(str_obj
, sub_obj
);
1093 string_item(PyStringObject
*a
, register Py_ssize_t i
)
1097 if (i
< 0 || i
>= Py_Size(a
)) {
1098 PyErr_SetString(PyExc_IndexError
, "string index out of range");
1101 pchar
= a
->ob_sval
[i
];
1102 v
= (PyObject
*)characters
[pchar
& UCHAR_MAX
];
1104 v
= PyString_FromStringAndSize(&pchar
, 1);
1115 string_richcompare(PyStringObject
*a
, PyStringObject
*b
, int op
)
1118 Py_ssize_t len_a
, len_b
;
1122 /* Make sure both arguments are strings. */
1123 if (!(PyString_Check(a
) && PyString_Check(b
))) {
1124 result
= Py_NotImplemented
;
1129 case Py_EQ
:case Py_LE
:case Py_GE
:
1132 case Py_NE
:case Py_LT
:case Py_GT
:
1138 /* Supporting Py_NE here as well does not save
1139 much time, since Py_NE is rarely used. */
1140 if (Py_Size(a
) == Py_Size(b
)
1141 && (a
->ob_sval
[0] == b
->ob_sval
[0]
1142 && memcmp(a
->ob_sval
, b
->ob_sval
, Py_Size(a
)) == 0)) {
1149 len_a
= Py_Size(a
); len_b
= Py_Size(b
);
1150 min_len
= (len_a
< len_b
) ? len_a
: len_b
;
1152 c
= Py_CHARMASK(*a
->ob_sval
) - Py_CHARMASK(*b
->ob_sval
);
1154 c
= memcmp(a
->ob_sval
, b
->ob_sval
, min_len
);
1158 c
= (len_a
< len_b
) ? -1 : (len_a
> len_b
) ? 1 : 0;
1160 case Py_LT
: c
= c
< 0; break;
1161 case Py_LE
: c
= c
<= 0; break;
1162 case Py_EQ
: assert(0); break; /* unreachable */
1163 case Py_NE
: c
= c
!= 0; break;
1164 case Py_GT
: c
= c
> 0; break;
1165 case Py_GE
: c
= c
>= 0; break;
1167 result
= Py_NotImplemented
;
1170 result
= c
? Py_True
: Py_False
;
1177 _PyString_Eq(PyObject
*o1
, PyObject
*o2
)
1179 PyStringObject
*a
= (PyStringObject
*) o1
;
1180 PyStringObject
*b
= (PyStringObject
*) o2
;
1181 return Py_Size(a
) == Py_Size(b
)
1182 && *a
->ob_sval
== *b
->ob_sval
1183 && memcmp(a
->ob_sval
, b
->ob_sval
, Py_Size(a
)) == 0;
1187 string_hash(PyStringObject
*a
)
1189 register Py_ssize_t len
;
1190 register unsigned char *p
;
1193 if (a
->ob_shash
!= -1)
1196 p
= (unsigned char *) a
->ob_sval
;
1199 x
= (1000003*x
) ^ *p
++;
1208 string_subscript(PyStringObject
* self
, PyObject
* item
)
1210 if (PyIndex_Check(item
)) {
1211 Py_ssize_t i
= PyNumber_AsSsize_t(item
, PyExc_IndexError
);
1212 if (i
== -1 && PyErr_Occurred())
1215 i
+= PyString_GET_SIZE(self
);
1216 return string_item(self
, i
);
1218 else if (PySlice_Check(item
)) {
1219 Py_ssize_t start
, stop
, step
, slicelength
, cur
, i
;
1224 if (PySlice_GetIndicesEx((PySliceObject
*)item
,
1225 PyString_GET_SIZE(self
),
1226 &start
, &stop
, &step
, &slicelength
) < 0) {
1230 if (slicelength
<= 0) {
1231 return PyString_FromStringAndSize("", 0);
1233 else if (start
== 0 && step
== 1 &&
1234 slicelength
== PyString_GET_SIZE(self
) &&
1235 PyString_CheckExact(self
)) {
1237 return (PyObject
*)self
;
1239 else if (step
== 1) {
1240 return PyString_FromStringAndSize(
1241 PyString_AS_STRING(self
) + start
,
1245 source_buf
= PyString_AsString((PyObject
*)self
);
1246 result_buf
= (char *)PyMem_Malloc(slicelength
);
1247 if (result_buf
== NULL
)
1248 return PyErr_NoMemory();
1250 for (cur
= start
, i
= 0; i
< slicelength
;
1252 result_buf
[i
] = source_buf
[cur
];
1255 result
= PyString_FromStringAndSize(result_buf
,
1257 PyMem_Free(result_buf
);
1262 PyErr_Format(PyExc_TypeError
,
1263 "string indices must be integers, not %.200s",
1264 Py_Type(item
)->tp_name
);
1270 string_buffer_getreadbuf(PyStringObject
*self
, Py_ssize_t index
, const void **ptr
)
1273 PyErr_SetString(PyExc_SystemError
,
1274 "accessing non-existent string segment");
1277 *ptr
= (void *)self
->ob_sval
;
1278 return Py_Size(self
);
1282 string_buffer_getwritebuf(PyStringObject
*self
, Py_ssize_t index
, const void **ptr
)
1284 PyErr_SetString(PyExc_TypeError
,
1285 "Cannot use string as modifiable buffer");
1290 string_buffer_getsegcount(PyStringObject
*self
, Py_ssize_t
*lenp
)
1293 *lenp
= Py_Size(self
);
1298 string_buffer_getcharbuf(PyStringObject
*self
, Py_ssize_t index
, const char **ptr
)
1301 PyErr_SetString(PyExc_SystemError
,
1302 "accessing non-existent string segment");
1305 *ptr
= self
->ob_sval
;
1306 return Py_Size(self
);
1309 static PySequenceMethods string_as_sequence
= {
1310 (lenfunc
)string_length
, /*sq_length*/
1311 (binaryfunc
)string_concat
, /*sq_concat*/
1312 (ssizeargfunc
)string_repeat
, /*sq_repeat*/
1313 (ssizeargfunc
)string_item
, /*sq_item*/
1314 (ssizessizeargfunc
)string_slice
, /*sq_slice*/
1317 (objobjproc
)string_contains
/*sq_contains*/
1320 static PyMappingMethods string_as_mapping
= {
1321 (lenfunc
)string_length
,
1322 (binaryfunc
)string_subscript
,
1326 static PyBufferProcs string_as_buffer
= {
1327 (readbufferproc
)string_buffer_getreadbuf
,
1328 (writebufferproc
)string_buffer_getwritebuf
,
1329 (segcountproc
)string_buffer_getsegcount
,
1330 (charbufferproc
)string_buffer_getcharbuf
,
1336 #define RIGHTSTRIP 1
1339 /* Arrays indexed by above */
1340 static const char *stripformat
[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
1342 #define STRIPNAME(i) (stripformat[i]+3)
1345 /* Don't call if length < 2 */
1346 #define Py_STRING_MATCH(target, offset, pattern, length) \
1347 (target[offset] == pattern[0] && \
1348 target[offset+length-1] == pattern[length-1] && \
1349 !memcmp(target+offset+1, pattern+1, length-2) )
1352 /* Overallocate the initial list to reduce the number of reallocs for small
1353 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
1354 resizes, to sizes 4, 8, then 16. Most observed string splits are for human
1355 text (roughly 11 words per line) and field delimited data (usually 1-10
1356 fields). For large strings the split algorithms are bandwidth limited
1357 so increasing the preallocation likely will not improve things.*/
1359 #define MAX_PREALLOC 12
1361 /* 5 splits gives 6 elements */
1362 #define PREALLOC_SIZE(maxsplit) \
1363 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
1365 #define SPLIT_APPEND(data, left, right) \
1366 str = PyString_FromStringAndSize((data) + (left), \
1367 (right) - (left)); \
1370 if (PyList_Append(list, str)) { \
1377 #define SPLIT_ADD(data, left, right) { \
1378 str = PyString_FromStringAndSize((data) + (left), \
1379 (right) - (left)); \
1382 if (count < MAX_PREALLOC) { \
1383 PyList_SET_ITEM(list, count, str); \
1385 if (PyList_Append(list, str)) { \
1394 /* Always force the list to the expected size. */
1395 #define FIX_PREALLOC_SIZE(list) Py_Size(list) = count
1397 #define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; }
1398 #define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; }
1399 #define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; }
1400 #define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; }
1402 Py_LOCAL_INLINE(PyObject
*)
1403 split_whitespace(const char *s
, Py_ssize_t len
, Py_ssize_t maxsplit
)
1405 Py_ssize_t i
, j
, count
=0;
1407 PyObject
*list
= PyList_New(PREALLOC_SIZE(maxsplit
));
1414 while (maxsplit
-- > 0) {
1415 SKIP_SPACE(s
, i
, len
);
1418 SKIP_NONSPACE(s
, i
, len
);
1423 /* Only occurs when maxsplit was reached */
1424 /* Skip any remaining whitespace and copy to end of string */
1425 SKIP_SPACE(s
, i
, len
);
1427 SPLIT_ADD(s
, i
, len
);
1429 FIX_PREALLOC_SIZE(list
);
1436 Py_LOCAL_INLINE(PyObject
*)
1437 split_char(const char *s
, Py_ssize_t len
, char ch
, Py_ssize_t maxcount
)
1439 register Py_ssize_t i
, j
, count
=0;
1441 PyObject
*list
= PyList_New(PREALLOC_SIZE(maxcount
));
1447 while ((j
< len
) && (maxcount
-- > 0)) {
1449 /* I found that using memchr makes no difference */
1458 SPLIT_ADD(s
, i
, len
);
1460 FIX_PREALLOC_SIZE(list
);
1468 PyDoc_STRVAR(split__doc__
,
1469 "S.split([sep [,maxsplit]]) -> list of strings\n\
1471 Return a list of the words in the string S, using sep as the\n\
1472 delimiter string. If maxsplit is given, at most maxsplit\n\
1473 splits are done. If sep is not specified or is None, any\n\
1474 whitespace string is a separator.");
1477 string_split(PyStringObject
*self
, PyObject
*args
)
1479 Py_ssize_t len
= PyString_GET_SIZE(self
), n
, i
, j
;
1480 Py_ssize_t maxsplit
= -1, count
=0;
1481 const char *s
= PyString_AS_STRING(self
), *sub
;
1482 PyObject
*list
, *str
, *subobj
= Py_None
;
1487 if (!PyArg_ParseTuple(args
, "|On:split", &subobj
, &maxsplit
))
1490 maxsplit
= PY_SSIZE_T_MAX
;
1491 if (subobj
== Py_None
)
1492 return split_whitespace(s
, len
, maxsplit
);
1493 if (PyString_Check(subobj
)) {
1494 sub
= PyString_AS_STRING(subobj
);
1495 n
= PyString_GET_SIZE(subobj
);
1497 #ifdef Py_USING_UNICODE
1498 else if (PyUnicode_Check(subobj
))
1499 return PyUnicode_Split((PyObject
*)self
, subobj
, maxsplit
);
1501 else if (PyObject_AsCharBuffer(subobj
, &sub
, &n
))
1505 PyErr_SetString(PyExc_ValueError
, "empty separator");
1509 return split_char(s
, len
, sub
[0], maxsplit
);
1511 list
= PyList_New(PREALLOC_SIZE(maxsplit
));
1517 while (maxsplit
-- > 0) {
1518 pos
= fastsearch(s
+i
, len
-i
, sub
, n
, FAST_SEARCH
);
1527 while ((j
+n
<= len
) && (maxsplit
-- > 0)) {
1528 for (; j
+n
<= len
; j
++) {
1529 if (Py_STRING_MATCH(s
, j
, sub
, n
)) {
1537 SPLIT_ADD(s
, i
, len
);
1538 FIX_PREALLOC_SIZE(list
);
1546 PyDoc_STRVAR(partition__doc__
,
1547 "S.partition(sep) -> (head, sep, tail)\n\
1549 Searches for the separator sep in S, and returns the part before it,\n\
1550 the separator itself, and the part after it. If the separator is not\n\
1551 found, returns S and two empty strings.");
1554 string_partition(PyStringObject
*self
, PyObject
*sep_obj
)
1559 if (PyString_Check(sep_obj
)) {
1560 sep
= PyString_AS_STRING(sep_obj
);
1561 sep_len
= PyString_GET_SIZE(sep_obj
);
1563 #ifdef Py_USING_UNICODE
1564 else if (PyUnicode_Check(sep_obj
))
1565 return PyUnicode_Partition((PyObject
*) self
, sep_obj
);
1567 else if (PyObject_AsCharBuffer(sep_obj
, &sep
, &sep_len
))
1570 return stringlib_partition(
1572 PyString_AS_STRING(self
), PyString_GET_SIZE(self
),
1573 sep_obj
, sep
, sep_len
1577 PyDoc_STRVAR(rpartition__doc__
,
1578 "S.rpartition(sep) -> (tail, sep, head)\n\
1580 Searches for the separator sep in S, starting at the end of S, and returns\n\
1581 the part before it, the separator itself, and the part after it. If the\n\
1582 separator is not found, returns two empty strings and S.");
1585 string_rpartition(PyStringObject
*self
, PyObject
*sep_obj
)
1590 if (PyString_Check(sep_obj
)) {
1591 sep
= PyString_AS_STRING(sep_obj
);
1592 sep_len
= PyString_GET_SIZE(sep_obj
);
1594 #ifdef Py_USING_UNICODE
1595 else if (PyUnicode_Check(sep_obj
))
1596 return PyUnicode_Partition((PyObject
*) self
, sep_obj
);
1598 else if (PyObject_AsCharBuffer(sep_obj
, &sep
, &sep_len
))
1601 return stringlib_rpartition(
1603 PyString_AS_STRING(self
), PyString_GET_SIZE(self
),
1604 sep_obj
, sep
, sep_len
1608 Py_LOCAL_INLINE(PyObject
*)
1609 rsplit_whitespace(const char *s
, Py_ssize_t len
, Py_ssize_t maxsplit
)
1611 Py_ssize_t i
, j
, count
=0;
1613 PyObject
*list
= PyList_New(PREALLOC_SIZE(maxsplit
));
1620 while (maxsplit
-- > 0) {
1624 RSKIP_NONSPACE(s
, i
);
1625 SPLIT_ADD(s
, i
+ 1, j
+ 1);
1628 /* Only occurs when maxsplit was reached */
1629 /* Skip any remaining whitespace and copy to beginning of string */
1632 SPLIT_ADD(s
, 0, i
+ 1);
1635 FIX_PREALLOC_SIZE(list
);
1636 if (PyList_Reverse(list
) < 0)
1644 Py_LOCAL_INLINE(PyObject
*)
1645 rsplit_char(const char *s
, Py_ssize_t len
, char ch
, Py_ssize_t maxcount
)
1647 register Py_ssize_t i
, j
, count
=0;
1649 PyObject
*list
= PyList_New(PREALLOC_SIZE(maxcount
));
1655 while ((i
>= 0) && (maxcount
-- > 0)) {
1656 for (; i
>= 0; i
--) {
1658 SPLIT_ADD(s
, i
+ 1, j
+ 1);
1665 SPLIT_ADD(s
, 0, j
+ 1);
1667 FIX_PREALLOC_SIZE(list
);
1668 if (PyList_Reverse(list
) < 0)
1677 PyDoc_STRVAR(rsplit__doc__
,
1678 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\
1680 Return a list of the words in the string S, using sep as the\n\
1681 delimiter string, starting at the end of the string and working\n\
1682 to the front. If maxsplit is given, at most maxsplit splits are\n\
1683 done. If sep is not specified or is None, any whitespace string\n\
1687 string_rsplit(PyStringObject
*self
, PyObject
*args
)
1689 Py_ssize_t len
= PyString_GET_SIZE(self
), n
, i
, j
;
1690 Py_ssize_t maxsplit
= -1, count
=0;
1691 const char *s
= PyString_AS_STRING(self
), *sub
;
1692 PyObject
*list
, *str
, *subobj
= Py_None
;
1694 if (!PyArg_ParseTuple(args
, "|On:rsplit", &subobj
, &maxsplit
))
1697 maxsplit
= PY_SSIZE_T_MAX
;
1698 if (subobj
== Py_None
)
1699 return rsplit_whitespace(s
, len
, maxsplit
);
1700 if (PyString_Check(subobj
)) {
1701 sub
= PyString_AS_STRING(subobj
);
1702 n
= PyString_GET_SIZE(subobj
);
1704 #ifdef Py_USING_UNICODE
1705 else if (PyUnicode_Check(subobj
))
1706 return PyUnicode_RSplit((PyObject
*)self
, subobj
, maxsplit
);
1708 else if (PyObject_AsCharBuffer(subobj
, &sub
, &n
))
1712 PyErr_SetString(PyExc_ValueError
, "empty separator");
1716 return rsplit_char(s
, len
, sub
[0], maxsplit
);
1718 list
= PyList_New(PREALLOC_SIZE(maxsplit
));
1725 while ( (i
>= 0) && (maxsplit
-- > 0) ) {
1727 if (Py_STRING_MATCH(s
, i
, sub
, n
)) {
1728 SPLIT_ADD(s
, i
+ n
, j
);
1736 FIX_PREALLOC_SIZE(list
);
1737 if (PyList_Reverse(list
) < 0)
1747 PyDoc_STRVAR(join__doc__
,
1748 "S.join(sequence) -> string\n\
1750 Return a string which is the concatenation of the strings in the\n\
1751 sequence. The separator between elements is S.");
1754 string_join(PyStringObject
*self
, PyObject
*orig
)
1756 char *sep
= PyString_AS_STRING(self
);
1757 const Py_ssize_t seplen
= PyString_GET_SIZE(self
);
1758 PyObject
*res
= NULL
;
1760 Py_ssize_t seqlen
= 0;
1763 PyObject
*seq
, *item
;
1765 seq
= PySequence_Fast(orig
, "");
1770 seqlen
= PySequence_Size(seq
);
1773 return PyString_FromString("");
1776 item
= PySequence_Fast_GET_ITEM(seq
, 0);
1777 if (PyString_CheckExact(item
) || PyUnicode_CheckExact(item
)) {
1784 /* There are at least two things to join, or else we have a subclass
1785 * of the builtin types in the sequence.
1786 * Do a pre-pass to figure out the total amount of space we'll
1787 * need (sz), see whether any argument is absurd, and defer to
1788 * the Unicode join if appropriate.
1790 for (i
= 0; i
< seqlen
; i
++) {
1791 const size_t old_sz
= sz
;
1792 item
= PySequence_Fast_GET_ITEM(seq
, i
);
1793 if (!PyString_Check(item
)){
1794 #ifdef Py_USING_UNICODE
1795 if (PyUnicode_Check(item
)) {
1796 /* Defer to Unicode join.
1797 * CAUTION: There's no gurantee that the
1798 * original sequence can be iterated over
1799 * again, so we must pass seq here.
1802 result
= PyUnicode_Join((PyObject
*)self
, seq
);
1807 PyErr_Format(PyExc_TypeError
,
1808 "sequence item %zd: expected string,"
1810 i
, Py_Type(item
)->tp_name
);
1814 sz
+= PyString_GET_SIZE(item
);
1817 if (sz
< old_sz
|| sz
> PY_SSIZE_T_MAX
) {
1818 PyErr_SetString(PyExc_OverflowError
,
1819 "join() result is too long for a Python string");
1825 /* Allocate result space. */
1826 res
= PyString_FromStringAndSize((char*)NULL
, sz
);
1832 /* Catenate everything. */
1833 p
= PyString_AS_STRING(res
);
1834 for (i
= 0; i
< seqlen
; ++i
) {
1836 item
= PySequence_Fast_GET_ITEM(seq
, i
);
1837 n
= PyString_GET_SIZE(item
);
1838 Py_MEMCPY(p
, PyString_AS_STRING(item
), n
);
1840 if (i
< seqlen
- 1) {
1841 Py_MEMCPY(p
, sep
, seplen
);
1851 _PyString_Join(PyObject
*sep
, PyObject
*x
)
1853 assert(sep
!= NULL
&& PyString_Check(sep
));
1855 return string_join((PyStringObject
*)sep
, x
);
1858 Py_LOCAL_INLINE(void)
1859 string_adjust_indices(Py_ssize_t
*start
, Py_ssize_t
*end
, Py_ssize_t len
)
1873 Py_LOCAL_INLINE(Py_ssize_t
)
1874 string_find_internal(PyStringObject
*self
, PyObject
*args
, int dir
)
1879 Py_ssize_t start
=0, end
=PY_SSIZE_T_MAX
;
1881 if (!PyArg_ParseTuple(args
, "O|O&O&:find/rfind/index/rindex", &subobj
,
1882 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
1884 if (PyString_Check(subobj
)) {
1885 sub
= PyString_AS_STRING(subobj
);
1886 sub_len
= PyString_GET_SIZE(subobj
);
1888 #ifdef Py_USING_UNICODE
1889 else if (PyUnicode_Check(subobj
))
1890 return PyUnicode_Find(
1891 (PyObject
*)self
, subobj
, start
, end
, dir
);
1893 else if (PyObject_AsCharBuffer(subobj
, &sub
, &sub_len
))
1894 /* XXX - the "expected a character buffer object" is pretty
1895 confusing for a non-expert. remap to something else ? */
1899 return stringlib_find_slice(
1900 PyString_AS_STRING(self
), PyString_GET_SIZE(self
),
1901 sub
, sub_len
, start
, end
);
1903 return stringlib_rfind_slice(
1904 PyString_AS_STRING(self
), PyString_GET_SIZE(self
),
1905 sub
, sub_len
, start
, end
);
1909 PyDoc_STRVAR(find__doc__
,
1910 "S.find(sub [,start [,end]]) -> int\n\
1912 Return the lowest index in S where substring sub is found,\n\
1913 such that sub is contained within s[start:end]. Optional\n\
1914 arguments start and end are interpreted as in slice notation.\n\
1916 Return -1 on failure.");
1919 string_find(PyStringObject
*self
, PyObject
*args
)
1921 Py_ssize_t result
= string_find_internal(self
, args
, +1);
1924 return PyInt_FromSsize_t(result
);
1928 PyDoc_STRVAR(index__doc__
,
1929 "S.index(sub [,start [,end]]) -> int\n\
1931 Like S.find() but raise ValueError when the substring is not found.");
1934 string_index(PyStringObject
*self
, PyObject
*args
)
1936 Py_ssize_t result
= string_find_internal(self
, args
, +1);
1940 PyErr_SetString(PyExc_ValueError
,
1941 "substring not found");
1944 return PyInt_FromSsize_t(result
);
1948 PyDoc_STRVAR(rfind__doc__
,
1949 "S.rfind(sub [,start [,end]]) -> int\n\
1951 Return the highest index in S where substring sub is found,\n\
1952 such that sub is contained within s[start:end]. Optional\n\
1953 arguments start and end are interpreted as in slice notation.\n\
1955 Return -1 on failure.");
1958 string_rfind(PyStringObject
*self
, PyObject
*args
)
1960 Py_ssize_t result
= string_find_internal(self
, args
, -1);
1963 return PyInt_FromSsize_t(result
);
1967 PyDoc_STRVAR(rindex__doc__
,
1968 "S.rindex(sub [,start [,end]]) -> int\n\
1970 Like S.rfind() but raise ValueError when the substring is not found.");
1973 string_rindex(PyStringObject
*self
, PyObject
*args
)
1975 Py_ssize_t result
= string_find_internal(self
, args
, -1);
1979 PyErr_SetString(PyExc_ValueError
,
1980 "substring not found");
1983 return PyInt_FromSsize_t(result
);
1987 Py_LOCAL_INLINE(PyObject
*)
1988 do_xstrip(PyStringObject
*self
, int striptype
, PyObject
*sepobj
)
1990 char *s
= PyString_AS_STRING(self
);
1991 Py_ssize_t len
= PyString_GET_SIZE(self
);
1992 char *sep
= PyString_AS_STRING(sepobj
);
1993 Py_ssize_t seplen
= PyString_GET_SIZE(sepobj
);
1997 if (striptype
!= RIGHTSTRIP
) {
1998 while (i
< len
&& memchr(sep
, Py_CHARMASK(s
[i
]), seplen
)) {
2004 if (striptype
!= LEFTSTRIP
) {
2007 } while (j
>= i
&& memchr(sep
, Py_CHARMASK(s
[j
]), seplen
));
2011 if (i
== 0 && j
== len
&& PyString_CheckExact(self
)) {
2013 return (PyObject
*)self
;
2016 return PyString_FromStringAndSize(s
+i
, j
-i
);
2020 Py_LOCAL_INLINE(PyObject
*)
2021 do_strip(PyStringObject
*self
, int striptype
)
2023 char *s
= PyString_AS_STRING(self
);
2024 Py_ssize_t len
= PyString_GET_SIZE(self
), i
, j
;
2027 if (striptype
!= RIGHTSTRIP
) {
2028 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
2034 if (striptype
!= LEFTSTRIP
) {
2037 } while (j
>= i
&& isspace(Py_CHARMASK(s
[j
])));
2041 if (i
== 0 && j
== len
&& PyString_CheckExact(self
)) {
2043 return (PyObject
*)self
;
2046 return PyString_FromStringAndSize(s
+i
, j
-i
);
2050 Py_LOCAL_INLINE(PyObject
*)
2051 do_argstrip(PyStringObject
*self
, int striptype
, PyObject
*args
)
2053 PyObject
*sep
= NULL
;
2055 if (!PyArg_ParseTuple(args
, (char *)stripformat
[striptype
], &sep
))
2058 if (sep
!= NULL
&& sep
!= Py_None
) {
2059 if (PyString_Check(sep
))
2060 return do_xstrip(self
, striptype
, sep
);
2061 #ifdef Py_USING_UNICODE
2062 else if (PyUnicode_Check(sep
)) {
2063 PyObject
*uniself
= PyUnicode_FromObject((PyObject
*)self
);
2067 res
= _PyUnicode_XStrip((PyUnicodeObject
*)uniself
,
2073 PyErr_Format(PyExc_TypeError
,
2074 #ifdef Py_USING_UNICODE
2075 "%s arg must be None, str or unicode",
2077 "%s arg must be None or str",
2079 STRIPNAME(striptype
));
2083 return do_strip(self
, striptype
);
2087 PyDoc_STRVAR(strip__doc__
,
2088 "S.strip([chars]) -> string or unicode\n\
2090 Return a copy of the string S with leading and trailing\n\
2091 whitespace removed.\n\
2092 If chars is given and not None, remove characters in chars instead.\n\
2093 If chars is unicode, S will be converted to unicode before stripping");
2096 string_strip(PyStringObject
*self
, PyObject
*args
)
2098 if (PyTuple_GET_SIZE(args
) == 0)
2099 return do_strip(self
, BOTHSTRIP
); /* Common case */
2101 return do_argstrip(self
, BOTHSTRIP
, args
);
2105 PyDoc_STRVAR(lstrip__doc__
,
2106 "S.lstrip([chars]) -> string or unicode\n\
2108 Return a copy of the string S with leading whitespace removed.\n\
2109 If chars is given and not None, remove characters in chars instead.\n\
2110 If chars is unicode, S will be converted to unicode before stripping");
2113 string_lstrip(PyStringObject
*self
, PyObject
*args
)
2115 if (PyTuple_GET_SIZE(args
) == 0)
2116 return do_strip(self
, LEFTSTRIP
); /* Common case */
2118 return do_argstrip(self
, LEFTSTRIP
, args
);
2122 PyDoc_STRVAR(rstrip__doc__
,
2123 "S.rstrip([chars]) -> string or unicode\n\
2125 Return a copy of the string S with trailing whitespace removed.\n\
2126 If chars is given and not None, remove characters in chars instead.\n\
2127 If chars is unicode, S will be converted to unicode before stripping");
2130 string_rstrip(PyStringObject
*self
, PyObject
*args
)
2132 if (PyTuple_GET_SIZE(args
) == 0)
2133 return do_strip(self
, RIGHTSTRIP
); /* Common case */
2135 return do_argstrip(self
, RIGHTSTRIP
, args
);
2139 PyDoc_STRVAR(lower__doc__
,
2140 "S.lower() -> string\n\
2142 Return a copy of the string S converted to lowercase.");
2144 /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */
2146 #define _tolower tolower
2150 string_lower(PyStringObject
*self
)
2153 Py_ssize_t i
, n
= PyString_GET_SIZE(self
);
2156 newobj
= PyString_FromStringAndSize(NULL
, n
);
2160 s
= PyString_AS_STRING(newobj
);
2162 Py_MEMCPY(s
, PyString_AS_STRING(self
), n
);
2164 for (i
= 0; i
< n
; i
++) {
2165 int c
= Py_CHARMASK(s
[i
]);
2173 PyDoc_STRVAR(upper__doc__
,
2174 "S.upper() -> string\n\
2176 Return a copy of the string S converted to uppercase.");
2179 #define _toupper toupper
2183 string_upper(PyStringObject
*self
)
2186 Py_ssize_t i
, n
= PyString_GET_SIZE(self
);
2189 newobj
= PyString_FromStringAndSize(NULL
, n
);
2193 s
= PyString_AS_STRING(newobj
);
2195 Py_MEMCPY(s
, PyString_AS_STRING(self
), n
);
2197 for (i
= 0; i
< n
; i
++) {
2198 int c
= Py_CHARMASK(s
[i
]);
2206 PyDoc_STRVAR(title__doc__
,
2207 "S.title() -> string\n\
2209 Return a titlecased version of S, i.e. words start with uppercase\n\
2210 characters, all remaining cased characters have lowercase.");
2213 string_title(PyStringObject
*self
)
2215 char *s
= PyString_AS_STRING(self
), *s_new
;
2216 Py_ssize_t i
, n
= PyString_GET_SIZE(self
);
2217 int previous_is_cased
= 0;
2220 newobj
= PyString_FromStringAndSize(NULL
, n
);
2223 s_new
= PyString_AsString(newobj
);
2224 for (i
= 0; i
< n
; i
++) {
2225 int c
= Py_CHARMASK(*s
++);
2227 if (!previous_is_cased
)
2229 previous_is_cased
= 1;
2230 } else if (isupper(c
)) {
2231 if (previous_is_cased
)
2233 previous_is_cased
= 1;
2235 previous_is_cased
= 0;
2241 PyDoc_STRVAR(capitalize__doc__
,
2242 "S.capitalize() -> string\n\
2244 Return a copy of the string S with only its first character\n\
2248 string_capitalize(PyStringObject
*self
)
2250 char *s
= PyString_AS_STRING(self
), *s_new
;
2251 Py_ssize_t i
, n
= PyString_GET_SIZE(self
);
2254 newobj
= PyString_FromStringAndSize(NULL
, n
);
2257 s_new
= PyString_AsString(newobj
);
2259 int c
= Py_CHARMASK(*s
++);
2261 *s_new
= toupper(c
);
2266 for (i
= 1; i
< n
; i
++) {
2267 int c
= Py_CHARMASK(*s
++);
2269 *s_new
= tolower(c
);
2278 PyDoc_STRVAR(count__doc__
,
2279 "S.count(sub[, start[, end]]) -> int\n\
2281 Return the number of non-overlapping occurrences of substring sub in\n\
2282 string S[start:end]. Optional arguments start and end are interpreted\n\
2283 as in slice notation.");
2286 string_count(PyStringObject
*self
, PyObject
*args
)
2289 const char *str
= PyString_AS_STRING(self
), *sub
;
2291 Py_ssize_t start
= 0, end
= PY_SSIZE_T_MAX
;
2293 if (!PyArg_ParseTuple(args
, "O|O&O&:count", &sub_obj
,
2294 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
2297 if (PyString_Check(sub_obj
)) {
2298 sub
= PyString_AS_STRING(sub_obj
);
2299 sub_len
= PyString_GET_SIZE(sub_obj
);
2301 #ifdef Py_USING_UNICODE
2302 else if (PyUnicode_Check(sub_obj
)) {
2304 count
= PyUnicode_Count((PyObject
*)self
, sub_obj
, start
, end
);
2308 return PyInt_FromSsize_t(count
);
2311 else if (PyObject_AsCharBuffer(sub_obj
, &sub
, &sub_len
))
2314 string_adjust_indices(&start
, &end
, PyString_GET_SIZE(self
));
2316 return PyInt_FromSsize_t(
2317 stringlib_count(str
+ start
, end
- start
, sub
, sub_len
)
2321 PyDoc_STRVAR(swapcase__doc__
,
2322 "S.swapcase() -> string\n\
2324 Return a copy of the string S with uppercase characters\n\
2325 converted to lowercase and vice versa.");
2328 string_swapcase(PyStringObject
*self
)
2330 char *s
= PyString_AS_STRING(self
), *s_new
;
2331 Py_ssize_t i
, n
= PyString_GET_SIZE(self
);
2334 newobj
= PyString_FromStringAndSize(NULL
, n
);
2337 s_new
= PyString_AsString(newobj
);
2338 for (i
= 0; i
< n
; i
++) {
2339 int c
= Py_CHARMASK(*s
++);
2341 *s_new
= toupper(c
);
2343 else if (isupper(c
)) {
2344 *s_new
= tolower(c
);
2354 PyDoc_STRVAR(translate__doc__
,
2355 "S.translate(table [,deletechars]) -> string\n\
2357 Return a copy of the string S, where all characters occurring\n\
2358 in the optional argument deletechars are removed, and the\n\
2359 remaining characters have been mapped through the given\n\
2360 translation table, which must be a string of length 256.");
2363 string_translate(PyStringObject
*self
, PyObject
*args
)
2365 register char *input
, *output
;
2367 register Py_ssize_t i
, c
, changed
= 0;
2368 PyObject
*input_obj
= (PyObject
*)self
;
2369 const char *output_start
, *del_table
=NULL
;
2370 Py_ssize_t inlen
, tablen
, dellen
= 0;
2372 int trans_table
[256];
2373 PyObject
*tableobj
, *delobj
= NULL
;
2375 if (!PyArg_UnpackTuple(args
, "translate", 1, 2,
2376 &tableobj
, &delobj
))
2379 if (PyString_Check(tableobj
)) {
2380 table
= PyString_AS_STRING(tableobj
);
2381 tablen
= PyString_GET_SIZE(tableobj
);
2383 else if (tableobj
== Py_None
) {
2387 #ifdef Py_USING_UNICODE
2388 else if (PyUnicode_Check(tableobj
)) {
2389 /* Unicode .translate() does not support the deletechars
2390 parameter; instead a mapping to None will cause characters
2392 if (delobj
!= NULL
) {
2393 PyErr_SetString(PyExc_TypeError
,
2394 "deletions are implemented differently for unicode");
2397 return PyUnicode_Translate((PyObject
*)self
, tableobj
, NULL
);
2400 else if (PyObject_AsCharBuffer(tableobj
, &table
, &tablen
))
2403 if (tablen
!= 256) {
2404 PyErr_SetString(PyExc_ValueError
,
2405 "translation table must be 256 characters long");
2409 if (delobj
!= NULL
) {
2410 if (PyString_Check(delobj
)) {
2411 del_table
= PyString_AS_STRING(delobj
);
2412 dellen
= PyString_GET_SIZE(delobj
);
2414 #ifdef Py_USING_UNICODE
2415 else if (PyUnicode_Check(delobj
)) {
2416 PyErr_SetString(PyExc_TypeError
,
2417 "deletions are implemented differently for unicode");
2421 else if (PyObject_AsCharBuffer(delobj
, &del_table
, &dellen
))
2429 inlen
= PyString_GET_SIZE(input_obj
);
2430 result
= PyString_FromStringAndSize((char *)NULL
, inlen
);
2433 output_start
= output
= PyString_AsString(result
);
2434 input
= PyString_AS_STRING(input_obj
);
2436 if (dellen
== 0 && table
!= NULL
) {
2437 /* If no deletions are required, use faster code */
2438 for (i
= inlen
; --i
>= 0; ) {
2439 c
= Py_CHARMASK(*input
++);
2440 if (Py_CHARMASK((*output
++ = table
[c
])) != c
)
2443 if (changed
|| !PyString_CheckExact(input_obj
))
2446 Py_INCREF(input_obj
);
2450 if (table
== NULL
) {
2451 for (i
= 0; i
< 256; i
++)
2452 trans_table
[i
] = Py_CHARMASK(i
);
2454 for (i
= 0; i
< 256; i
++)
2455 trans_table
[i
] = Py_CHARMASK(table
[i
]);
2458 for (i
= 0; i
< dellen
; i
++)
2459 trans_table
[(int) Py_CHARMASK(del_table
[i
])] = -1;
2461 for (i
= inlen
; --i
>= 0; ) {
2462 c
= Py_CHARMASK(*input
++);
2463 if (trans_table
[c
] != -1)
2464 if (Py_CHARMASK(*output
++ = (char)trans_table
[c
]) == c
)
2468 if (!changed
&& PyString_CheckExact(input_obj
)) {
2470 Py_INCREF(input_obj
);
2473 /* Fix the size of the resulting string */
2475 _PyString_Resize(&result
, output
- output_start
);
2483 /* find and count characters and substrings */
2485 #define findchar(target, target_len, c) \
2486 ((char *)memchr((const void *)(target), c, target_len))
2488 /* String ops must return a string. */
2489 /* If the object is subclass of string, create a copy */
2490 Py_LOCAL(PyStringObject
*)
2491 return_self(PyStringObject
*self
)
2493 if (PyString_CheckExact(self
)) {
2497 return (PyStringObject
*)PyString_FromStringAndSize(
2498 PyString_AS_STRING(self
),
2499 PyString_GET_SIZE(self
));
2502 Py_LOCAL_INLINE(Py_ssize_t
)
2503 countchar(const char *target
, int target_len
, char c
, Py_ssize_t maxcount
)
2506 const char *start
=target
;
2507 const char *end
=target
+target_len
;
2509 while ( (start
=findchar(start
, end
-start
, c
)) != NULL
) {
2511 if (count
>= maxcount
)
2518 Py_LOCAL(Py_ssize_t
)
2519 findstring(const char *target
, Py_ssize_t target_len
,
2520 const char *pattern
, Py_ssize_t pattern_len
,
2526 start
+= target_len
;
2530 if (end
> target_len
) {
2532 } else if (end
< 0) {
2538 /* zero-length substrings always match at the first attempt */
2539 if (pattern_len
== 0)
2540 return (direction
> 0) ? start
: end
;
2544 if (direction
< 0) {
2545 for (; end
>= start
; end
--)
2546 if (Py_STRING_MATCH(target
, end
, pattern
, pattern_len
))
2549 for (; start
<= end
; start
++)
2550 if (Py_STRING_MATCH(target
, start
, pattern
, pattern_len
))
2556 Py_LOCAL_INLINE(Py_ssize_t
)
2557 countstring(const char *target
, Py_ssize_t target_len
,
2558 const char *pattern
, Py_ssize_t pattern_len
,
2561 int direction
, Py_ssize_t maxcount
)
2566 start
+= target_len
;
2570 if (end
> target_len
) {
2572 } else if (end
< 0) {
2578 /* zero-length substrings match everywhere */
2579 if (pattern_len
== 0 || maxcount
== 0) {
2580 if (target_len
+1 < maxcount
)
2581 return target_len
+1;
2586 if (direction
< 0) {
2587 for (; (end
>= start
); end
--)
2588 if (Py_STRING_MATCH(target
, end
, pattern
, pattern_len
)) {
2590 if (--maxcount
<= 0) break;
2591 end
-= pattern_len
-1;
2594 for (; (start
<= end
); start
++)
2595 if (Py_STRING_MATCH(target
, start
, pattern
, pattern_len
)) {
2597 if (--maxcount
<= 0)
2599 start
+= pattern_len
-1;
2606 /* Algorithms for different cases of string replacement */
2608 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
2609 Py_LOCAL(PyStringObject
*)
2610 replace_interleave(PyStringObject
*self
,
2611 const char *to_s
, Py_ssize_t to_len
,
2612 Py_ssize_t maxcount
)
2614 char *self_s
, *result_s
;
2615 Py_ssize_t self_len
, result_len
;
2616 Py_ssize_t count
, i
, product
;
2617 PyStringObject
*result
;
2619 self_len
= PyString_GET_SIZE(self
);
2621 /* 1 at the end plus 1 after every character */
2623 if (maxcount
< count
)
2626 /* Check for overflow */
2627 /* result_len = count * to_len + self_len; */
2628 product
= count
* to_len
;
2629 if (product
/ to_len
!= count
) {
2630 PyErr_SetString(PyExc_OverflowError
,
2631 "replace string is too long");
2634 result_len
= product
+ self_len
;
2635 if (result_len
< 0) {
2636 PyErr_SetString(PyExc_OverflowError
,
2637 "replace string is too long");
2641 if (! (result
= (PyStringObject
*)
2642 PyString_FromStringAndSize(NULL
, result_len
)) )
2645 self_s
= PyString_AS_STRING(self
);
2646 result_s
= PyString_AS_STRING(result
);
2648 /* TODO: special case single character, which doesn't need memcpy */
2650 /* Lay the first one down (guaranteed this will occur) */
2651 Py_MEMCPY(result_s
, to_s
, to_len
);
2655 for (i
=0; i
<count
; i
++) {
2656 *result_s
++ = *self_s
++;
2657 Py_MEMCPY(result_s
, to_s
, to_len
);
2661 /* Copy the rest of the original string */
2662 Py_MEMCPY(result_s
, self_s
, self_len
-i
);
2667 /* Special case for deleting a single character */
2668 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */
2669 Py_LOCAL(PyStringObject
*)
2670 replace_delete_single_character(PyStringObject
*self
,
2671 char from_c
, Py_ssize_t maxcount
)
2673 char *self_s
, *result_s
;
2674 char *start
, *next
, *end
;
2675 Py_ssize_t self_len
, result_len
;
2677 PyStringObject
*result
;
2679 self_len
= PyString_GET_SIZE(self
);
2680 self_s
= PyString_AS_STRING(self
);
2682 count
= countchar(self_s
, self_len
, from_c
, maxcount
);
2684 return return_self(self
);
2687 result_len
= self_len
- count
; /* from_len == 1 */
2688 assert(result_len
>=0);
2690 if ( (result
= (PyStringObject
*)
2691 PyString_FromStringAndSize(NULL
, result_len
)) == NULL
)
2693 result_s
= PyString_AS_STRING(result
);
2696 end
= self_s
+ self_len
;
2697 while (count
-- > 0) {
2698 next
= findchar(start
, end
-start
, from_c
);
2701 Py_MEMCPY(result_s
, start
, next
-start
);
2702 result_s
+= (next
-start
);
2705 Py_MEMCPY(result_s
, start
, end
-start
);
2710 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
2712 Py_LOCAL(PyStringObject
*)
2713 replace_delete_substring(PyStringObject
*self
,
2714 const char *from_s
, Py_ssize_t from_len
,
2715 Py_ssize_t maxcount
) {
2716 char *self_s
, *result_s
;
2717 char *start
, *next
, *end
;
2718 Py_ssize_t self_len
, result_len
;
2719 Py_ssize_t count
, offset
;
2720 PyStringObject
*result
;
2722 self_len
= PyString_GET_SIZE(self
);
2723 self_s
= PyString_AS_STRING(self
);
2725 count
= countstring(self_s
, self_len
,
2732 return return_self(self
);
2735 result_len
= self_len
- (count
* from_len
);
2736 assert (result_len
>=0);
2738 if ( (result
= (PyStringObject
*)
2739 PyString_FromStringAndSize(NULL
, result_len
)) == NULL
)
2742 result_s
= PyString_AS_STRING(result
);
2745 end
= self_s
+ self_len
;
2746 while (count
-- > 0) {
2747 offset
= findstring(start
, end
-start
,
2749 0, end
-start
, FORWARD
);
2752 next
= start
+ offset
;
2754 Py_MEMCPY(result_s
, start
, next
-start
);
2756 result_s
+= (next
-start
);
2757 start
= next
+from_len
;
2759 Py_MEMCPY(result_s
, start
, end
-start
);
2763 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
2764 Py_LOCAL(PyStringObject
*)
2765 replace_single_character_in_place(PyStringObject
*self
,
2766 char from_c
, char to_c
,
2767 Py_ssize_t maxcount
)
2769 char *self_s
, *result_s
, *start
, *end
, *next
;
2770 Py_ssize_t self_len
;
2771 PyStringObject
*result
;
2773 /* The result string will be the same size */
2774 self_s
= PyString_AS_STRING(self
);
2775 self_len
= PyString_GET_SIZE(self
);
2777 next
= findchar(self_s
, self_len
, from_c
);
2780 /* No matches; return the original string */
2781 return return_self(self
);
2784 /* Need to make a new string */
2785 result
= (PyStringObject
*) PyString_FromStringAndSize(NULL
, self_len
);
2788 result_s
= PyString_AS_STRING(result
);
2789 Py_MEMCPY(result_s
, self_s
, self_len
);
2791 /* change everything in-place, starting with this one */
2792 start
= result_s
+ (next
-self_s
);
2795 end
= result_s
+ self_len
;
2797 while (--maxcount
> 0) {
2798 next
= findchar(start
, end
-start
, from_c
);
2808 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
2809 Py_LOCAL(PyStringObject
*)
2810 replace_substring_in_place(PyStringObject
*self
,
2811 const char *from_s
, Py_ssize_t from_len
,
2812 const char *to_s
, Py_ssize_t to_len
,
2813 Py_ssize_t maxcount
)
2815 char *result_s
, *start
, *end
;
2817 Py_ssize_t self_len
, offset
;
2818 PyStringObject
*result
;
2820 /* The result string will be the same size */
2822 self_s
= PyString_AS_STRING(self
);
2823 self_len
= PyString_GET_SIZE(self
);
2825 offset
= findstring(self_s
, self_len
,
2827 0, self_len
, FORWARD
);
2829 /* No matches; return the original string */
2830 return return_self(self
);
2833 /* Need to make a new string */
2834 result
= (PyStringObject
*) PyString_FromStringAndSize(NULL
, self_len
);
2837 result_s
= PyString_AS_STRING(result
);
2838 Py_MEMCPY(result_s
, self_s
, self_len
);
2840 /* change everything in-place, starting with this one */
2841 start
= result_s
+ offset
;
2842 Py_MEMCPY(start
, to_s
, from_len
);
2844 end
= result_s
+ self_len
;
2846 while ( --maxcount
> 0) {
2847 offset
= findstring(start
, end
-start
,
2849 0, end
-start
, FORWARD
);
2852 Py_MEMCPY(start
+offset
, to_s
, from_len
);
2853 start
+= offset
+from_len
;
2859 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
2860 Py_LOCAL(PyStringObject
*)
2861 replace_single_character(PyStringObject
*self
,
2863 const char *to_s
, Py_ssize_t to_len
,
2864 Py_ssize_t maxcount
)
2866 char *self_s
, *result_s
;
2867 char *start
, *next
, *end
;
2868 Py_ssize_t self_len
, result_len
;
2869 Py_ssize_t count
, product
;
2870 PyStringObject
*result
;
2872 self_s
= PyString_AS_STRING(self
);
2873 self_len
= PyString_GET_SIZE(self
);
2875 count
= countchar(self_s
, self_len
, from_c
, maxcount
);
2877 /* no matches, return unchanged */
2878 return return_self(self
);
2881 /* use the difference between current and new, hence the "-1" */
2882 /* result_len = self_len + count * (to_len-1) */
2883 product
= count
* (to_len
-1);
2884 if (product
/ (to_len
-1) != count
) {
2885 PyErr_SetString(PyExc_OverflowError
, "replace string is too long");
2888 result_len
= self_len
+ product
;
2889 if (result_len
< 0) {
2890 PyErr_SetString(PyExc_OverflowError
, "replace string is too long");
2894 if ( (result
= (PyStringObject
*)
2895 PyString_FromStringAndSize(NULL
, result_len
)) == NULL
)
2897 result_s
= PyString_AS_STRING(result
);
2900 end
= self_s
+ self_len
;
2901 while (count
-- > 0) {
2902 next
= findchar(start
, end
-start
, from_c
);
2906 if (next
== start
) {
2907 /* replace with the 'to' */
2908 Py_MEMCPY(result_s
, to_s
, to_len
);
2912 /* copy the unchanged old then the 'to' */
2913 Py_MEMCPY(result_s
, start
, next
-start
);
2914 result_s
+= (next
-start
);
2915 Py_MEMCPY(result_s
, to_s
, to_len
);
2920 /* Copy the remainder of the remaining string */
2921 Py_MEMCPY(result_s
, start
, end
-start
);
2926 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
2927 Py_LOCAL(PyStringObject
*)
2928 replace_substring(PyStringObject
*self
,
2929 const char *from_s
, Py_ssize_t from_len
,
2930 const char *to_s
, Py_ssize_t to_len
,
2931 Py_ssize_t maxcount
) {
2932 char *self_s
, *result_s
;
2933 char *start
, *next
, *end
;
2934 Py_ssize_t self_len
, result_len
;
2935 Py_ssize_t count
, offset
, product
;
2936 PyStringObject
*result
;
2938 self_s
= PyString_AS_STRING(self
);
2939 self_len
= PyString_GET_SIZE(self
);
2941 count
= countstring(self_s
, self_len
,
2943 0, self_len
, FORWARD
, maxcount
);
2945 /* no matches, return unchanged */
2946 return return_self(self
);
2949 /* Check for overflow */
2950 /* result_len = self_len + count * (to_len-from_len) */
2951 product
= count
* (to_len
-from_len
);
2952 if (product
/ (to_len
-from_len
) != count
) {
2953 PyErr_SetString(PyExc_OverflowError
, "replace string is too long");
2956 result_len
= self_len
+ product
;
2957 if (result_len
< 0) {
2958 PyErr_SetString(PyExc_OverflowError
, "replace string is too long");
2962 if ( (result
= (PyStringObject
*)
2963 PyString_FromStringAndSize(NULL
, result_len
)) == NULL
)
2965 result_s
= PyString_AS_STRING(result
);
2968 end
= self_s
+ self_len
;
2969 while (count
-- > 0) {
2970 offset
= findstring(start
, end
-start
,
2972 0, end
-start
, FORWARD
);
2975 next
= start
+offset
;
2976 if (next
== start
) {
2977 /* replace with the 'to' */
2978 Py_MEMCPY(result_s
, to_s
, to_len
);
2982 /* copy the unchanged old then the 'to' */
2983 Py_MEMCPY(result_s
, start
, next
-start
);
2984 result_s
+= (next
-start
);
2985 Py_MEMCPY(result_s
, to_s
, to_len
);
2987 start
= next
+from_len
;
2990 /* Copy the remainder of the remaining string */
2991 Py_MEMCPY(result_s
, start
, end
-start
);
2997 Py_LOCAL(PyStringObject
*)
2998 replace(PyStringObject
*self
,
2999 const char *from_s
, Py_ssize_t from_len
,
3000 const char *to_s
, Py_ssize_t to_len
,
3001 Py_ssize_t maxcount
)
3004 maxcount
= PY_SSIZE_T_MAX
;
3005 } else if (maxcount
== 0 || PyString_GET_SIZE(self
) == 0) {
3006 /* nothing to do; return the original string */
3007 return return_self(self
);
3010 if (maxcount
== 0 ||
3011 (from_len
== 0 && to_len
== 0)) {
3012 /* nothing to do; return the original string */
3013 return return_self(self
);
3016 /* Handle zero-length special cases */
3018 if (from_len
== 0) {
3019 /* insert the 'to' string everywhere. */
3020 /* >>> "Python".replace("", ".") */
3021 /* '.P.y.t.h.o.n.' */
3022 return replace_interleave(self
, to_s
, to_len
, maxcount
);
3025 /* Except for "".replace("", "A") == "A" there is no way beyond this */
3026 /* point for an empty self string to generate a non-empty string */
3027 /* Special case so the remaining code always gets a non-empty string */
3028 if (PyString_GET_SIZE(self
) == 0) {
3029 return return_self(self
);
3033 /* delete all occurances of 'from' string */
3034 if (from_len
== 1) {
3035 return replace_delete_single_character(
3036 self
, from_s
[0], maxcount
);
3038 return replace_delete_substring(self
, from_s
, from_len
, maxcount
);
3042 /* Handle special case where both strings have the same length */
3044 if (from_len
== to_len
) {
3045 if (from_len
== 1) {
3046 return replace_single_character_in_place(
3052 return replace_substring_in_place(
3053 self
, from_s
, from_len
, to_s
, to_len
, maxcount
);
3057 /* Otherwise use the more generic algorithms */
3058 if (from_len
== 1) {
3059 return replace_single_character(self
, from_s
[0],
3060 to_s
, to_len
, maxcount
);
3062 /* len('from')>=2, len('to')>=1 */
3063 return replace_substring(self
, from_s
, from_len
, to_s
, to_len
, maxcount
);
3067 PyDoc_STRVAR(replace__doc__
,
3068 "S.replace (old, new[, count]) -> string\n\
3070 Return a copy of string S with all occurrences of substring\n\
3071 old replaced by new. If the optional argument count is\n\
3072 given, only the first count occurrences are replaced.");
3075 string_replace(PyStringObject
*self
, PyObject
*args
)
3077 Py_ssize_t count
= -1;
3078 PyObject
*from
, *to
;
3079 const char *from_s
, *to_s
;
3080 Py_ssize_t from_len
, to_len
;
3082 if (!PyArg_ParseTuple(args
, "OO|n:replace", &from
, &to
, &count
))
3085 if (PyString_Check(from
)) {
3086 from_s
= PyString_AS_STRING(from
);
3087 from_len
= PyString_GET_SIZE(from
);
3089 #ifdef Py_USING_UNICODE
3090 if (PyUnicode_Check(from
))
3091 return PyUnicode_Replace((PyObject
*)self
,
3094 else if (PyObject_AsCharBuffer(from
, &from_s
, &from_len
))
3097 if (PyString_Check(to
)) {
3098 to_s
= PyString_AS_STRING(to
);
3099 to_len
= PyString_GET_SIZE(to
);
3101 #ifdef Py_USING_UNICODE
3102 else if (PyUnicode_Check(to
))
3103 return PyUnicode_Replace((PyObject
*)self
,
3106 else if (PyObject_AsCharBuffer(to
, &to_s
, &to_len
))
3109 return (PyObject
*)replace((PyStringObject
*) self
,
3111 to_s
, to_len
, count
);
3116 /* Matches the end (direction >= 0) or start (direction < 0) of self
3117 * against substr, using the start and end arguments. Returns
3118 * -1 on error, 0 if not found and 1 if found.
3121 _string_tailmatch(PyStringObject
*self
, PyObject
*substr
, Py_ssize_t start
,
3122 Py_ssize_t end
, int direction
)
3124 Py_ssize_t len
= PyString_GET_SIZE(self
);
3129 if (PyString_Check(substr
)) {
3130 sub
= PyString_AS_STRING(substr
);
3131 slen
= PyString_GET_SIZE(substr
);
3133 #ifdef Py_USING_UNICODE
3134 else if (PyUnicode_Check(substr
))
3135 return PyUnicode_Tailmatch((PyObject
*)self
,
3136 substr
, start
, end
, direction
);
3138 else if (PyObject_AsCharBuffer(substr
, &sub
, &slen
))
3140 str
= PyString_AS_STRING(self
);
3142 string_adjust_indices(&start
, &end
, len
);
3144 if (direction
< 0) {
3146 if (start
+slen
> len
)
3150 if (end
-start
< slen
|| start
> len
)
3153 if (end
-slen
> start
)
3156 if (end
-start
>= slen
)
3157 return ! memcmp(str
+start
, sub
, slen
);
3162 PyDoc_STRVAR(startswith__doc__
,
3163 "S.startswith(prefix[, start[, end]]) -> bool\n\
3165 Return True if S starts with the specified prefix, False otherwise.\n\
3166 With optional start, test S beginning at that position.\n\
3167 With optional end, stop comparing S at that position.\n\
3168 prefix can also be a tuple of strings to try.");
3171 string_startswith(PyStringObject
*self
, PyObject
*args
)
3173 Py_ssize_t start
= 0;
3174 Py_ssize_t end
= PY_SSIZE_T_MAX
;
3178 if (!PyArg_ParseTuple(args
, "O|O&O&:startswith", &subobj
,
3179 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
3181 if (PyTuple_Check(subobj
)) {
3183 for (i
= 0; i
< PyTuple_GET_SIZE(subobj
); i
++) {
3184 result
= _string_tailmatch(self
,
3185 PyTuple_GET_ITEM(subobj
, i
),
3195 result
= _string_tailmatch(self
, subobj
, start
, end
, -1);
3199 return PyBool_FromLong(result
);
3203 PyDoc_STRVAR(endswith__doc__
,
3204 "S.endswith(suffix[, start[, end]]) -> bool\n\
3206 Return True if S ends with the specified suffix, False otherwise.\n\
3207 With optional start, test S beginning at that position.\n\
3208 With optional end, stop comparing S at that position.\n\
3209 suffix can also be a tuple of strings to try.");
3212 string_endswith(PyStringObject
*self
, PyObject
*args
)
3214 Py_ssize_t start
= 0;
3215 Py_ssize_t end
= PY_SSIZE_T_MAX
;
3219 if (!PyArg_ParseTuple(args
, "O|O&O&:endswith", &subobj
,
3220 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
3222 if (PyTuple_Check(subobj
)) {
3224 for (i
= 0; i
< PyTuple_GET_SIZE(subobj
); i
++) {
3225 result
= _string_tailmatch(self
,
3226 PyTuple_GET_ITEM(subobj
, i
),
3236 result
= _string_tailmatch(self
, subobj
, start
, end
, +1);
3240 return PyBool_FromLong(result
);
3244 PyDoc_STRVAR(encode__doc__
,
3245 "S.encode([encoding[,errors]]) -> object\n\
3247 Encodes S using the codec registered for encoding. encoding defaults\n\
3248 to the default encoding. errors may be given to set a different error\n\
3249 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
3250 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
3251 'xmlcharrefreplace' as well as any other name registered with\n\
3252 codecs.register_error that is able to handle UnicodeEncodeErrors.");
3255 string_encode(PyStringObject
*self
, PyObject
*args
)
3257 char *encoding
= NULL
;
3258 char *errors
= NULL
;
3261 if (!PyArg_ParseTuple(args
, "|ss:encode", &encoding
, &errors
))
3263 v
= PyString_AsEncodedObject((PyObject
*)self
, encoding
, errors
);
3266 if (!PyString_Check(v
) && !PyUnicode_Check(v
)) {
3267 PyErr_Format(PyExc_TypeError
,
3268 "encoder did not return a string/unicode object "
3270 Py_Type(v
)->tp_name
);
3281 PyDoc_STRVAR(decode__doc__
,
3282 "S.decode([encoding[,errors]]) -> object\n\
3284 Decodes S using the codec registered for encoding. encoding defaults\n\
3285 to the default encoding. errors may be given to set a different error\n\
3286 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
3287 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
3288 as well as any other name registerd with codecs.register_error that is\n\
3289 able to handle UnicodeDecodeErrors.");
3292 string_decode(PyStringObject
*self
, PyObject
*args
)
3294 char *encoding
= NULL
;
3295 char *errors
= NULL
;
3298 if (!PyArg_ParseTuple(args
, "|ss:decode", &encoding
, &errors
))
3300 v
= PyString_AsDecodedObject((PyObject
*)self
, encoding
, errors
);
3303 if (!PyString_Check(v
) && !PyUnicode_Check(v
)) {
3304 PyErr_Format(PyExc_TypeError
,
3305 "decoder did not return a string/unicode object "
3307 Py_Type(v
)->tp_name
);
3318 PyDoc_STRVAR(expandtabs__doc__
,
3319 "S.expandtabs([tabsize]) -> string\n\
3321 Return a copy of S where all tab characters are expanded using spaces.\n\
3322 If tabsize is not given, a tab size of 8 characters is assumed.");
3325 string_expandtabs(PyStringObject
*self
, PyObject
*args
)
3329 Py_ssize_t i
, j
, old_j
;
3333 if (!PyArg_ParseTuple(args
, "|i:expandtabs", &tabsize
))
3336 /* First pass: determine size of output string */
3338 e
= PyString_AS_STRING(self
) + PyString_GET_SIZE(self
);
3339 for (p
= PyString_AS_STRING(self
); p
< e
; p
++)
3342 j
+= tabsize
- (j
% tabsize
);
3344 PyErr_SetString(PyExc_OverflowError
,
3345 "new string is too long");
3353 if (*p
== '\n' || *p
== '\r') {
3357 PyErr_SetString(PyExc_OverflowError
,
3358 "new string is too long");
3365 PyErr_SetString(PyExc_OverflowError
, "new string is too long");
3369 /* Second pass: create output string and fill it */
3370 u
= PyString_FromStringAndSize(NULL
, i
+ j
);
3375 q
= PyString_AS_STRING(u
);
3377 for (p
= PyString_AS_STRING(self
); p
< e
; p
++)
3380 i
= tabsize
- (j
% tabsize
);
3389 if (*p
== '\n' || *p
== '\r')
3396 Py_LOCAL_INLINE(PyObject
*)
3397 pad(PyStringObject
*self
, Py_ssize_t left
, Py_ssize_t right
, char fill
)
3406 if (left
== 0 && right
== 0 && PyString_CheckExact(self
)) {
3408 return (PyObject
*)self
;
3411 u
= PyString_FromStringAndSize(NULL
,
3412 left
+ PyString_GET_SIZE(self
) + right
);
3415 memset(PyString_AS_STRING(u
), fill
, left
);
3416 Py_MEMCPY(PyString_AS_STRING(u
) + left
,
3417 PyString_AS_STRING(self
),
3418 PyString_GET_SIZE(self
));
3420 memset(PyString_AS_STRING(u
) + left
+ PyString_GET_SIZE(self
),
3427 PyDoc_STRVAR(ljust__doc__
,
3428 "S.ljust(width[, fillchar]) -> string\n"
3430 "Return S left justified in a string of length width. Padding is\n"
3431 "done using the specified fill character (default is a space).");
3434 string_ljust(PyStringObject
*self
, PyObject
*args
)
3437 char fillchar
= ' ';
3439 if (!PyArg_ParseTuple(args
, "n|c:ljust", &width
, &fillchar
))
3442 if (PyString_GET_SIZE(self
) >= width
&& PyString_CheckExact(self
)) {
3444 return (PyObject
*) self
;
3447 return pad(self
, 0, width
- PyString_GET_SIZE(self
), fillchar
);
3451 PyDoc_STRVAR(rjust__doc__
,
3452 "S.rjust(width[, fillchar]) -> string\n"
3454 "Return S right justified in a string of length width. Padding is\n"
3455 "done using the specified fill character (default is a space)");
3458 string_rjust(PyStringObject
*self
, PyObject
*args
)
3461 char fillchar
= ' ';
3463 if (!PyArg_ParseTuple(args
, "n|c:rjust", &width
, &fillchar
))
3466 if (PyString_GET_SIZE(self
) >= width
&& PyString_CheckExact(self
)) {
3468 return (PyObject
*) self
;
3471 return pad(self
, width
- PyString_GET_SIZE(self
), 0, fillchar
);
3475 PyDoc_STRVAR(center__doc__
,
3476 "S.center(width[, fillchar]) -> string\n"
3478 "Return S centered in a string of length width. Padding is\n"
3479 "done using the specified fill character (default is a space)");
3482 string_center(PyStringObject
*self
, PyObject
*args
)
3484 Py_ssize_t marg
, left
;
3486 char fillchar
= ' ';
3488 if (!PyArg_ParseTuple(args
, "n|c:center", &width
, &fillchar
))
3491 if (PyString_GET_SIZE(self
) >= width
&& PyString_CheckExact(self
)) {
3493 return (PyObject
*) self
;
3496 marg
= width
- PyString_GET_SIZE(self
);
3497 left
= marg
/ 2 + (marg
& width
& 1);
3499 return pad(self
, left
, marg
- left
, fillchar
);
3502 PyDoc_STRVAR(zfill__doc__
,
3503 "S.zfill(width) -> string\n"
3505 "Pad a numeric string S with zeros on the left, to fill a field\n"
3506 "of the specified width. The string S is never truncated.");
3509 string_zfill(PyStringObject
*self
, PyObject
*args
)
3516 if (!PyArg_ParseTuple(args
, "n:zfill", &width
))
3519 if (PyString_GET_SIZE(self
) >= width
) {
3520 if (PyString_CheckExact(self
)) {
3522 return (PyObject
*) self
;
3525 return PyString_FromStringAndSize(
3526 PyString_AS_STRING(self
),
3527 PyString_GET_SIZE(self
)
3531 fill
= width
- PyString_GET_SIZE(self
);
3533 s
= pad(self
, fill
, 0, '0');
3538 p
= PyString_AS_STRING(s
);
3539 if (p
[fill
] == '+' || p
[fill
] == '-') {
3540 /* move sign to beginning of string */
3545 return (PyObject
*) s
;
3548 PyDoc_STRVAR(isspace__doc__
,
3549 "S.isspace() -> bool\n\
3551 Return True if all characters in S are whitespace\n\
3552 and there is at least one character in S, False otherwise.");
3555 string_isspace(PyStringObject
*self
)
3557 register const unsigned char *p
3558 = (unsigned char *) PyString_AS_STRING(self
);
3559 register const unsigned char *e
;
3561 /* Shortcut for single character strings */
3562 if (PyString_GET_SIZE(self
) == 1 &&
3564 return PyBool_FromLong(1);
3566 /* Special case for empty strings */
3567 if (PyString_GET_SIZE(self
) == 0)
3568 return PyBool_FromLong(0);
3570 e
= p
+ PyString_GET_SIZE(self
);
3571 for (; p
< e
; p
++) {
3573 return PyBool_FromLong(0);
3575 return PyBool_FromLong(1);
3579 PyDoc_STRVAR(isalpha__doc__
,
3580 "S.isalpha() -> bool\n\
3582 Return True if all characters in S are alphabetic\n\
3583 and there is at least one character in S, False otherwise.");
3586 string_isalpha(PyStringObject
*self
)
3588 register const unsigned char *p
3589 = (unsigned char *) PyString_AS_STRING(self
);
3590 register const unsigned char *e
;
3592 /* Shortcut for single character strings */
3593 if (PyString_GET_SIZE(self
) == 1 &&
3595 return PyBool_FromLong(1);
3597 /* Special case for empty strings */
3598 if (PyString_GET_SIZE(self
) == 0)
3599 return PyBool_FromLong(0);
3601 e
= p
+ PyString_GET_SIZE(self
);
3602 for (; p
< e
; p
++) {
3604 return PyBool_FromLong(0);
3606 return PyBool_FromLong(1);
3610 PyDoc_STRVAR(isalnum__doc__
,
3611 "S.isalnum() -> bool\n\
3613 Return True if all characters in S are alphanumeric\n\
3614 and there is at least one character in S, False otherwise.");
3617 string_isalnum(PyStringObject
*self
)
3619 register const unsigned char *p
3620 = (unsigned char *) PyString_AS_STRING(self
);
3621 register const unsigned char *e
;
3623 /* Shortcut for single character strings */
3624 if (PyString_GET_SIZE(self
) == 1 &&
3626 return PyBool_FromLong(1);
3628 /* Special case for empty strings */
3629 if (PyString_GET_SIZE(self
) == 0)
3630 return PyBool_FromLong(0);
3632 e
= p
+ PyString_GET_SIZE(self
);
3633 for (; p
< e
; p
++) {
3635 return PyBool_FromLong(0);
3637 return PyBool_FromLong(1);
3641 PyDoc_STRVAR(isdigit__doc__
,
3642 "S.isdigit() -> bool\n\
3644 Return True if all characters in S are digits\n\
3645 and there is at least one character in S, False otherwise.");
3648 string_isdigit(PyStringObject
*self
)
3650 register const unsigned char *p
3651 = (unsigned char *) PyString_AS_STRING(self
);
3652 register const unsigned char *e
;
3654 /* Shortcut for single character strings */
3655 if (PyString_GET_SIZE(self
) == 1 &&
3657 return PyBool_FromLong(1);
3659 /* Special case for empty strings */
3660 if (PyString_GET_SIZE(self
) == 0)
3661 return PyBool_FromLong(0);
3663 e
= p
+ PyString_GET_SIZE(self
);
3664 for (; p
< e
; p
++) {
3666 return PyBool_FromLong(0);
3668 return PyBool_FromLong(1);
3672 PyDoc_STRVAR(islower__doc__
,
3673 "S.islower() -> bool\n\
3675 Return True if all cased characters in S are lowercase and there is\n\
3676 at least one cased character in S, False otherwise.");
3679 string_islower(PyStringObject
*self
)
3681 register const unsigned char *p
3682 = (unsigned char *) PyString_AS_STRING(self
);
3683 register const unsigned char *e
;
3686 /* Shortcut for single character strings */
3687 if (PyString_GET_SIZE(self
) == 1)
3688 return PyBool_FromLong(islower(*p
) != 0);
3690 /* Special case for empty strings */
3691 if (PyString_GET_SIZE(self
) == 0)
3692 return PyBool_FromLong(0);
3694 e
= p
+ PyString_GET_SIZE(self
);
3696 for (; p
< e
; p
++) {
3698 return PyBool_FromLong(0);
3699 else if (!cased
&& islower(*p
))
3702 return PyBool_FromLong(cased
);
3706 PyDoc_STRVAR(isupper__doc__
,
3707 "S.isupper() -> bool\n\
3709 Return True if all cased characters in S are uppercase and there is\n\
3710 at least one cased character in S, False otherwise.");
3713 string_isupper(PyStringObject
*self
)
3715 register const unsigned char *p
3716 = (unsigned char *) PyString_AS_STRING(self
);
3717 register const unsigned char *e
;
3720 /* Shortcut for single character strings */
3721 if (PyString_GET_SIZE(self
) == 1)
3722 return PyBool_FromLong(isupper(*p
) != 0);
3724 /* Special case for empty strings */
3725 if (PyString_GET_SIZE(self
) == 0)
3726 return PyBool_FromLong(0);
3728 e
= p
+ PyString_GET_SIZE(self
);
3730 for (; p
< e
; p
++) {
3732 return PyBool_FromLong(0);
3733 else if (!cased
&& isupper(*p
))
3736 return PyBool_FromLong(cased
);
3740 PyDoc_STRVAR(istitle__doc__
,
3741 "S.istitle() -> bool\n\
3743 Return True if S is a titlecased string and there is at least one\n\
3744 character in S, i.e. uppercase characters may only follow uncased\n\
3745 characters and lowercase characters only cased ones. Return False\n\
3749 string_istitle(PyStringObject
*self
, PyObject
*uncased
)
3751 register const unsigned char *p
3752 = (unsigned char *) PyString_AS_STRING(self
);
3753 register const unsigned char *e
;
3754 int cased
, previous_is_cased
;
3756 /* Shortcut for single character strings */
3757 if (PyString_GET_SIZE(self
) == 1)
3758 return PyBool_FromLong(isupper(*p
) != 0);
3760 /* Special case for empty strings */
3761 if (PyString_GET_SIZE(self
) == 0)
3762 return PyBool_FromLong(0);
3764 e
= p
+ PyString_GET_SIZE(self
);
3766 previous_is_cased
= 0;
3767 for (; p
< e
; p
++) {
3768 register const unsigned char ch
= *p
;
3771 if (previous_is_cased
)
3772 return PyBool_FromLong(0);
3773 previous_is_cased
= 1;
3776 else if (islower(ch
)) {
3777 if (!previous_is_cased
)
3778 return PyBool_FromLong(0);
3779 previous_is_cased
= 1;
3783 previous_is_cased
= 0;
3785 return PyBool_FromLong(cased
);
3789 PyDoc_STRVAR(splitlines__doc__
,
3790 "S.splitlines([keepends]) -> list of strings\n\
3792 Return a list of the lines in S, breaking at line boundaries.\n\
3793 Line breaks are not included in the resulting list unless keepends\n\
3794 is given and true.");
3797 string_splitlines(PyStringObject
*self
, PyObject
*args
)
3799 register Py_ssize_t i
;
3800 register Py_ssize_t j
;
3807 if (!PyArg_ParseTuple(args
, "|i:splitlines", &keepends
))
3810 data
= PyString_AS_STRING(self
);
3811 len
= PyString_GET_SIZE(self
);
3813 /* This does not use the preallocated list because splitlines is
3814 usually run with hundreds of newlines. The overhead of
3815 switching between PyList_SET_ITEM and append causes about a
3816 2-3% slowdown for that common case. A smarter implementation
3817 could move the if check out, so the SET_ITEMs are done first
3818 and the appends only done when the prealloc buffer is full.
3819 That's too much work for little gain.*/
3821 list
= PyList_New(0);
3825 for (i
= j
= 0; i
< len
; ) {
3828 /* Find a line and append it */
3829 while (i
< len
&& data
[i
] != '\n' && data
[i
] != '\r')
3832 /* Skip the line break reading CRLF as one line break */
3835 if (data
[i
] == '\r' && i
+ 1 < len
&&
3843 SPLIT_APPEND(data
, j
, eol
);
3847 SPLIT_APPEND(data
, j
, len
);
3860 #undef PREALLOC_SIZE
3863 string_getnewargs(PyStringObject
*v
)
3865 return Py_BuildValue("(s#)", v
->ob_sval
, Py_Size(v
));
3870 string_methods
[] = {
3871 /* Counterparts of the obsolete stropmodule functions; except
3872 string.maketrans(). */
3873 {"join", (PyCFunction
)string_join
, METH_O
, join__doc__
},
3874 {"split", (PyCFunction
)string_split
, METH_VARARGS
, split__doc__
},
3875 {"rsplit", (PyCFunction
)string_rsplit
, METH_VARARGS
, rsplit__doc__
},
3876 {"lower", (PyCFunction
)string_lower
, METH_NOARGS
, lower__doc__
},
3877 {"upper", (PyCFunction
)string_upper
, METH_NOARGS
, upper__doc__
},
3878 {"islower", (PyCFunction
)string_islower
, METH_NOARGS
, islower__doc__
},
3879 {"isupper", (PyCFunction
)string_isupper
, METH_NOARGS
, isupper__doc__
},
3880 {"isspace", (PyCFunction
)string_isspace
, METH_NOARGS
, isspace__doc__
},
3881 {"isdigit", (PyCFunction
)string_isdigit
, METH_NOARGS
, isdigit__doc__
},
3882 {"istitle", (PyCFunction
)string_istitle
, METH_NOARGS
, istitle__doc__
},
3883 {"isalpha", (PyCFunction
)string_isalpha
, METH_NOARGS
, isalpha__doc__
},
3884 {"isalnum", (PyCFunction
)string_isalnum
, METH_NOARGS
, isalnum__doc__
},
3885 {"capitalize", (PyCFunction
)string_capitalize
, METH_NOARGS
,
3887 {"count", (PyCFunction
)string_count
, METH_VARARGS
, count__doc__
},
3888 {"endswith", (PyCFunction
)string_endswith
, METH_VARARGS
,
3890 {"partition", (PyCFunction
)string_partition
, METH_O
, partition__doc__
},
3891 {"find", (PyCFunction
)string_find
, METH_VARARGS
, find__doc__
},
3892 {"index", (PyCFunction
)string_index
, METH_VARARGS
, index__doc__
},
3893 {"lstrip", (PyCFunction
)string_lstrip
, METH_VARARGS
, lstrip__doc__
},
3894 {"replace", (PyCFunction
)string_replace
, METH_VARARGS
, replace__doc__
},
3895 {"rfind", (PyCFunction
)string_rfind
, METH_VARARGS
, rfind__doc__
},
3896 {"rindex", (PyCFunction
)string_rindex
, METH_VARARGS
, rindex__doc__
},
3897 {"rstrip", (PyCFunction
)string_rstrip
, METH_VARARGS
, rstrip__doc__
},
3898 {"rpartition", (PyCFunction
)string_rpartition
, METH_O
,
3900 {"startswith", (PyCFunction
)string_startswith
, METH_VARARGS
,
3902 {"strip", (PyCFunction
)string_strip
, METH_VARARGS
, strip__doc__
},
3903 {"swapcase", (PyCFunction
)string_swapcase
, METH_NOARGS
,
3905 {"translate", (PyCFunction
)string_translate
, METH_VARARGS
,
3907 {"title", (PyCFunction
)string_title
, METH_NOARGS
, title__doc__
},
3908 {"ljust", (PyCFunction
)string_ljust
, METH_VARARGS
, ljust__doc__
},
3909 {"rjust", (PyCFunction
)string_rjust
, METH_VARARGS
, rjust__doc__
},
3910 {"center", (PyCFunction
)string_center
, METH_VARARGS
, center__doc__
},
3911 {"zfill", (PyCFunction
)string_zfill
, METH_VARARGS
, zfill__doc__
},
3912 {"encode", (PyCFunction
)string_encode
, METH_VARARGS
, encode__doc__
},
3913 {"decode", (PyCFunction
)string_decode
, METH_VARARGS
, decode__doc__
},
3914 {"expandtabs", (PyCFunction
)string_expandtabs
, METH_VARARGS
,
3916 {"splitlines", (PyCFunction
)string_splitlines
, METH_VARARGS
,
3918 {"__getnewargs__", (PyCFunction
)string_getnewargs
, METH_NOARGS
},
3919 {NULL
, NULL
} /* sentinel */
3923 str_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
);
3926 string_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
3929 static char *kwlist
[] = {"object", 0};
3931 if (type
!= &PyString_Type
)
3932 return str_subtype_new(type
, args
, kwds
);
3933 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|O:str", kwlist
, &x
))
3936 return PyString_FromString("");
3937 return PyObject_Str(x
);
3941 str_subtype_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
3943 PyObject
*tmp
, *pnew
;
3946 assert(PyType_IsSubtype(type
, &PyString_Type
));
3947 tmp
= string_new(&PyString_Type
, args
, kwds
);
3950 assert(PyString_CheckExact(tmp
));
3951 n
= PyString_GET_SIZE(tmp
);
3952 pnew
= type
->tp_alloc(type
, n
);
3954 Py_MEMCPY(PyString_AS_STRING(pnew
), PyString_AS_STRING(tmp
), n
+1);
3955 ((PyStringObject
*)pnew
)->ob_shash
=
3956 ((PyStringObject
*)tmp
)->ob_shash
;
3957 ((PyStringObject
*)pnew
)->ob_sstate
= SSTATE_NOT_INTERNED
;
3964 basestring_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
3966 PyErr_SetString(PyExc_TypeError
,
3967 "The basestring type cannot be instantiated");
3972 string_mod(PyObject
*v
, PyObject
*w
)
3974 if (!PyString_Check(v
)) {
3975 Py_INCREF(Py_NotImplemented
);
3976 return Py_NotImplemented
;
3978 return PyString_Format(v
, w
);
3981 PyDoc_STRVAR(basestring_doc
,
3982 "Type basestring cannot be instantiated; it is the base for str and unicode.");
3984 static PyNumberMethods string_as_number
= {
3989 string_mod
, /*nb_remainder*/
3993 PyTypeObject PyBaseString_Type
= {
3994 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
4004 0, /* tp_as_number */
4005 0, /* tp_as_sequence */
4006 0, /* tp_as_mapping */
4010 0, /* tp_getattro */
4011 0, /* tp_setattro */
4012 0, /* tp_as_buffer */
4013 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
4014 basestring_doc
, /* tp_doc */
4015 0, /* tp_traverse */
4017 0, /* tp_richcompare */
4018 0, /* tp_weaklistoffset */
4020 0, /* tp_iternext */
4024 &PyBaseObject_Type
, /* tp_base */
4026 0, /* tp_descr_get */
4027 0, /* tp_descr_set */
4028 0, /* tp_dictoffset */
4031 basestring_new
, /* tp_new */
4035 PyDoc_STRVAR(string_doc
,
4036 "str(object) -> string\n\
4038 Return a nice string representation of the object.\n\
4039 If the argument is a string, the return value is the same object.");
4041 PyTypeObject PyString_Type
= {
4042 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
4044 sizeof(PyStringObject
),
4046 string_dealloc
, /* tp_dealloc */
4047 (printfunc
)string_print
, /* tp_print */
4051 string_repr
, /* tp_repr */
4052 &string_as_number
, /* tp_as_number */
4053 &string_as_sequence
, /* tp_as_sequence */
4054 &string_as_mapping
, /* tp_as_mapping */
4055 (hashfunc
)string_hash
, /* tp_hash */
4057 string_str
, /* tp_str */
4058 PyObject_GenericGetAttr
, /* tp_getattro */
4059 0, /* tp_setattro */
4060 &string_as_buffer
, /* tp_as_buffer */
4061 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
|
4062 Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_STRING_SUBCLASS
, /* tp_flags */
4063 string_doc
, /* tp_doc */
4064 0, /* tp_traverse */
4066 (richcmpfunc
)string_richcompare
, /* tp_richcompare */
4067 0, /* tp_weaklistoffset */
4069 0, /* tp_iternext */
4070 string_methods
, /* tp_methods */
4073 &PyBaseString_Type
, /* tp_base */
4075 0, /* tp_descr_get */
4076 0, /* tp_descr_set */
4077 0, /* tp_dictoffset */
4080 string_new
, /* tp_new */
4081 PyObject_Del
, /* tp_free */
4085 PyString_Concat(register PyObject
**pv
, register PyObject
*w
)
4087 register PyObject
*v
;
4090 if (w
== NULL
|| !PyString_Check(*pv
)) {
4095 v
= string_concat((PyStringObject
*) *pv
, w
);
4101 PyString_ConcatAndDel(register PyObject
**pv
, register PyObject
*w
)
4103 PyString_Concat(pv
, w
);
4108 /* The following function breaks the notion that strings are immutable:
4109 it changes the size of a string. We get away with this only if there
4110 is only one module referencing the object. You can also think of it
4111 as creating a new string object and destroying the old one, only
4112 more efficiently. In any case, don't use this if the string may
4113 already be known to some other part of the code...
4114 Note that if there's not enough memory to resize the string, the original
4115 string object at *pv is deallocated, *pv is set to NULL, an "out of
4116 memory" exception is set, and -1 is returned. Else (on success) 0 is
4117 returned, and the value in *pv may or may not be the same as on input.
4118 As always, an extra byte is allocated for a trailing \0 byte (newsize
4119 does *not* include that), and a trailing \0 byte is stored.
4123 _PyString_Resize(PyObject
**pv
, Py_ssize_t newsize
)
4125 register PyObject
*v
;
4126 register PyStringObject
*sv
;
4128 if (!PyString_Check(v
) || Py_Refcnt(v
) != 1 || newsize
< 0 ||
4129 PyString_CHECK_INTERNED(v
)) {
4132 PyErr_BadInternalCall();
4135 /* XXX UNREF/NEWREF interface should be more symmetrical */
4137 _Py_ForgetReference(v
);
4139 PyObject_REALLOC((char *)v
, sizeof(PyStringObject
) + newsize
);
4145 _Py_NewReference(*pv
);
4146 sv
= (PyStringObject
*) *pv
;
4147 Py_Size(sv
) = newsize
;
4148 sv
->ob_sval
[newsize
] = '\0';
4149 sv
->ob_shash
= -1; /* invalidate cached hash value */
4153 /* Helpers for formatstring */
4155 Py_LOCAL_INLINE(PyObject
*)
4156 getnextarg(PyObject
*args
, Py_ssize_t arglen
, Py_ssize_t
*p_argidx
)
4158 Py_ssize_t argidx
= *p_argidx
;
4159 if (argidx
< arglen
) {
4164 return PyTuple_GetItem(args
, argidx
);
4166 PyErr_SetString(PyExc_TypeError
,
4167 "not enough arguments for format string");
4178 #define F_LJUST (1<<0)
4179 #define F_SIGN (1<<1)
4180 #define F_BLANK (1<<2)
4181 #define F_ALT (1<<3)
4182 #define F_ZERO (1<<4)
4184 Py_LOCAL_INLINE(int)
4185 formatfloat(char *buf
, size_t buflen
, int flags
,
4186 int prec
, int type
, PyObject
*v
)
4188 /* fmt = '%#.' + `prec` + `type`
4189 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
4192 x
= PyFloat_AsDouble(v
);
4193 if (x
== -1.0 && PyErr_Occurred()) {
4194 PyErr_Format(PyExc_TypeError
, "float argument required, "
4195 "not %.200s", Py_Type(v
)->tp_name
);
4200 if (type
== 'f' && fabs(x
)/1e25
>= 1e25
)
4202 /* Worst case length calc to ensure no buffer overrun:
4206 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
4207 for any double rep.)
4208 len = 1 + prec + 1 + 2 + 5 = 9 + prec
4211 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
4212 len = 1 + 50 + 1 + prec = 52 + prec
4214 If prec=0 the effective precision is 1 (the leading digit is
4215 always given), therefore increase the length by one.
4218 if (((type
== 'g' || type
== 'G') &&
4219 buflen
<= (size_t)10 + (size_t)prec
) ||
4220 (type
== 'f' && buflen
<= (size_t)53 + (size_t)prec
)) {
4221 PyErr_SetString(PyExc_OverflowError
,
4222 "formatted float is too long (precision too large?)");
4225 PyOS_snprintf(fmt
, sizeof(fmt
), "%%%s.%d%c",
4226 (flags
&F_ALT
) ? "#" : "",
4228 PyOS_ascii_formatd(buf
, buflen
, fmt
, x
);
4229 return (int)strlen(buf
);
4232 /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
4233 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
4234 * Python's regular ints.
4235 * Return value: a new PyString*, or NULL if error.
4236 * . *pbuf is set to point into it,
4237 * *plen set to the # of chars following that.
4238 * Caller must decref it when done using pbuf.
4239 * The string starting at *pbuf is of the form
4240 * "-"? ("0x" | "0X")? digit+
4241 * "0x"/"0X" are present only for x and X conversions, with F_ALT
4242 * set in flags. The case of hex digits will be correct,
4243 * There will be at least prec digits, zero-filled on the left if
4244 * necessary to get that many.
4245 * val object to be converted
4246 * flags bitmask of format flags; only F_ALT is looked at
4247 * prec minimum number of digits; 0-fill on left if needed
4248 * type a character in [duoxX]; u acts the same as d
4250 * CAUTION: o, x and X conversions on regular ints can never
4251 * produce a '-' sign, but can for Python's unbounded ints.
4254 _PyString_FormatLong(PyObject
*val
, int flags
, int prec
, int type
,
4255 char **pbuf
, int *plen
)
4257 PyObject
*result
= NULL
;
4260 int sign
; /* 1 if '-', else 0 */
4261 int len
; /* number of characters */
4263 int numdigits
; /* len == numnondigits + numdigits */
4264 int numnondigits
= 0;
4269 result
= Py_Type(val
)->tp_str(val
);
4272 result
= Py_Type(val
)->tp_as_number
->nb_oct(val
);
4277 result
= Py_Type(val
)->tp_as_number
->nb_hex(val
);
4280 assert(!"'type' not in [duoxX]");
4285 buf
= PyString_AsString(result
);
4291 /* To modify the string in-place, there can only be one reference. */
4292 if (Py_Refcnt(result
) != 1) {
4293 PyErr_BadInternalCall();
4296 llen
= PyString_Size(result
);
4297 if (llen
> INT_MAX
) {
4298 PyErr_SetString(PyExc_ValueError
, "string too large in _PyString_FormatLong");
4302 if (buf
[len
-1] == 'L') {
4306 sign
= buf
[0] == '-';
4307 numnondigits
+= sign
;
4308 numdigits
= len
- numnondigits
;
4309 assert(numdigits
> 0);
4311 /* Get rid of base marker unless F_ALT */
4312 if ((flags
& F_ALT
) == 0) {
4313 /* Need to skip 0x, 0X or 0. */
4317 assert(buf
[sign
] == '0');
4318 /* If 0 is only digit, leave it alone. */
4319 if (numdigits
> 1) {
4326 assert(buf
[sign
] == '0');
4327 assert(buf
[sign
+ 1] == 'x');
4338 assert(len
== numnondigits
+ numdigits
);
4339 assert(numdigits
> 0);
4342 /* Fill with leading zeroes to meet minimum width. */
4343 if (prec
> numdigits
) {
4344 PyObject
*r1
= PyString_FromStringAndSize(NULL
,
4345 numnondigits
+ prec
);
4351 b1
= PyString_AS_STRING(r1
);
4352 for (i
= 0; i
< numnondigits
; ++i
)
4354 for (i
= 0; i
< prec
- numdigits
; i
++)
4356 for (i
= 0; i
< numdigits
; i
++)
4361 buf
= PyString_AS_STRING(result
);
4362 len
= numnondigits
+ prec
;
4365 /* Fix up case for hex conversions. */
4367 /* Need to convert all lower case letters to upper case.
4368 and need to convert 0x to 0X (and -0x to -0X). */
4369 for (i
= 0; i
< len
; i
++)
4370 if (buf
[i
] >= 'a' && buf
[i
] <= 'x')
4378 Py_LOCAL_INLINE(int)
4379 formatint(char *buf
, size_t buflen
, int flags
,
4380 int prec
, int type
, PyObject
*v
)
4382 /* fmt = '%#.' + `prec` + 'l' + `type`
4383 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
4385 char fmt
[64]; /* plenty big enough! */
4389 x
= PyInt_AsLong(v
);
4390 if (x
== -1 && PyErr_Occurred()) {
4391 PyErr_Format(PyExc_TypeError
, "int argument required, not %.200s",
4392 Py_Type(v
)->tp_name
);
4395 if (x
< 0 && type
== 'u') {
4398 if (x
< 0 && (type
== 'x' || type
== 'X' || type
== 'o'))
4405 if ((flags
& F_ALT
) &&
4406 (type
== 'x' || type
== 'X')) {
4407 /* When converting under %#x or %#X, there are a number
4408 * of issues that cause pain:
4409 * - when 0 is being converted, the C standard leaves off
4410 * the '0x' or '0X', which is inconsistent with other
4411 * %#x/%#X conversions and inconsistent with Python's
4413 * - there are platforms that violate the standard and
4414 * convert 0 with the '0x' or '0X'
4415 * (Metrowerks, Compaq Tru64)
4416 * - there are platforms that give '0x' when converting
4417 * under %#X, but convert 0 in accordance with the
4418 * standard (OS/2 EMX)
4420 * We can achieve the desired consistency by inserting our
4421 * own '0x' or '0X' prefix, and substituting %x/%X in place
4424 * Note that this is the same approach as used in
4425 * formatint() in unicodeobject.c
4427 PyOS_snprintf(fmt
, sizeof(fmt
), "%s0%c%%.%dl%c",
4428 sign
, type
, prec
, type
);
4431 PyOS_snprintf(fmt
, sizeof(fmt
), "%s%%%s.%dl%c",
4432 sign
, (flags
&F_ALT
) ? "#" : "",
4436 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
4437 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
4439 if (buflen
<= 14 || buflen
<= (size_t)3 + (size_t)prec
) {
4440 PyErr_SetString(PyExc_OverflowError
,
4441 "formatted integer is too long (precision too large?)");
4445 PyOS_snprintf(buf
, buflen
, fmt
, -x
);
4447 PyOS_snprintf(buf
, buflen
, fmt
, x
);
4448 return (int)strlen(buf
);
4451 Py_LOCAL_INLINE(int)
4452 formatchar(char *buf
, size_t buflen
, PyObject
*v
)
4454 /* presume that the buffer is at least 2 characters long */
4455 if (PyString_Check(v
)) {
4456 if (!PyArg_Parse(v
, "c;%c requires int or char", &buf
[0]))
4460 if (!PyArg_Parse(v
, "b;%c requires int or char", &buf
[0]))
4467 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
4469 FORMATBUFLEN is the length of the buffer in which the floats, ints, &
4470 chars are formatted. XXX This is a magic number. Each formatting
4471 routine does bounds checking to ensure no overflow, but a better
4472 solution may be to malloc a buffer of appropriate size for each
4473 format. For now, the current solution is sufficient.
4475 #define FORMATBUFLEN (size_t)120
4478 PyString_Format(PyObject
*format
, PyObject
*args
)
4481 Py_ssize_t arglen
, argidx
;
4482 Py_ssize_t reslen
, rescnt
, fmtcnt
;
4484 PyObject
*result
, *orig_args
;
4485 #ifdef Py_USING_UNICODE
4488 PyObject
*dict
= NULL
;
4489 if (format
== NULL
|| !PyString_Check(format
) || args
== NULL
) {
4490 PyErr_BadInternalCall();
4494 fmt
= PyString_AS_STRING(format
);
4495 fmtcnt
= PyString_GET_SIZE(format
);
4496 reslen
= rescnt
= fmtcnt
+ 100;
4497 result
= PyString_FromStringAndSize((char *)NULL
, reslen
);
4500 res
= PyString_AsString(result
);
4501 if (PyTuple_Check(args
)) {
4502 arglen
= PyTuple_GET_SIZE(args
);
4509 if (Py_Type(args
)->tp_as_mapping
&& !PyTuple_Check(args
) &&
4510 !PyObject_TypeCheck(args
, &PyBaseString_Type
))
4512 while (--fmtcnt
>= 0) {
4515 rescnt
= fmtcnt
+ 100;
4517 if (_PyString_Resize(&result
, reslen
) < 0)
4519 res
= PyString_AS_STRING(result
)
4526 /* Got a format specifier */
4528 Py_ssize_t width
= -1;
4533 PyObject
*temp
= NULL
;
4537 char formatbuf
[FORMATBUFLEN
];
4538 /* For format{float,int,char}() */
4539 #ifdef Py_USING_UNICODE
4540 char *fmt_start
= fmt
;
4541 Py_ssize_t argidx_start
= argidx
;
4552 PyErr_SetString(PyExc_TypeError
,
4553 "format requires a mapping");
4559 /* Skip over balanced parentheses */
4560 while (pcount
> 0 && --fmtcnt
>= 0) {
4563 else if (*fmt
== '(')
4567 keylen
= fmt
- keystart
- 1;
4568 if (fmtcnt
< 0 || pcount
> 0) {
4569 PyErr_SetString(PyExc_ValueError
,
4570 "incomplete format key");
4573 key
= PyString_FromStringAndSize(keystart
,
4581 args
= PyObject_GetItem(dict
, key
);
4590 while (--fmtcnt
>= 0) {
4591 switch (c
= *fmt
++) {
4592 case '-': flags
|= F_LJUST
; continue;
4593 case '+': flags
|= F_SIGN
; continue;
4594 case ' ': flags
|= F_BLANK
; continue;
4595 case '#': flags
|= F_ALT
; continue;
4596 case '0': flags
|= F_ZERO
; continue;
4601 v
= getnextarg(args
, arglen
, &argidx
);
4604 if (!PyInt_Check(v
)) {
4605 PyErr_SetString(PyExc_TypeError
,
4609 width
= PyInt_AsLong(v
);
4617 else if (c
>= 0 && isdigit(c
)) {
4619 while (--fmtcnt
>= 0) {
4620 c
= Py_CHARMASK(*fmt
++);
4623 if ((width
*10) / 10 != width
) {
4629 width
= width
*10 + (c
- '0');
4637 v
= getnextarg(args
, arglen
, &argidx
);
4640 if (!PyInt_Check(v
)) {
4646 prec
= PyInt_AsLong(v
);
4652 else if (c
>= 0 && isdigit(c
)) {
4654 while (--fmtcnt
>= 0) {
4655 c
= Py_CHARMASK(*fmt
++);
4658 if ((prec
*10) / 10 != prec
) {
4664 prec
= prec
*10 + (c
- '0');
4669 if (c
== 'h' || c
== 'l' || c
== 'L') {
4675 PyErr_SetString(PyExc_ValueError
,
4676 "incomplete format");
4680 v
= getnextarg(args
, arglen
, &argidx
);
4692 #ifdef Py_USING_UNICODE
4693 if (PyUnicode_Check(v
)) {
4695 argidx
= argidx_start
;
4699 temp
= _PyObject_Str(v
);
4700 #ifdef Py_USING_UNICODE
4701 if (temp
!= NULL
&& PyUnicode_Check(temp
)) {
4704 argidx
= argidx_start
;
4711 temp
= PyObject_Repr(v
);
4714 if (!PyString_Check(temp
)) {
4715 PyErr_SetString(PyExc_TypeError
,
4716 "%s argument has non-string str()");
4720 pbuf
= PyString_AS_STRING(temp
);
4721 len
= PyString_GET_SIZE(temp
);
4722 if (prec
>= 0 && len
> prec
)
4733 if (PyLong_Check(v
)) {
4735 temp
= _PyString_FormatLong(v
, flags
,
4736 prec
, c
, &pbuf
, &ilen
);
4744 len
= formatint(pbuf
,
4763 len
= formatfloat(pbuf
, sizeof(formatbuf
),
4772 #ifdef Py_USING_UNICODE
4773 if (PyUnicode_Check(v
)) {
4775 argidx
= argidx_start
;
4780 len
= formatchar(pbuf
, sizeof(formatbuf
), v
);
4785 PyErr_Format(PyExc_ValueError
,
4786 "unsupported format character '%c' (0x%x) "
4789 (Py_ssize_t
)(fmt
- 1 -
4790 PyString_AsString(format
)));
4794 if (*pbuf
== '-' || *pbuf
== '+') {
4798 else if (flags
& F_SIGN
)
4800 else if (flags
& F_BLANK
)
4807 if (rescnt
- (sign
!= 0) < width
) {
4809 rescnt
= width
+ fmtcnt
+ 100;
4814 return PyErr_NoMemory();
4816 if (_PyString_Resize(&result
, reslen
) < 0) {
4820 res
= PyString_AS_STRING(result
)
4830 if ((flags
& F_ALT
) && (c
== 'x' || c
== 'X')) {
4831 assert(pbuf
[0] == '0');
4832 assert(pbuf
[1] == c
);
4843 if (width
> len
&& !(flags
& F_LJUST
)) {
4847 } while (--width
> len
);
4852 if ((flags
& F_ALT
) &&
4853 (c
== 'x' || c
== 'X')) {
4854 assert(pbuf
[0] == '0');
4855 assert(pbuf
[1] == c
);
4860 Py_MEMCPY(res
, pbuf
, len
);
4863 while (--width
>= len
) {
4867 if (dict
&& (argidx
< arglen
) && c
!= '%') {
4868 PyErr_SetString(PyExc_TypeError
,
4869 "not all arguments converted during string formatting");
4876 if (argidx
< arglen
&& !dict
) {
4877 PyErr_SetString(PyExc_TypeError
,
4878 "not all arguments converted during string formatting");
4884 _PyString_Resize(&result
, reslen
- rescnt
);
4887 #ifdef Py_USING_UNICODE
4893 /* Fiddle args right (remove the first argidx arguments) */
4894 if (PyTuple_Check(orig_args
) && argidx
> 0) {
4896 Py_ssize_t n
= PyTuple_GET_SIZE(orig_args
) - argidx
;
4901 PyObject
*w
= PyTuple_GET_ITEM(orig_args
, n
+ argidx
);
4903 PyTuple_SET_ITEM(v
, n
, w
);
4907 Py_INCREF(orig_args
);
4911 /* Take what we have of the result and let the Unicode formatting
4912 function format the rest of the input. */
4913 rescnt
= res
- PyString_AS_STRING(result
);
4914 if (_PyString_Resize(&result
, rescnt
))
4916 fmtcnt
= PyString_GET_SIZE(format
) - \
4917 (fmt
- PyString_AS_STRING(format
));
4918 format
= PyUnicode_Decode(fmt
, fmtcnt
, NULL
, NULL
);
4921 v
= PyUnicode_Format(format
, args
);
4925 /* Paste what we have (result) to what the Unicode formatting
4926 function returned (v) and return the result (or error) */
4927 w
= PyUnicode_Concat(result
, v
);
4932 #endif /* Py_USING_UNICODE */
4943 PyString_InternInPlace(PyObject
**p
)
4945 register PyStringObject
*s
= (PyStringObject
*)(*p
);
4947 if (s
== NULL
|| !PyString_Check(s
))
4948 Py_FatalError("PyString_InternInPlace: strings only please!");
4949 /* If it's a string subclass, we don't really know what putting
4950 it in the interned dict might do. */
4951 if (!PyString_CheckExact(s
))
4953 if (PyString_CHECK_INTERNED(s
))
4955 if (interned
== NULL
) {
4956 interned
= PyDict_New();
4957 if (interned
== NULL
) {
4958 PyErr_Clear(); /* Don't leave an exception */
4962 t
= PyDict_GetItem(interned
, (PyObject
*)s
);
4970 if (PyDict_SetItem(interned
, (PyObject
*)s
, (PyObject
*)s
) < 0) {
4974 /* The two references in interned are not counted by refcnt.
4975 The string deallocator will take care of this */
4977 PyString_CHECK_INTERNED(s
) = SSTATE_INTERNED_MORTAL
;
4981 PyString_InternImmortal(PyObject
**p
)
4983 PyString_InternInPlace(p
);
4984 if (PyString_CHECK_INTERNED(*p
) != SSTATE_INTERNED_IMMORTAL
) {
4985 PyString_CHECK_INTERNED(*p
) = SSTATE_INTERNED_IMMORTAL
;
4992 PyString_InternFromString(const char *cp
)
4994 PyObject
*s
= PyString_FromString(cp
);
4997 PyString_InternInPlace(&s
);
5005 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++) {
5006 Py_XDECREF(characters
[i
]);
5007 characters
[i
] = NULL
;
5009 Py_XDECREF(nullstring
);
5013 void _Py_ReleaseInternedStrings(void)
5018 Py_ssize_t immortal_size
= 0, mortal_size
= 0;
5020 if (interned
== NULL
|| !PyDict_Check(interned
))
5022 keys
= PyDict_Keys(interned
);
5023 if (keys
== NULL
|| !PyList_Check(keys
)) {
5028 /* Since _Py_ReleaseInternedStrings() is intended to help a leak
5029 detector, interned strings are not forcibly deallocated; rather, we
5030 give them their stolen references back, and then clear and DECREF
5031 the interned dict. */
5033 n
= PyList_GET_SIZE(keys
);
5034 fprintf(stderr
, "releasing %" PY_FORMAT_SIZE_T
"d interned strings\n",
5036 for (i
= 0; i
< n
; i
++) {
5037 s
= (PyStringObject
*) PyList_GET_ITEM(keys
, i
);
5038 switch (s
->ob_sstate
) {
5039 case SSTATE_NOT_INTERNED
:
5040 /* XXX Shouldn't happen */
5042 case SSTATE_INTERNED_IMMORTAL
:
5044 immortal_size
+= Py_Size(s
);
5046 case SSTATE_INTERNED_MORTAL
:
5048 mortal_size
+= Py_Size(s
);
5051 Py_FatalError("Inconsistent interned string state.");
5053 s
->ob_sstate
= SSTATE_NOT_INTERNED
;
5055 fprintf(stderr
, "total size of all interned strings: "
5056 "%" PY_FORMAT_SIZE_T
"d/%" PY_FORMAT_SIZE_T
"d "
5057 "mortal/immortal\n", mortal_size
, immortal_size
);
5059 PyDict_Clear(interned
);
5060 Py_DECREF(interned
);