2 /* String object implementation */
9 int null_strings
, one_strings
;
20 static PyStringObject
*characters
[UCHAR_MAX
+ 1];
21 #ifndef DONT_SHARE_SHORT_STRINGS
22 static PyStringObject
*nullstring
;
26 Newsizedstringobject() and newstringobject() try in certain cases
27 to share string objects. When the size of the string is zero,
28 these routines always return a pointer to the same string object;
29 when the size is one, they return a pointer to an already existing
30 object if the contents of the string is known. For
31 newstringobject() this is always the case, for
32 newsizedstringobject() this is the case when the first argument in
34 A common practice to allocate a string and then fill it in or
35 change it must be done carefully. It is only allowed to change the
36 contents of the string if the obect was gotten from
37 newsizedstringobject() with a NULL first argument, because in the
38 future these routines may try to do even more sharing of objects.
41 PyString_FromStringAndSize(const char *str
, int size
)
43 register PyStringObject
*op
;
44 #ifndef DONT_SHARE_SHORT_STRINGS
45 if (size
== 0 && (op
= nullstring
) != NULL
) {
50 return (PyObject
*)op
;
52 if (size
== 1 && str
!= NULL
&&
53 (op
= characters
[*str
& UCHAR_MAX
]) != NULL
)
59 return (PyObject
*)op
;
61 #endif /* DONT_SHARE_SHORT_STRINGS */
63 /* PyObject_NewVar is inlined */
64 op
= (PyStringObject
*)
65 PyObject_MALLOC(sizeof(PyStringObject
) + size
* sizeof(char));
67 return PyErr_NoMemory();
68 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
73 op
->ob_sinterned
= NULL
;
76 memcpy(op
->ob_sval
, str
, size
);
77 op
->ob_sval
[size
] = '\0';
78 #ifndef DONT_SHARE_SHORT_STRINGS
82 } else if (size
== 1 && str
!= NULL
) {
83 characters
[*str
& UCHAR_MAX
] = op
;
87 return (PyObject
*) op
;
91 PyString_FromString(const char *str
)
93 register size_t size
= strlen(str
);
94 register PyStringObject
*op
;
96 PyErr_SetString(PyExc_OverflowError
,
97 "string is too long for a Python string");
100 #ifndef DONT_SHARE_SHORT_STRINGS
101 if (size
== 0 && (op
= nullstring
) != NULL
) {
106 return (PyObject
*)op
;
108 if (size
== 1 && (op
= characters
[*str
& UCHAR_MAX
]) != NULL
) {
113 return (PyObject
*)op
;
115 #endif /* DONT_SHARE_SHORT_STRINGS */
117 /* PyObject_NewVar is inlined */
118 op
= (PyStringObject
*)
119 PyObject_MALLOC(sizeof(PyStringObject
) + size
* sizeof(char));
121 return PyErr_NoMemory();
122 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
126 #ifdef INTERN_STRINGS
127 op
->ob_sinterned
= NULL
;
129 strcpy(op
->ob_sval
, str
);
130 #ifndef DONT_SHARE_SHORT_STRINGS
134 } else if (size
== 1) {
135 characters
[*str
& UCHAR_MAX
] = op
;
139 return (PyObject
*) op
;
142 PyObject
*PyString_Decode(const char *s
,
144 const char *encoding
,
147 PyObject
*buffer
= NULL
, *str
;
149 if (encoding
== NULL
)
150 encoding
= PyUnicode_GetDefaultEncoding();
152 /* Decode via the codec registry */
153 buffer
= PyBuffer_FromMemory((void *)s
, size
);
156 str
= PyCodec_Decode(buffer
, encoding
, errors
);
159 /* Convert Unicode to a string using the default encoding */
160 if (PyUnicode_Check(str
)) {
161 PyObject
*temp
= str
;
162 str
= PyUnicode_AsEncodedString(str
, NULL
, NULL
);
167 if (!PyString_Check(str
)) {
168 PyErr_Format(PyExc_TypeError
,
169 "decoder did not return a string object (type=%.400s)",
170 str
->ob_type
->tp_name
);
182 PyObject
*PyString_Encode(const char *s
,
184 const char *encoding
,
189 str
= PyString_FromStringAndSize(s
, size
);
192 v
= PyString_AsEncodedString(str
, encoding
, errors
);
197 PyObject
*PyString_AsEncodedString(PyObject
*str
,
198 const char *encoding
,
203 if (!PyString_Check(str
)) {
208 if (encoding
== NULL
)
209 encoding
= PyUnicode_GetDefaultEncoding();
211 /* Encode via the codec registry */
212 v
= PyCodec_Encode(str
, encoding
, errors
);
215 /* Convert Unicode to a string using the default encoding */
216 if (PyUnicode_Check(v
)) {
218 v
= PyUnicode_AsEncodedString(v
, NULL
, NULL
);
223 if (!PyString_Check(v
)) {
224 PyErr_Format(PyExc_TypeError
,
225 "encoder did not return a string object (type=%.400s)",
226 v
->ob_type
->tp_name
);
237 string_dealloc(PyObject
*op
)
243 string_getsize(register PyObject
*op
)
247 if (PyString_AsStringAndSize(op
, &s
, &len
))
252 static /*const*/ char *
253 string_getbuffer(register PyObject
*op
)
257 if (PyString_AsStringAndSize(op
, &s
, &len
))
263 PyString_Size(register PyObject
*op
)
265 if (!PyString_Check(op
))
266 return string_getsize(op
);
267 return ((PyStringObject
*)op
) -> ob_size
;
271 PyString_AsString(register PyObject
*op
)
273 if (!PyString_Check(op
))
274 return string_getbuffer(op
);
275 return ((PyStringObject
*)op
) -> ob_sval
;
278 /* Internal API needed by PyString_AsStringAndSize(): */
280 PyObject
*_PyUnicode_AsDefaultEncodedString(PyObject
*unicode
,
284 PyString_AsStringAndSize(register PyObject
*obj
,
289 PyErr_BadInternalCall();
293 if (!PyString_Check(obj
)) {
294 if (PyUnicode_Check(obj
)) {
295 obj
= _PyUnicode_AsDefaultEncodedString(obj
, NULL
);
300 PyErr_Format(PyExc_TypeError
,
301 "expected string or Unicode object, "
302 "%.200s found", obj
->ob_type
->tp_name
);
307 *s
= PyString_AS_STRING(obj
);
309 *len
= PyString_GET_SIZE(obj
);
310 else if ((int)strlen(*s
) != PyString_GET_SIZE(obj
)) {
311 PyErr_SetString(PyExc_TypeError
,
312 "expected string without null bytes");
321 string_print(PyStringObject
*op
, FILE *fp
, int flags
)
326 /* XXX Ought to check for interrupts when writing long strings */
327 if (flags
& Py_PRINT_RAW
) {
328 fwrite(op
->ob_sval
, 1, (int) op
->ob_size
, fp
);
332 /* figure out which quote to use; single is preferred */
334 if (strchr(op
->ob_sval
, '\'') && !strchr(op
->ob_sval
, '"'))
338 for (i
= 0; i
< op
->ob_size
; i
++) {
340 if (c
== quote
|| c
== '\\')
341 fprintf(fp
, "\\%c", c
);
342 else if (c
< ' ' || c
>= 0177)
343 fprintf(fp
, "\\%03o", c
& 0377);
352 string_repr(register PyStringObject
*op
)
354 size_t newsize
= 2 + 4 * op
->ob_size
* sizeof(char);
356 if (newsize
> INT_MAX
) {
357 PyErr_SetString(PyExc_OverflowError
,
358 "string is too large to make repr");
360 v
= PyString_FromStringAndSize((char *)NULL
, newsize
);
370 /* figure out which quote to use; single is preferred */
372 if (strchr(op
->ob_sval
, '\'') && !strchr(op
->ob_sval
, '"'))
375 p
= ((PyStringObject
*)v
)->ob_sval
;
377 for (i
= 0; i
< op
->ob_size
; i
++) {
379 if (c
== quote
|| c
== '\\')
380 *p
++ = '\\', *p
++ = c
;
381 else if (c
< ' ' || c
>= 0177) {
382 sprintf(p
, "\\%03o", c
& 0377);
392 &v
, (int) (p
- ((PyStringObject
*)v
)->ob_sval
));
398 string_length(PyStringObject
*a
)
404 string_concat(register PyStringObject
*a
, register PyObject
*bb
)
406 register unsigned int size
;
407 register PyStringObject
*op
;
408 if (!PyString_Check(bb
)) {
409 if (PyUnicode_Check(bb
))
410 return PyUnicode_Concat((PyObject
*)a
, bb
);
411 PyErr_Format(PyExc_TypeError
,
412 "cannot add type \"%.200s\" to string",
413 bb
->ob_type
->tp_name
);
416 #define b ((PyStringObject *)bb)
417 /* Optimize cases with empty left or right operand */
418 if (a
->ob_size
== 0) {
422 if (b
->ob_size
== 0) {
424 return (PyObject
*)a
;
426 size
= a
->ob_size
+ b
->ob_size
;
427 /* PyObject_NewVar is inlined */
428 op
= (PyStringObject
*)
429 PyObject_MALLOC(sizeof(PyStringObject
) + size
* sizeof(char));
431 return PyErr_NoMemory();
432 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
436 #ifdef INTERN_STRINGS
437 op
->ob_sinterned
= NULL
;
439 memcpy(op
->ob_sval
, a
->ob_sval
, (int) a
->ob_size
);
440 memcpy(op
->ob_sval
+ a
->ob_size
, b
->ob_sval
, (int) b
->ob_size
);
441 op
->ob_sval
[size
] = '\0';
442 return (PyObject
*) op
;
447 string_repeat(register PyStringObject
*a
, register int n
)
451 register PyStringObject
*op
;
455 /* watch out for overflows: the size can overflow int,
456 * and the # of bytes needed can overflow size_t
458 size
= a
->ob_size
* n
;
459 if (n
&& size
/ n
!= a
->ob_size
) {
460 PyErr_SetString(PyExc_OverflowError
,
461 "repeated string is too long");
464 if (size
== a
->ob_size
) {
466 return (PyObject
*)a
;
468 nbytes
= size
* sizeof(char);
469 if (nbytes
/ sizeof(char) != (size_t)size
||
470 nbytes
+ sizeof(PyStringObject
) <= nbytes
) {
471 PyErr_SetString(PyExc_OverflowError
,
472 "repeated string is too long");
475 op
= (PyStringObject
*)
476 PyObject_MALLOC(sizeof(PyStringObject
) + nbytes
);
478 return PyErr_NoMemory();
479 PyObject_INIT_VAR(op
, &PyString_Type
, size
);
483 #ifdef INTERN_STRINGS
484 op
->ob_sinterned
= NULL
;
486 for (i
= 0; i
< size
; i
+= a
->ob_size
)
487 memcpy(op
->ob_sval
+i
, a
->ob_sval
, (int) a
->ob_size
);
488 op
->ob_sval
[size
] = '\0';
489 return (PyObject
*) op
;
492 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
495 string_slice(register PyStringObject
*a
, register int i
, register int j
)
496 /* j -- may be negative! */
501 j
= 0; /* Avoid signed/unsigned bug in next line */
504 if (i
== 0 && j
== a
->ob_size
) { /* It's the same as a */
506 return (PyObject
*)a
;
510 return PyString_FromStringAndSize(a
->ob_sval
+ i
, (int) (j
-i
));
514 string_contains(PyObject
*a
, PyObject
*el
)
516 register char *s
, *end
;
518 if (PyUnicode_Check(el
))
519 return PyUnicode_Contains(a
, el
);
520 if (!PyString_Check(el
) || PyString_Size(el
) != 1) {
521 PyErr_SetString(PyExc_TypeError
,
522 "'in <string>' requires character as left operand");
525 c
= PyString_AsString(el
)[0];
526 s
= PyString_AsString(a
);
527 end
= s
+ PyString_Size(a
);
536 string_item(PyStringObject
*a
, register int i
)
540 if (i
< 0 || i
>= a
->ob_size
) {
541 PyErr_SetString(PyExc_IndexError
, "string index out of range");
544 c
= a
->ob_sval
[i
] & UCHAR_MAX
;
545 v
= (PyObject
*) characters
[c
];
551 v
= PyString_FromStringAndSize((char *)NULL
, 1);
554 characters
[c
] = (PyStringObject
*) v
;
555 ((PyStringObject
*)v
)->ob_sval
[0] = c
;
562 string_compare(PyStringObject
*a
, PyStringObject
*b
)
564 int len_a
= a
->ob_size
, len_b
= b
->ob_size
;
565 int min_len
= (len_a
< len_b
) ? len_a
: len_b
;
568 cmp
= Py_CHARMASK(*a
->ob_sval
) - Py_CHARMASK(*b
->ob_sval
);
570 cmp
= memcmp(a
->ob_sval
, b
->ob_sval
, min_len
);
574 return (len_a
< len_b
) ? -1 : (len_a
> len_b
) ? 1 : 0;
578 string_hash(PyStringObject
*a
)
581 register unsigned char *p
;
585 if (a
->ob_shash
!= -1)
587 #ifdef INTERN_STRINGS
588 if (a
->ob_sinterned
!= NULL
)
589 return (a
->ob_shash
=
590 ((PyStringObject
*)(a
->ob_sinterned
))->ob_shash
);
594 p
= (unsigned char *) a
->ob_sval
;
597 x
= (1000003*x
) ^ *p
++;
608 string_buffer_getreadbuf(PyStringObject
*self
, int index
, const void **ptr
)
611 PyErr_SetString(PyExc_SystemError
,
612 "accessing non-existent string segment");
615 *ptr
= (void *)self
->ob_sval
;
616 return self
->ob_size
;
620 string_buffer_getwritebuf(PyStringObject
*self
, int index
, const void **ptr
)
622 PyErr_SetString(PyExc_TypeError
,
623 "Cannot use string as modifiable buffer");
628 string_buffer_getsegcount(PyStringObject
*self
, int *lenp
)
631 *lenp
= self
->ob_size
;
636 string_buffer_getcharbuf(PyStringObject
*self
, int index
, const char **ptr
)
639 PyErr_SetString(PyExc_SystemError
,
640 "accessing non-existent string segment");
643 *ptr
= self
->ob_sval
;
644 return self
->ob_size
;
647 static PySequenceMethods string_as_sequence
= {
648 (inquiry
)string_length
, /*sq_length*/
649 (binaryfunc
)string_concat
, /*sq_concat*/
650 (intargfunc
)string_repeat
, /*sq_repeat*/
651 (intargfunc
)string_item
, /*sq_item*/
652 (intintargfunc
)string_slice
, /*sq_slice*/
655 (objobjproc
)string_contains
/*sq_contains*/
658 static PyBufferProcs string_as_buffer
= {
659 (getreadbufferproc
)string_buffer_getreadbuf
,
660 (getwritebufferproc
)string_buffer_getwritebuf
,
661 (getsegcountproc
)string_buffer_getsegcount
,
662 (getcharbufferproc
)string_buffer_getcharbuf
,
673 split_whitespace(const char *s
, int len
, int maxsplit
)
677 PyObject
*list
= PyList_New(0);
682 for (i
= j
= 0; i
< len
; ) {
683 while (i
< len
&& isspace(Py_CHARMASK(s
[i
])))
686 while (i
< len
&& !isspace(Py_CHARMASK(s
[i
])))
691 item
= PyString_FromStringAndSize(s
+j
, (int)(i
-j
));
694 err
= PyList_Append(list
, item
);
698 while (i
< len
&& isspace(Py_CHARMASK(s
[i
])))
704 item
= PyString_FromStringAndSize(s
+j
, (int)(len
- j
));
707 err
= PyList_Append(list
, item
);
719 static char split__doc__
[] =
720 "S.split([sep [,maxsplit]]) -> list of strings\n\
722 Return a list of the words in the string S, using sep as the\n\
723 delimiter string. If maxsplit is given, at most maxsplit\n\
724 splits are done. If sep is not specified, any whitespace string\n\
728 string_split(PyStringObject
*self
, PyObject
*args
)
730 int len
= PyString_GET_SIZE(self
), n
, i
, j
, err
;
732 const char *s
= PyString_AS_STRING(self
), *sub
;
733 PyObject
*list
, *item
, *subobj
= Py_None
;
735 if (!PyArg_ParseTuple(args
, "|Oi:split", &subobj
, &maxsplit
))
739 if (subobj
== Py_None
)
740 return split_whitespace(s
, len
, maxsplit
);
741 if (PyString_Check(subobj
)) {
742 sub
= PyString_AS_STRING(subobj
);
743 n
= PyString_GET_SIZE(subobj
);
745 else if (PyUnicode_Check(subobj
))
746 return PyUnicode_Split((PyObject
*)self
, subobj
, maxsplit
);
747 else if (PyObject_AsCharBuffer(subobj
, &sub
, &n
))
750 PyErr_SetString(PyExc_ValueError
, "empty separator");
754 list
= PyList_New(0);
760 if (s
[i
] == sub
[0] && memcmp(s
+i
, sub
, n
) == 0) {
763 item
= PyString_FromStringAndSize(s
+j
, (int)(i
-j
));
766 err
= PyList_Append(list
, item
);
775 item
= PyString_FromStringAndSize(s
+j
, (int)(len
-j
));
778 err
= PyList_Append(list
, item
);
791 static char join__doc__
[] =
792 "S.join(sequence) -> string\n\
794 Return a string which is the concatenation of the strings in the\n\
795 sequence. The separator between elements is S.";
798 string_join(PyStringObject
*self
, PyObject
*args
)
800 char *sep
= PyString_AS_STRING(self
);
801 int seplen
= PyString_GET_SIZE(self
);
802 PyObject
*res
= NULL
;
807 int i
, slen
, sz_incr
;
808 PyObject
*orig
, *seq
, *item
;
810 if (!PyArg_ParseTuple(args
, "O:join", &orig
))
813 if (!(seq
= PySequence_Fast(orig
, ""))) {
814 if (PyErr_ExceptionMatches(PyExc_TypeError
))
815 PyErr_Format(PyExc_TypeError
,
816 "sequence expected, %.80s found",
817 orig
->ob_type
->tp_name
);
820 /* From here on out, errors go through finally: for proper
821 * reference count manipulations.
823 seqlen
= PySequence_Size(seq
);
825 item
= PySequence_Fast_GET_ITEM(seq
, 0);
831 if (!(res
= PyString_FromStringAndSize((char*)NULL
, sz
)))
834 p
= PyString_AS_STRING(res
);
836 for (i
= 0; i
< seqlen
; i
++) {
837 item
= PySequence_Fast_GET_ITEM(seq
, i
);
838 if (!PyString_Check(item
)){
839 if (PyUnicode_Check(item
)) {
842 return PyUnicode_Join((PyObject
*)self
, seq
);
844 PyErr_Format(PyExc_TypeError
,
845 "sequence item %i: expected string,"
847 i
, item
->ob_type
->tp_name
);
850 slen
= PyString_GET_SIZE(item
);
851 while (reslen
+ slen
+ seplen
>= sz
) {
852 /* at least double the size of the string */
853 sz_incr
= slen
+ seplen
> sz
? slen
+ seplen
: sz
;
854 if (_PyString_Resize(&res
, sz
+ sz_incr
)) {
858 p
= PyString_AS_STRING(res
) + reslen
;
861 memcpy(p
, sep
, seplen
);
865 memcpy(p
, PyString_AS_STRING(item
), slen
);
869 if (_PyString_Resize(&res
, reslen
))
883 string_find_internal(PyStringObject
*self
, PyObject
*args
, int dir
)
885 const char *s
= PyString_AS_STRING(self
), *sub
;
886 int len
= PyString_GET_SIZE(self
);
887 int n
, i
= 0, last
= INT_MAX
;
890 if (!PyArg_ParseTuple(args
, "O|O&O&:find/rfind/index/rindex",
891 &subobj
, _PyEval_SliceIndex
, &i
, _PyEval_SliceIndex
, &last
))
893 if (PyString_Check(subobj
)) {
894 sub
= PyString_AS_STRING(subobj
);
895 n
= PyString_GET_SIZE(subobj
);
897 else if (PyUnicode_Check(subobj
))
898 return PyUnicode_Find((PyObject
*)self
, subobj
, i
, last
, 1);
899 else if (PyObject_AsCharBuffer(subobj
, &sub
, &n
))
914 if (n
== 0 && i
<= last
)
917 for (; i
<= last
; ++i
)
918 if (s
[i
] == sub
[0] && memcmp(&s
[i
], sub
, n
) == 0)
924 if (n
== 0 && i
<= last
)
926 for (j
= last
-n
; j
>= i
; --j
)
927 if (s
[j
] == sub
[0] && memcmp(&s
[j
], sub
, n
) == 0)
935 static char find__doc__
[] =
936 "S.find(sub [,start [,end]]) -> int\n\
938 Return the lowest index in S where substring sub is found,\n\
939 such that sub is contained within s[start,end]. Optional\n\
940 arguments start and end are interpreted as in slice notation.\n\
942 Return -1 on failure.";
945 string_find(PyStringObject
*self
, PyObject
*args
)
947 long result
= string_find_internal(self
, args
, +1);
950 return PyInt_FromLong(result
);
954 static char index__doc__
[] =
955 "S.index(sub [,start [,end]]) -> int\n\
957 Like S.find() but raise ValueError when the substring is not found.";
960 string_index(PyStringObject
*self
, PyObject
*args
)
962 long result
= string_find_internal(self
, args
, +1);
966 PyErr_SetString(PyExc_ValueError
,
967 "substring not found in string.index");
970 return PyInt_FromLong(result
);
974 static char rfind__doc__
[] =
975 "S.rfind(sub [,start [,end]]) -> int\n\
977 Return the highest index in S where substring sub is found,\n\
978 such that sub is contained within s[start,end]. Optional\n\
979 arguments start and end are interpreted as in slice notation.\n\
981 Return -1 on failure.";
984 string_rfind(PyStringObject
*self
, PyObject
*args
)
986 long result
= string_find_internal(self
, args
, -1);
989 return PyInt_FromLong(result
);
993 static char rindex__doc__
[] =
994 "S.rindex(sub [,start [,end]]) -> int\n\
996 Like S.rfind() but raise ValueError when the substring is not found.";
999 string_rindex(PyStringObject
*self
, PyObject
*args
)
1001 long result
= string_find_internal(self
, args
, -1);
1005 PyErr_SetString(PyExc_ValueError
,
1006 "substring not found in string.rindex");
1009 return PyInt_FromLong(result
);
1014 do_strip(PyStringObject
*self
, PyObject
*args
, int striptype
)
1016 char *s
= PyString_AS_STRING(self
);
1017 int len
= PyString_GET_SIZE(self
), i
, j
;
1019 if (!PyArg_ParseTuple(args
, ":strip"))
1023 if (striptype
!= RIGHTSTRIP
) {
1024 while (i
< len
&& isspace(Py_CHARMASK(s
[i
]))) {
1030 if (striptype
!= LEFTSTRIP
) {
1033 } while (j
>= i
&& isspace(Py_CHARMASK(s
[j
])));
1037 if (i
== 0 && j
== len
) {
1039 return (PyObject
*)self
;
1042 return PyString_FromStringAndSize(s
+i
, j
-i
);
1046 static char strip__doc__
[] =
1047 "S.strip() -> string\n\
1049 Return a copy of the string S with leading and trailing\n\
1050 whitespace removed.";
1053 string_strip(PyStringObject
*self
, PyObject
*args
)
1055 return do_strip(self
, args
, BOTHSTRIP
);
1059 static char lstrip__doc__
[] =
1060 "S.lstrip() -> string\n\
1062 Return a copy of the string S with leading whitespace removed.";
1065 string_lstrip(PyStringObject
*self
, PyObject
*args
)
1067 return do_strip(self
, args
, LEFTSTRIP
);
1071 static char rstrip__doc__
[] =
1072 "S.rstrip() -> string\n\
1074 Return a copy of the string S with trailing whitespace removed.";
1077 string_rstrip(PyStringObject
*self
, PyObject
*args
)
1079 return do_strip(self
, args
, RIGHTSTRIP
);
1083 static char lower__doc__
[] =
1084 "S.lower() -> string\n\
1086 Return a copy of the string S converted to lowercase.";
1089 string_lower(PyStringObject
*self
, PyObject
*args
)
1091 char *s
= PyString_AS_STRING(self
), *s_new
;
1092 int i
, n
= PyString_GET_SIZE(self
);
1095 if (!PyArg_ParseTuple(args
, ":lower"))
1097 new = PyString_FromStringAndSize(NULL
, n
);
1100 s_new
= PyString_AsString(new);
1101 for (i
= 0; i
< n
; i
++) {
1102 int c
= Py_CHARMASK(*s
++);
1104 *s_new
= tolower(c
);
1113 static char upper__doc__
[] =
1114 "S.upper() -> string\n\
1116 Return a copy of the string S converted to uppercase.";
1119 string_upper(PyStringObject
*self
, PyObject
*args
)
1121 char *s
= PyString_AS_STRING(self
), *s_new
;
1122 int i
, n
= PyString_GET_SIZE(self
);
1125 if (!PyArg_ParseTuple(args
, ":upper"))
1127 new = PyString_FromStringAndSize(NULL
, n
);
1130 s_new
= PyString_AsString(new);
1131 for (i
= 0; i
< n
; i
++) {
1132 int c
= Py_CHARMASK(*s
++);
1134 *s_new
= toupper(c
);
1143 static char title__doc__
[] =
1144 "S.title() -> string\n\
1146 Return a titlecased version of S, i.e. words start with uppercase\n\
1147 characters, all remaining cased characters have lowercase.";
1150 string_title(PyUnicodeObject
*self
, PyObject
*args
)
1152 char *s
= PyString_AS_STRING(self
), *s_new
;
1153 int i
, n
= PyString_GET_SIZE(self
);
1154 int previous_is_cased
= 0;
1157 if (!PyArg_ParseTuple(args
, ":title"))
1159 new = PyString_FromStringAndSize(NULL
, n
);
1162 s_new
= PyString_AsString(new);
1163 for (i
= 0; i
< n
; i
++) {
1164 int c
= Py_CHARMASK(*s
++);
1166 if (!previous_is_cased
)
1168 previous_is_cased
= 1;
1169 } else if (isupper(c
)) {
1170 if (previous_is_cased
)
1172 previous_is_cased
= 1;
1174 previous_is_cased
= 0;
1180 static char capitalize__doc__
[] =
1181 "S.capitalize() -> string\n\
1183 Return a copy of the string S with only its first character\n\
1187 string_capitalize(PyStringObject
*self
, PyObject
*args
)
1189 char *s
= PyString_AS_STRING(self
), *s_new
;
1190 int i
, n
= PyString_GET_SIZE(self
);
1193 if (!PyArg_ParseTuple(args
, ":capitalize"))
1195 new = PyString_FromStringAndSize(NULL
, n
);
1198 s_new
= PyString_AsString(new);
1200 int c
= Py_CHARMASK(*s
++);
1202 *s_new
= toupper(c
);
1207 for (i
= 1; i
< n
; i
++) {
1208 int c
= Py_CHARMASK(*s
++);
1210 *s_new
= tolower(c
);
1219 static char count__doc__
[] =
1220 "S.count(sub[, start[, end]]) -> int\n\
1222 Return the number of occurrences of substring sub in string\n\
1223 S[start:end]. Optional arguments start and end are\n\
1224 interpreted as in slice notation.";
1227 string_count(PyStringObject
*self
, PyObject
*args
)
1229 const char *s
= PyString_AS_STRING(self
), *sub
;
1230 int len
= PyString_GET_SIZE(self
), n
;
1231 int i
= 0, last
= INT_MAX
;
1235 if (!PyArg_ParseTuple(args
, "O|O&O&:count", &subobj
,
1236 _PyEval_SliceIndex
, &i
, _PyEval_SliceIndex
, &last
))
1239 if (PyString_Check(subobj
)) {
1240 sub
= PyString_AS_STRING(subobj
);
1241 n
= PyString_GET_SIZE(subobj
);
1243 else if (PyUnicode_Check(subobj
))
1244 return PyInt_FromLong(
1245 PyUnicode_Count((PyObject
*)self
, subobj
, i
, last
));
1246 else if (PyObject_AsCharBuffer(subobj
, &sub
, &n
))
1261 return PyInt_FromLong((long) (m
-i
));
1265 if (!memcmp(s
+i
, sub
, n
)) {
1272 return PyInt_FromLong((long) r
);
1276 static char swapcase__doc__
[] =
1277 "S.swapcase() -> string\n\
1279 Return a copy of the string S with uppercase characters\n\
1280 converted to lowercase and vice versa.";
1283 string_swapcase(PyStringObject
*self
, PyObject
*args
)
1285 char *s
= PyString_AS_STRING(self
), *s_new
;
1286 int i
, n
= PyString_GET_SIZE(self
);
1289 if (!PyArg_ParseTuple(args
, ":swapcase"))
1291 new = PyString_FromStringAndSize(NULL
, n
);
1294 s_new
= PyString_AsString(new);
1295 for (i
= 0; i
< n
; i
++) {
1296 int c
= Py_CHARMASK(*s
++);
1298 *s_new
= toupper(c
);
1300 else if (isupper(c
)) {
1301 *s_new
= tolower(c
);
1311 static char translate__doc__
[] =
1312 "S.translate(table [,deletechars]) -> string\n\
1314 Return a copy of the string S, where all characters occurring\n\
1315 in the optional argument deletechars are removed, and the\n\
1316 remaining characters have been mapped through the given\n\
1317 translation table, which must be a string of length 256.";
1320 string_translate(PyStringObject
*self
, PyObject
*args
)
1322 register char *input
, *output
;
1323 register const char *table
;
1324 register int i
, c
, changed
= 0;
1325 PyObject
*input_obj
= (PyObject
*)self
;
1326 const char *table1
, *output_start
, *del_table
=NULL
;
1327 int inlen
, tablen
, dellen
= 0;
1329 int trans_table
[256];
1330 PyObject
*tableobj
, *delobj
= NULL
;
1332 if (!PyArg_ParseTuple(args
, "O|O:translate",
1333 &tableobj
, &delobj
))
1336 if (PyString_Check(tableobj
)) {
1337 table1
= PyString_AS_STRING(tableobj
);
1338 tablen
= PyString_GET_SIZE(tableobj
);
1340 else if (PyUnicode_Check(tableobj
)) {
1341 /* Unicode .translate() does not support the deletechars
1342 parameter; instead a mapping to None will cause characters
1344 if (delobj
!= NULL
) {
1345 PyErr_SetString(PyExc_TypeError
,
1346 "deletions are implemented differently for unicode");
1349 return PyUnicode_Translate((PyObject
*)self
, tableobj
, NULL
);
1351 else if (PyObject_AsCharBuffer(tableobj
, &table1
, &tablen
))
1354 if (delobj
!= NULL
) {
1355 if (PyString_Check(delobj
)) {
1356 del_table
= PyString_AS_STRING(delobj
);
1357 dellen
= PyString_GET_SIZE(delobj
);
1359 else if (PyUnicode_Check(delobj
)) {
1360 PyErr_SetString(PyExc_TypeError
,
1361 "deletions are implemented differently for unicode");
1364 else if (PyObject_AsCharBuffer(delobj
, &del_table
, &dellen
))
1367 if (tablen
!= 256) {
1368 PyErr_SetString(PyExc_ValueError
,
1369 "translation table must be 256 characters long");
1379 inlen
= PyString_Size(input_obj
);
1380 result
= PyString_FromStringAndSize((char *)NULL
, inlen
);
1383 output_start
= output
= PyString_AsString(result
);
1384 input
= PyString_AsString(input_obj
);
1387 /* If no deletions are required, use faster code */
1388 for (i
= inlen
; --i
>= 0; ) {
1389 c
= Py_CHARMASK(*input
++);
1390 if (Py_CHARMASK((*output
++ = table
[c
])) != c
)
1396 Py_INCREF(input_obj
);
1400 for (i
= 0; i
< 256; i
++)
1401 trans_table
[i
] = Py_CHARMASK(table
[i
]);
1403 for (i
= 0; i
< dellen
; i
++)
1404 trans_table
[(int) Py_CHARMASK(del_table
[i
])] = -1;
1406 for (i
= inlen
; --i
>= 0; ) {
1407 c
= Py_CHARMASK(*input
++);
1408 if (trans_table
[c
] != -1)
1409 if (Py_CHARMASK(*output
++ = (char)trans_table
[c
]) == c
)
1415 Py_INCREF(input_obj
);
1418 /* Fix the size of the resulting string */
1419 if (inlen
> 0 &&_PyString_Resize(&result
, output
-output_start
))
1425 /* What follows is used for implementing replace(). Perry Stoll. */
1430 strstr replacement for arbitrary blocks of memory.
1432 Locates the first occurrence in the memory pointed to by MEM of the
1433 contents of memory pointed to by PAT. Returns the index into MEM if
1434 found, or -1 if not found. If len of PAT is greater than length of
1435 MEM, the function returns -1.
1438 mymemfind(const char *mem
, int len
, const char *pat
, int pat_len
)
1442 /* pattern can not occur in the last pat_len-1 chars */
1445 for (ii
= 0; ii
<= len
; ii
++) {
1446 if (mem
[ii
] == pat
[0] && memcmp(&mem
[ii
], pat
, pat_len
) == 0) {
1456 Return the number of distinct times PAT is found in MEM.
1457 meaning mem=1111 and pat==11 returns 2.
1458 mem=11111 and pat==11 also return 2.
1461 mymemcnt(const char *mem
, int len
, const char *pat
, int pat_len
)
1463 register int offset
= 0;
1467 offset
= mymemfind(mem
, len
, pat
, pat_len
);
1470 mem
+= offset
+ pat_len
;
1471 len
-= offset
+ pat_len
;
1480 Return a string in which all occurrences of PAT in memory STR are
1483 If length of PAT is less than length of STR or there are no occurrences
1484 of PAT in STR, then the original string is returned. Otherwise, a new
1485 string is allocated here and returned.
1487 on return, out_len is:
1488 the length of output string, or
1489 -1 if the input string is returned, or
1490 unchanged if an error occurs (no memory).
1493 the new string allocated locally, or
1494 NULL if an error occurred.
1497 mymemreplace(const char *str
, int len
, /* input string */
1498 const char *pat
, int pat_len
, /* pattern string to find */
1499 const char *sub
, int sub_len
, /* substitution string */
1500 int count
, /* number of replacements */
1505 int nfound
, offset
, new_len
;
1507 if (len
== 0 || pat_len
> len
)
1510 /* find length of output string */
1511 nfound
= mymemcnt(str
, len
, pat
, pat_len
);
1514 else if (nfound
> count
)
1518 new_len
= len
+ nfound
*(sub_len
- pat_len
);
1520 new_s
= (char *)PyMem_MALLOC(new_len
);
1521 if (new_s
== NULL
) return NULL
;
1527 /* find index of next instance of pattern */
1528 offset
= mymemfind(str
, len
, pat
, pat_len
);
1529 /* if not found, break out of loop */
1530 if (offset
== -1) break;
1532 /* copy non matching part of input string */
1533 memcpy(new_s
, str
, offset
); /* copy part of str before pat */
1534 str
+= offset
+ pat_len
; /* move str past pattern */
1535 len
-= offset
+ pat_len
; /* reduce length of str remaining */
1537 /* copy substitute into the output string */
1538 new_s
+= offset
; /* move new_s to dest for sub string */
1539 memcpy(new_s
, sub
, sub_len
); /* copy substring into new_s */
1540 new_s
+= sub_len
; /* offset new_s past sub string */
1542 /* break when we've done count replacements */
1543 if (--count
== 0) break;
1545 /* copy any remaining values into output string */
1547 memcpy(new_s
, str
, len
);
1552 return (char*)str
; /* have to cast away constness here */
1556 static char replace__doc__
[] =
1557 "S.replace (old, new[, maxsplit]) -> string\n\
1559 Return a copy of string S with all occurrences of substring\n\
1560 old replaced by new. If the optional argument maxsplit is\n\
1561 given, only the first maxsplit occurrences are replaced.";
1564 string_replace(PyStringObject
*self
, PyObject
*args
)
1566 const char *str
= PyString_AS_STRING(self
), *sub
, *repl
;
1568 int len
= PyString_GET_SIZE(self
), sub_len
, repl_len
, out_len
;
1571 PyObject
*subobj
, *replobj
;
1573 if (!PyArg_ParseTuple(args
, "OO|i:replace",
1574 &subobj
, &replobj
, &count
))
1577 if (PyString_Check(subobj
)) {
1578 sub
= PyString_AS_STRING(subobj
);
1579 sub_len
= PyString_GET_SIZE(subobj
);
1581 else if (PyUnicode_Check(subobj
))
1582 return PyUnicode_Replace((PyObject
*)self
,
1583 subobj
, replobj
, count
);
1584 else if (PyObject_AsCharBuffer(subobj
, &sub
, &sub_len
))
1587 if (PyString_Check(replobj
)) {
1588 repl
= PyString_AS_STRING(replobj
);
1589 repl_len
= PyString_GET_SIZE(replobj
);
1591 else if (PyUnicode_Check(replobj
))
1592 return PyUnicode_Replace((PyObject
*)self
,
1593 subobj
, replobj
, count
);
1594 else if (PyObject_AsCharBuffer(replobj
, &repl
, &repl_len
))
1598 PyErr_SetString(PyExc_ValueError
, "empty pattern string");
1601 new_s
= mymemreplace(str
,len
,sub
,sub_len
,repl
,repl_len
,count
,&out_len
);
1602 if (new_s
== NULL
) {
1606 if (out_len
== -1) {
1607 /* we're returning another reference to self */
1608 new = (PyObject
*)self
;
1612 new = PyString_FromStringAndSize(new_s
, out_len
);
1619 static char startswith__doc__
[] =
1620 "S.startswith(prefix[, start[, end]]) -> int\n\
1622 Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
1623 optional start, test S beginning at that position. With optional end, stop\n\
1624 comparing S at that position.";
1627 string_startswith(PyStringObject
*self
, PyObject
*args
)
1629 const char* str
= PyString_AS_STRING(self
);
1630 int len
= PyString_GET_SIZE(self
);
1637 if (!PyArg_ParseTuple(args
, "O|O&O&:startswith", &subobj
,
1638 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
1640 if (PyString_Check(subobj
)) {
1641 prefix
= PyString_AS_STRING(subobj
);
1642 plen
= PyString_GET_SIZE(subobj
);
1644 else if (PyUnicode_Check(subobj
))
1645 return PyInt_FromLong(
1646 PyUnicode_Tailmatch((PyObject
*)self
,
1647 subobj
, start
, end
, -1));
1648 else if (PyObject_AsCharBuffer(subobj
, &prefix
, &plen
))
1651 /* adopt Java semantics for index out of range. it is legal for
1652 * offset to be == plen, but this only returns true if prefix is
1655 if (start
< 0 || start
+plen
> len
)
1656 return PyInt_FromLong(0);
1658 if (!memcmp(str
+start
, prefix
, plen
)) {
1659 /* did the match end after the specified end? */
1661 return PyInt_FromLong(1);
1662 else if (end
- start
< plen
)
1663 return PyInt_FromLong(0);
1665 return PyInt_FromLong(1);
1667 else return PyInt_FromLong(0);
1671 static char endswith__doc__
[] =
1672 "S.endswith(suffix[, start[, end]]) -> int\n\
1674 Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
1675 optional start, test S beginning at that position. With optional end, stop\n\
1676 comparing S at that position.";
1679 string_endswith(PyStringObject
*self
, PyObject
*args
)
1681 const char* str
= PyString_AS_STRING(self
);
1682 int len
= PyString_GET_SIZE(self
);
1690 if (!PyArg_ParseTuple(args
, "O|O&O&:endswith", &subobj
,
1691 _PyEval_SliceIndex
, &start
, _PyEval_SliceIndex
, &end
))
1693 if (PyString_Check(subobj
)) {
1694 suffix
= PyString_AS_STRING(subobj
);
1695 slen
= PyString_GET_SIZE(subobj
);
1697 else if (PyUnicode_Check(subobj
))
1698 return PyInt_FromLong(
1699 PyUnicode_Tailmatch((PyObject
*)self
,
1700 subobj
, start
, end
, +1));
1701 else if (PyObject_AsCharBuffer(subobj
, &suffix
, &slen
))
1704 if (start
< 0 || start
> len
|| slen
> len
)
1705 return PyInt_FromLong(0);
1707 upper
= (end
>= 0 && end
<= len
) ? end
: len
;
1708 lower
= (upper
- slen
) > start
? (upper
- slen
) : start
;
1710 if (upper
-lower
>= slen
&& !memcmp(str
+lower
, suffix
, slen
))
1711 return PyInt_FromLong(1);
1712 else return PyInt_FromLong(0);
1716 static char encode__doc__
[] =
1717 "S.encode([encoding[,errors]]) -> string\n\
1719 Return an encoded string version of S. Default encoding is the current\n\
1720 default string encoding. errors may be given to set a different error\n\
1721 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
1722 a ValueError. Other possible values are 'ignore' and 'replace'.";
1725 string_encode(PyStringObject
*self
, PyObject
*args
)
1727 char *encoding
= NULL
;
1728 char *errors
= NULL
;
1729 if (!PyArg_ParseTuple(args
, "|ss:encode", &encoding
, &errors
))
1731 return PyString_AsEncodedString((PyObject
*)self
, encoding
, errors
);
1735 static char expandtabs__doc__
[] =
1736 "S.expandtabs([tabsize]) -> string\n\
1738 Return a copy of S where all tab characters are expanded using spaces.\n\
1739 If tabsize is not given, a tab size of 8 characters is assumed.";
1742 string_expandtabs(PyStringObject
*self
, PyObject
*args
)
1750 if (!PyArg_ParseTuple(args
, "|i:expandtabs", &tabsize
))
1753 /* First pass: determine size of output string */
1755 e
= PyString_AS_STRING(self
) + PyString_GET_SIZE(self
);
1756 for (p
= PyString_AS_STRING(self
); p
< e
; p
++)
1759 j
+= tabsize
- (j
% tabsize
);
1763 if (*p
== '\n' || *p
== '\r') {
1769 /* Second pass: create output string and fill it */
1770 u
= PyString_FromStringAndSize(NULL
, i
+ j
);
1775 q
= PyString_AS_STRING(u
);
1777 for (p
= PyString_AS_STRING(self
); p
< e
; p
++)
1780 i
= tabsize
- (j
% tabsize
);
1789 if (*p
== '\n' || *p
== '\r')
1797 PyObject
*pad(PyStringObject
*self
,
1809 if (left
== 0 && right
== 0) {
1811 return (PyObject
*)self
;
1814 u
= PyString_FromStringAndSize(NULL
,
1815 left
+ PyString_GET_SIZE(self
) + right
);
1818 memset(PyString_AS_STRING(u
), fill
, left
);
1819 memcpy(PyString_AS_STRING(u
) + left
,
1820 PyString_AS_STRING(self
),
1821 PyString_GET_SIZE(self
));
1823 memset(PyString_AS_STRING(u
) + left
+ PyString_GET_SIZE(self
),
1830 static char ljust__doc__
[] =
1831 "S.ljust(width) -> string\n\
1833 Return S left justified in a string of length width. Padding is\n\
1834 done using spaces.";
1837 string_ljust(PyStringObject
*self
, PyObject
*args
)
1840 if (!PyArg_ParseTuple(args
, "i:ljust", &width
))
1843 if (PyString_GET_SIZE(self
) >= width
) {
1845 return (PyObject
*) self
;
1848 return pad(self
, 0, width
- PyString_GET_SIZE(self
), ' ');
1852 static char rjust__doc__
[] =
1853 "S.rjust(width) -> string\n\
1855 Return S right justified in a string of length width. Padding is\n\
1856 done using spaces.";
1859 string_rjust(PyStringObject
*self
, PyObject
*args
)
1862 if (!PyArg_ParseTuple(args
, "i:rjust", &width
))
1865 if (PyString_GET_SIZE(self
) >= width
) {
1867 return (PyObject
*) self
;
1870 return pad(self
, width
- PyString_GET_SIZE(self
), 0, ' ');
1874 static char center__doc__
[] =
1875 "S.center(width) -> string\n\
1877 Return S centered in a string of length width. Padding is done\n\
1881 string_center(PyStringObject
*self
, PyObject
*args
)
1886 if (!PyArg_ParseTuple(args
, "i:center", &width
))
1889 if (PyString_GET_SIZE(self
) >= width
) {
1891 return (PyObject
*) self
;
1894 marg
= width
- PyString_GET_SIZE(self
);
1895 left
= marg
/ 2 + (marg
& width
& 1);
1897 return pad(self
, left
, marg
- left
, ' ');
1901 static char zfill__doc__
[] =
1902 "S.zfill(width) -> string\n\
1904 Pad a numeric string x with zeros on the left, to fill a field\n\
1905 of the specified width. The string x is never truncated.";
1908 string_zfill(PyStringObject
*self
, PyObject
*args
)
1915 if (!PyArg_ParseTuple(args
, "i:zfill", &width
))
1918 if (PyString_GET_SIZE(self
) >= width
) {
1920 return (PyObject
*) self
;
1923 fill
= width
- PyString_GET_SIZE(self
);
1925 u
= pad(self
, fill
, 0, '0');
1929 str
= PyString_AS_STRING(u
);
1930 if (str
[fill
] == '+' || str
[fill
] == '-') {
1931 /* move sign to beginning of string */
1940 static char isspace__doc__
[] =
1941 "S.isspace() -> int\n\
1943 Return 1 if there are only whitespace characters in S,\n\
1947 string_isspace(PyStringObject
*self
, PyObject
*args
)
1949 register const unsigned char *p
1950 = (unsigned char *) PyString_AS_STRING(self
);
1951 register const unsigned char *e
;
1953 if (!PyArg_NoArgs(args
))
1956 /* Shortcut for single character strings */
1957 if (PyString_GET_SIZE(self
) == 1 &&
1959 return PyInt_FromLong(1);
1961 /* Special case for empty strings */
1962 if (PyString_GET_SIZE(self
) == 0)
1963 return PyInt_FromLong(0);
1965 e
= p
+ PyString_GET_SIZE(self
);
1966 for (; p
< e
; p
++) {
1968 return PyInt_FromLong(0);
1970 return PyInt_FromLong(1);
1974 static char isalpha__doc__
[] =
1975 "S.isalpha() -> int\n\
1977 Return 1 if all characters in S are alphabetic\n\
1978 and there is at least one character in S, 0 otherwise.";
1981 string_isalpha(PyUnicodeObject
*self
, PyObject
*args
)
1983 register const unsigned char *p
1984 = (unsigned char *) PyString_AS_STRING(self
);
1985 register const unsigned char *e
;
1987 if (!PyArg_NoArgs(args
))
1990 /* Shortcut for single character strings */
1991 if (PyString_GET_SIZE(self
) == 1 &&
1993 return PyInt_FromLong(1);
1995 /* Special case for empty strings */
1996 if (PyString_GET_SIZE(self
) == 0)
1997 return PyInt_FromLong(0);
1999 e
= p
+ PyString_GET_SIZE(self
);
2000 for (; p
< e
; p
++) {
2002 return PyInt_FromLong(0);
2004 return PyInt_FromLong(1);
2008 static char isalnum__doc__
[] =
2009 "S.isalnum() -> int\n\
2011 Return 1 if all characters in S are alphanumeric\n\
2012 and there is at least one character in S, 0 otherwise.";
2015 string_isalnum(PyUnicodeObject
*self
, PyObject
*args
)
2017 register const unsigned char *p
2018 = (unsigned char *) PyString_AS_STRING(self
);
2019 register const unsigned char *e
;
2021 if (!PyArg_NoArgs(args
))
2024 /* Shortcut for single character strings */
2025 if (PyString_GET_SIZE(self
) == 1 &&
2027 return PyInt_FromLong(1);
2029 /* Special case for empty strings */
2030 if (PyString_GET_SIZE(self
) == 0)
2031 return PyInt_FromLong(0);
2033 e
= p
+ PyString_GET_SIZE(self
);
2034 for (; p
< e
; p
++) {
2036 return PyInt_FromLong(0);
2038 return PyInt_FromLong(1);
2042 static char isdigit__doc__
[] =
2043 "S.isdigit() -> int\n\
2045 Return 1 if there are only digit characters in S,\n\
2049 string_isdigit(PyStringObject
*self
, PyObject
*args
)
2051 register const unsigned char *p
2052 = (unsigned char *) PyString_AS_STRING(self
);
2053 register const unsigned char *e
;
2055 if (!PyArg_NoArgs(args
))
2058 /* Shortcut for single character strings */
2059 if (PyString_GET_SIZE(self
) == 1 &&
2061 return PyInt_FromLong(1);
2063 /* Special case for empty strings */
2064 if (PyString_GET_SIZE(self
) == 0)
2065 return PyInt_FromLong(0);
2067 e
= p
+ PyString_GET_SIZE(self
);
2068 for (; p
< e
; p
++) {
2070 return PyInt_FromLong(0);
2072 return PyInt_FromLong(1);
2076 static char islower__doc__
[] =
2077 "S.islower() -> int\n\
2079 Return 1 if all cased characters in S are lowercase and there is\n\
2080 at least one cased character in S, 0 otherwise.";
2083 string_islower(PyStringObject
*self
, PyObject
*args
)
2085 register const unsigned char *p
2086 = (unsigned char *) PyString_AS_STRING(self
);
2087 register const unsigned char *e
;
2090 if (!PyArg_NoArgs(args
))
2093 /* Shortcut for single character strings */
2094 if (PyString_GET_SIZE(self
) == 1)
2095 return PyInt_FromLong(islower(*p
) != 0);
2097 /* Special case for empty strings */
2098 if (PyString_GET_SIZE(self
) == 0)
2099 return PyInt_FromLong(0);
2101 e
= p
+ PyString_GET_SIZE(self
);
2103 for (; p
< e
; p
++) {
2105 return PyInt_FromLong(0);
2106 else if (!cased
&& islower(*p
))
2109 return PyInt_FromLong(cased
);
2113 static char isupper__doc__
[] =
2114 "S.isupper() -> int\n\
2116 Return 1 if all cased characters in S are uppercase and there is\n\
2117 at least one cased character in S, 0 otherwise.";
2120 string_isupper(PyStringObject
*self
, PyObject
*args
)
2122 register const unsigned char *p
2123 = (unsigned char *) PyString_AS_STRING(self
);
2124 register const unsigned char *e
;
2127 if (!PyArg_NoArgs(args
))
2130 /* Shortcut for single character strings */
2131 if (PyString_GET_SIZE(self
) == 1)
2132 return PyInt_FromLong(isupper(*p
) != 0);
2134 /* Special case for empty strings */
2135 if (PyString_GET_SIZE(self
) == 0)
2136 return PyInt_FromLong(0);
2138 e
= p
+ PyString_GET_SIZE(self
);
2140 for (; p
< e
; p
++) {
2142 return PyInt_FromLong(0);
2143 else if (!cased
&& isupper(*p
))
2146 return PyInt_FromLong(cased
);
2150 static char istitle__doc__
[] =
2151 "S.istitle() -> int\n\
2153 Return 1 if S is a titlecased string, i.e. uppercase characters\n\
2154 may only follow uncased characters and lowercase characters only cased\n\
2155 ones. Return 0 otherwise.";
2158 string_istitle(PyStringObject
*self
, PyObject
*args
)
2160 register const unsigned char *p
2161 = (unsigned char *) PyString_AS_STRING(self
);
2162 register const unsigned char *e
;
2163 int cased
, previous_is_cased
;
2165 if (!PyArg_NoArgs(args
))
2168 /* Shortcut for single character strings */
2169 if (PyString_GET_SIZE(self
) == 1)
2170 return PyInt_FromLong(isupper(*p
) != 0);
2172 /* Special case for empty strings */
2173 if (PyString_GET_SIZE(self
) == 0)
2174 return PyInt_FromLong(0);
2176 e
= p
+ PyString_GET_SIZE(self
);
2178 previous_is_cased
= 0;
2179 for (; p
< e
; p
++) {
2180 register const unsigned char ch
= *p
;
2183 if (previous_is_cased
)
2184 return PyInt_FromLong(0);
2185 previous_is_cased
= 1;
2188 else if (islower(ch
)) {
2189 if (!previous_is_cased
)
2190 return PyInt_FromLong(0);
2191 previous_is_cased
= 1;
2195 previous_is_cased
= 0;
2197 return PyInt_FromLong(cased
);
2201 static char splitlines__doc__
[] =
2202 "S.splitlines([keepends]]) -> list of strings\n\
2204 Return a list of the lines in S, breaking at line boundaries.\n\
2205 Line breaks are not included in the resulting list unless keepends\n\
2206 is given and true.";
2208 #define SPLIT_APPEND(data, left, right) \
2209 str = PyString_FromStringAndSize(data + left, right - left); \
2212 if (PyList_Append(list, str)) { \
2220 string_splitlines(PyStringObject
*self
, PyObject
*args
)
2230 if (!PyArg_ParseTuple(args
, "|i:splitlines", &keepends
))
2233 data
= PyString_AS_STRING(self
);
2234 len
= PyString_GET_SIZE(self
);
2236 list
= PyList_New(0);
2240 for (i
= j
= 0; i
< len
; ) {
2243 /* Find a line and append it */
2244 while (i
< len
&& data
[i
] != '\n' && data
[i
] != '\r')
2247 /* Skip the line break reading CRLF as one line break */
2250 if (data
[i
] == '\r' && i
+ 1 < len
&&
2258 SPLIT_APPEND(data
, j
, eol
);
2262 SPLIT_APPEND(data
, j
, len
);
2276 string_methods
[] = {
2277 /* Counterparts of the obsolete stropmodule functions; except
2278 string.maketrans(). */
2279 {"join", (PyCFunction
)string_join
, 1, join__doc__
},
2280 {"split", (PyCFunction
)string_split
, 1, split__doc__
},
2281 {"lower", (PyCFunction
)string_lower
, 1, lower__doc__
},
2282 {"upper", (PyCFunction
)string_upper
, 1, upper__doc__
},
2283 {"islower", (PyCFunction
)string_islower
, 0, islower__doc__
},
2284 {"isupper", (PyCFunction
)string_isupper
, 0, isupper__doc__
},
2285 {"isspace", (PyCFunction
)string_isspace
, 0, isspace__doc__
},
2286 {"isdigit", (PyCFunction
)string_isdigit
, 0, isdigit__doc__
},
2287 {"istitle", (PyCFunction
)string_istitle
, 0, istitle__doc__
},
2288 {"isalpha", (PyCFunction
)string_isalpha
, 0, isalpha__doc__
},
2289 {"isalnum", (PyCFunction
)string_isalnum
, 0, isalnum__doc__
},
2290 {"capitalize", (PyCFunction
)string_capitalize
, 1, capitalize__doc__
},
2291 {"count", (PyCFunction
)string_count
, 1, count__doc__
},
2292 {"endswith", (PyCFunction
)string_endswith
, 1, endswith__doc__
},
2293 {"find", (PyCFunction
)string_find
, 1, find__doc__
},
2294 {"index", (PyCFunction
)string_index
, 1, index__doc__
},
2295 {"lstrip", (PyCFunction
)string_lstrip
, 1, lstrip__doc__
},
2296 {"replace", (PyCFunction
)string_replace
, 1, replace__doc__
},
2297 {"rfind", (PyCFunction
)string_rfind
, 1, rfind__doc__
},
2298 {"rindex", (PyCFunction
)string_rindex
, 1, rindex__doc__
},
2299 {"rstrip", (PyCFunction
)string_rstrip
, 1, rstrip__doc__
},
2300 {"startswith", (PyCFunction
)string_startswith
, 1, startswith__doc__
},
2301 {"strip", (PyCFunction
)string_strip
, 1, strip__doc__
},
2302 {"swapcase", (PyCFunction
)string_swapcase
, 1, swapcase__doc__
},
2303 {"translate", (PyCFunction
)string_translate
, 1, translate__doc__
},
2304 {"title", (PyCFunction
)string_title
, 1, title__doc__
},
2305 {"ljust", (PyCFunction
)string_ljust
, 1, ljust__doc__
},
2306 {"rjust", (PyCFunction
)string_rjust
, 1, rjust__doc__
},
2307 {"center", (PyCFunction
)string_center
, 1, center__doc__
},
2308 {"encode", (PyCFunction
)string_encode
, 1, encode__doc__
},
2309 {"expandtabs", (PyCFunction
)string_expandtabs
, 1, expandtabs__doc__
},
2310 {"splitlines", (PyCFunction
)string_splitlines
, 1, splitlines__doc__
},
2312 {"zfill", (PyCFunction
)string_zfill
, 1, zfill__doc__
},
2314 {NULL
, NULL
} /* sentinel */
2318 string_getattr(PyStringObject
*s
, char *name
)
2320 return Py_FindMethod(string_methods
, (PyObject
*)s
, name
);
2324 PyTypeObject PyString_Type
= {
2325 PyObject_HEAD_INIT(&PyType_Type
)
2328 sizeof(PyStringObject
),
2330 (destructor
)string_dealloc
, /*tp_dealloc*/
2331 (printfunc
)string_print
, /*tp_print*/
2332 (getattrfunc
)string_getattr
, /*tp_getattr*/
2334 (cmpfunc
)string_compare
, /*tp_compare*/
2335 (reprfunc
)string_repr
, /*tp_repr*/
2337 &string_as_sequence
, /*tp_as_sequence*/
2338 0, /*tp_as_mapping*/
2339 (hashfunc
)string_hash
, /*tp_hash*/
2344 &string_as_buffer
, /*tp_as_buffer*/
2345 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
2350 PyString_Concat(register PyObject
**pv
, register PyObject
*w
)
2352 register PyObject
*v
;
2355 if (w
== NULL
|| !PyString_Check(*pv
)) {
2360 v
= string_concat((PyStringObject
*) *pv
, w
);
2366 PyString_ConcatAndDel(register PyObject
**pv
, register PyObject
*w
)
2368 PyString_Concat(pv
, w
);
2373 /* The following function breaks the notion that strings are immutable:
2374 it changes the size of a string. We get away with this only if there
2375 is only one module referencing the object. You can also think of it
2376 as creating a new string object and destroying the old one, only
2377 more efficiently. In any case, don't use this if the string may
2378 already be known to some other part of the code... */
2381 _PyString_Resize(PyObject
**pv
, int newsize
)
2383 register PyObject
*v
;
2384 register PyStringObject
*sv
;
2386 if (!PyString_Check(v
) || v
->ob_refcnt
!= 1) {
2389 PyErr_BadInternalCall();
2392 /* XXX UNREF/NEWREF interface should be more symmetrical */
2396 _Py_ForgetReference(v
);
2398 PyObject_REALLOC((char *)v
,
2399 sizeof(PyStringObject
) + newsize
* sizeof(char));
2405 _Py_NewReference(*pv
);
2406 sv
= (PyStringObject
*) *pv
;
2407 sv
->ob_size
= newsize
;
2408 sv
->ob_sval
[newsize
] = '\0';
2412 /* Helpers for formatstring */
2415 getnextarg(PyObject
*args
, int arglen
, int *p_argidx
)
2417 int argidx
= *p_argidx
;
2418 if (argidx
< arglen
) {
2423 return PyTuple_GetItem(args
, argidx
);
2425 PyErr_SetString(PyExc_TypeError
,
2426 "not enough arguments for format string");
2437 #define F_LJUST (1<<0)
2438 #define F_SIGN (1<<1)
2439 #define F_BLANK (1<<2)
2440 #define F_ALT (1<<3)
2441 #define F_ZERO (1<<4)
2444 formatfloat(char *buf
, size_t buflen
, int flags
,
2445 int prec
, int type
, PyObject
*v
)
2447 /* fmt = '%#.' + `prec` + `type`
2448 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
2451 if (!PyArg_Parse(v
, "d;float argument required", &x
))
2455 if (type
== 'f' && fabs(x
)/1e25
>= 1e25
)
2457 sprintf(fmt
, "%%%s.%d%c", (flags
&F_ALT
) ? "#" : "", prec
, type
);
2458 /* worst case length calc to ensure no buffer overrun:
2460 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
2461 for any double rep.)
2462 len = 1 + prec + 1 + 2 + 5 = 9 + prec
2463 If prec=0 the effective precision is 1 (the leading digit is
2464 always given), therefore increase by one to 10+prec. */
2465 if (buflen
<= (size_t)10 + (size_t)prec
) {
2466 PyErr_SetString(PyExc_OverflowError
,
2467 "formatted float is too long (precision too long?)");
2470 sprintf(buf
, fmt
, x
);
2474 /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
2475 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
2476 * Python's regular ints.
2477 * Return value: a new PyString*, or NULL if error.
2478 * . *pbuf is set to point into it,
2479 * *plen set to the # of chars following that.
2480 * Caller must decref it when done using pbuf.
2481 * The string starting at *pbuf is of the form
2482 * "-"? ("0x" | "0X")? digit+
2483 * "0x"/"0X" are present only for x and X conversions, with F_ALT
2484 * set in flags. The case of hex digits will be correct,
2485 * There will be at least prec digits, zero-filled on the left if
2486 * necessary to get that many.
2487 * val object to be converted
2488 * flags bitmask of format flags; only F_ALT is looked at
2489 * prec minimum number of digits; 0-fill on left if needed
2490 * type a character in [duoxX]; u acts the same as d
2492 * CAUTION: o, x and X conversions on regular ints can never
2493 * produce a '-' sign, but can for Python's unbounded ints.
2496 _PyString_FormatLong(PyObject
*val
, int flags
, int prec
, int type
,
2497 char **pbuf
, int *plen
)
2499 PyObject
*result
= NULL
;
2502 int sign
; /* 1 if '-', else 0 */
2503 int len
; /* number of characters */
2504 int numdigits
; /* len == numnondigits + numdigits */
2505 int numnondigits
= 0;
2510 result
= val
->ob_type
->tp_str(val
);
2513 result
= val
->ob_type
->tp_as_number
->nb_oct(val
);
2518 result
= val
->ob_type
->tp_as_number
->nb_hex(val
);
2521 assert(!"'type' not in [duoxX]");
2526 /* To modify the string in-place, there can only be one reference. */
2527 if (result
->ob_refcnt
!= 1) {
2528 PyErr_BadInternalCall();
2531 buf
= PyString_AsString(result
);
2532 len
= PyString_Size(result
);
2533 if (buf
[len
-1] == 'L') {
2537 sign
= buf
[0] == '-';
2538 numnondigits
+= sign
;
2539 numdigits
= len
- numnondigits
;
2540 assert(numdigits
> 0);
2542 /* Get rid of base marker unless F_ALT */
2543 if ((flags
& F_ALT
) == 0) {
2544 /* Need to skip 0x, 0X or 0. */
2548 assert(buf
[sign
] == '0');
2549 /* If 0 is only digit, leave it alone. */
2550 if (numdigits
> 1) {
2557 assert(buf
[sign
] == '0');
2558 assert(buf
[sign
+ 1] == 'x');
2569 assert(len
== numnondigits
+ numdigits
);
2570 assert(numdigits
> 0);
2573 /* Fill with leading zeroes to meet minimum width. */
2574 if (prec
> numdigits
) {
2575 PyObject
*r1
= PyString_FromStringAndSize(NULL
,
2576 numnondigits
+ prec
);
2582 b1
= PyString_AS_STRING(r1
);
2583 for (i
= 0; i
< numnondigits
; ++i
)
2585 for (i
= 0; i
< prec
- numdigits
; i
++)
2587 for (i
= 0; i
< numdigits
; i
++)
2592 buf
= PyString_AS_STRING(result
);
2593 len
= numnondigits
+ prec
;
2596 /* Fix up case for hex conversions. */
2599 /* Need to convert all upper case letters to lower case. */
2600 for (i
= 0; i
< len
; i
++)
2601 if (buf
[i
] >= 'A' && buf
[i
] <= 'F')
2605 /* Need to convert 0x to 0X (and -0x to -0X). */
2606 if (buf
[sign
+ 1] == 'x')
2607 buf
[sign
+ 1] = 'X';
2616 formatint(char *buf
, size_t buflen
, int flags
,
2617 int prec
, int type
, PyObject
*v
)
2619 /* fmt = '%#.' + `prec` + 'l' + `type`
2620 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
2622 char fmt
[64]; /* plenty big enough! */
2624 if (!PyArg_Parse(v
, "l;int argument required", &x
))
2628 sprintf(fmt
, "%%%s.%dl%c", (flags
&F_ALT
) ? "#" : "", prec
, type
);
2629 /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal))
2630 worst case buf = '0x' + [0-9]*prec, where prec >= 11 */
2631 if (buflen
<= 13 || buflen
<= (size_t)2 + (size_t)prec
) {
2632 PyErr_SetString(PyExc_OverflowError
,
2633 "formatted integer is too long (precision too long?)");
2636 sprintf(buf
, fmt
, x
);
2641 formatchar(char *buf
, size_t buflen
, PyObject
*v
)
2643 /* presume that the buffer is at least 2 characters long */
2644 if (PyString_Check(v
)) {
2645 if (!PyArg_Parse(v
, "c;%c requires int or char", &buf
[0]))
2649 if (!PyArg_Parse(v
, "b;%c requires int or char", &buf
[0]))
2657 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
2659 FORMATBUFLEN is the length of the buffer in which the floats, ints, &
2660 chars are formatted. XXX This is a magic number. Each formatting
2661 routine does bounds checking to ensure no overflow, but a better
2662 solution may be to malloc a buffer of appropriate size for each
2663 format. For now, the current solution is sufficient.
2665 #define FORMATBUFLEN (size_t)120
2668 PyString_Format(PyObject
*format
, PyObject
*args
)
2671 int fmtcnt
, rescnt
, reslen
, arglen
, argidx
;
2673 PyObject
*result
, *orig_args
;
2674 PyObject
*dict
= NULL
;
2675 if (format
== NULL
|| !PyString_Check(format
) || args
== NULL
) {
2676 PyErr_BadInternalCall();
2680 fmt
= PyString_AsString(format
);
2681 fmtcnt
= PyString_Size(format
);
2682 reslen
= rescnt
= fmtcnt
+ 100;
2683 result
= PyString_FromStringAndSize((char *)NULL
, reslen
);
2686 res
= PyString_AsString(result
);
2687 if (PyTuple_Check(args
)) {
2688 arglen
= PyTuple_Size(args
);
2695 if (args
->ob_type
->tp_as_mapping
)
2697 while (--fmtcnt
>= 0) {
2700 rescnt
= fmtcnt
+ 100;
2702 if (_PyString_Resize(&result
, reslen
) < 0)
2704 res
= PyString_AsString(result
)
2711 /* Got a format specifier */
2719 PyObject
*temp
= NULL
;
2723 char formatbuf
[FORMATBUFLEN
]; /* For format{float,int,char}() */
2724 char *fmt_start
= fmt
;
2734 PyErr_SetString(PyExc_TypeError
,
2735 "format requires a mapping");
2741 /* Skip over balanced parentheses */
2742 while (pcount
> 0 && --fmtcnt
>= 0) {
2745 else if (*fmt
== '(')
2749 keylen
= fmt
- keystart
- 1;
2750 if (fmtcnt
< 0 || pcount
> 0) {
2751 PyErr_SetString(PyExc_ValueError
,
2752 "incomplete format key");
2755 key
= PyString_FromStringAndSize(keystart
,
2763 args
= PyObject_GetItem(dict
, key
);
2772 while (--fmtcnt
>= 0) {
2773 switch (c
= *fmt
++) {
2774 case '-': flags
|= F_LJUST
; continue;
2775 case '+': flags
|= F_SIGN
; continue;
2776 case ' ': flags
|= F_BLANK
; continue;
2777 case '#': flags
|= F_ALT
; continue;
2778 case '0': flags
|= F_ZERO
; continue;
2783 v
= getnextarg(args
, arglen
, &argidx
);
2786 if (!PyInt_Check(v
)) {
2787 PyErr_SetString(PyExc_TypeError
,
2791 width
= PyInt_AsLong(v
);
2799 else if (c
>= 0 && isdigit(c
)) {
2801 while (--fmtcnt
>= 0) {
2802 c
= Py_CHARMASK(*fmt
++);
2805 if ((width
*10) / 10 != width
) {
2811 width
= width
*10 + (c
- '0');
2819 v
= getnextarg(args
, arglen
, &argidx
);
2822 if (!PyInt_Check(v
)) {
2828 prec
= PyInt_AsLong(v
);
2834 else if (c
>= 0 && isdigit(c
)) {
2836 while (--fmtcnt
>= 0) {
2837 c
= Py_CHARMASK(*fmt
++);
2840 if ((prec
*10) / 10 != prec
) {
2846 prec
= prec
*10 + (c
- '0');
2851 if (c
== 'h' || c
== 'l' || c
== 'L') {
2858 PyErr_SetString(PyExc_ValueError
,
2859 "incomplete format");
2863 v
= getnextarg(args
, arglen
, &argidx
);
2876 if (PyUnicode_Check(v
)) {
2881 temp
= PyObject_Str(v
);
2883 temp
= PyObject_Repr(v
);
2886 if (!PyString_Check(temp
)) {
2887 PyErr_SetString(PyExc_TypeError
,
2888 "%s argument has non-string str()");
2891 pbuf
= PyString_AsString(temp
);
2892 len
= PyString_Size(temp
);
2893 if (prec
>= 0 && len
> prec
)
2904 if (PyLong_Check(v
) && PyLong_AsLong(v
) == -1
2905 && PyErr_Occurred()) {
2906 /* Too big for a C long. */
2908 temp
= _PyString_FormatLong(v
, flags
,
2909 prec
, c
, &pbuf
, &len
);
2912 /* unbounded ints can always produce
2913 a sign character! */
2918 len
= formatint(pbuf
, sizeof(formatbuf
),
2922 /* only d conversion is signed */
2934 len
= formatfloat(pbuf
, sizeof(formatbuf
), flags
, prec
, c
, v
);
2943 len
= formatchar(pbuf
, sizeof(formatbuf
), v
);
2948 PyErr_Format(PyExc_ValueError
,
2949 "unsupported format character '%c' (0x%x)",
2954 if (*pbuf
== '-' || *pbuf
== '+') {
2958 else if (flags
& F_SIGN
)
2960 else if (flags
& F_BLANK
)
2967 if (rescnt
< width
+ (sign
!= 0)) {
2969 rescnt
= width
+ fmtcnt
+ 100;
2971 if (_PyString_Resize(&result
, reslen
) < 0)
2973 res
= PyString_AsString(result
)
2983 if ((flags
& F_ALT
) && (c
== 'x' || c
== 'X')) {
2984 assert(pbuf
[0] == '0');
2985 assert(pbuf
[1] == c
);
2996 if (width
> len
&& !(flags
& F_LJUST
)) {
3000 } while (--width
> len
);
3005 if ((flags
& F_ALT
) &&
3006 (c
== 'x' || c
== 'X')) {
3007 assert(pbuf
[0] == '0');
3008 assert(pbuf
[1] == c
);
3013 memcpy(res
, pbuf
, len
);
3016 while (--width
>= len
) {
3020 if (dict
&& (argidx
< arglen
) && c
!= '%') {
3021 PyErr_SetString(PyExc_TypeError
,
3022 "not all arguments converted");
3028 if (argidx
< arglen
&& !dict
) {
3029 PyErr_SetString(PyExc_TypeError
,
3030 "not all arguments converted");
3036 _PyString_Resize(&result
, reslen
- rescnt
);
3044 /* Fiddle args right (remove the first argidx-1 arguments) */
3046 if (PyTuple_Check(orig_args
) && argidx
> 0) {
3048 int n
= PyTuple_GET_SIZE(orig_args
) - argidx
;
3053 PyObject
*w
= PyTuple_GET_ITEM(orig_args
, n
+ argidx
);
3055 PyTuple_SET_ITEM(v
, n
, w
);
3059 Py_INCREF(orig_args
);
3062 /* Paste rest of format string to what we have of the result
3063 string; we reuse result for this */
3064 rescnt
= res
- PyString_AS_STRING(result
);
3065 fmtcnt
= PyString_GET_SIZE(format
) - \
3066 (fmt
- PyString_AS_STRING(format
));
3067 if (_PyString_Resize(&result
, rescnt
+ fmtcnt
)) {
3071 memcpy(PyString_AS_STRING(result
) + rescnt
, fmt
, fmtcnt
);
3073 /* Let Unicode do its magic */
3074 result
= PyUnicode_Format(format
, args
);
3088 #ifdef INTERN_STRINGS
3090 /* This dictionary will leak at PyString_Fini() time. That's acceptable
3091 * because PyString_Fini() specifically frees interned strings that are
3092 * only referenced by this dictionary. The CVS log entry for revision 2.45
3095 * Change the Fini function to only remove otherwise unreferenced
3096 * strings from the interned table. There are references in
3097 * hard-to-find static variables all over the interpreter, and it's not
3098 * worth trying to get rid of all those; but "uninterning" isn't fair
3099 * either and may cause subtle failures later -- so we have to keep them
3100 * in the interned table.
3102 static PyObject
*interned
;
3105 PyString_InternInPlace(PyObject
**p
)
3107 register PyStringObject
*s
= (PyStringObject
*)(*p
);
3109 if (s
== NULL
|| !PyString_Check(s
))
3110 Py_FatalError("PyString_InternInPlace: strings only please!");
3111 if ((t
= s
->ob_sinterned
) != NULL
) {
3112 if (t
== (PyObject
*)s
)
3119 if (interned
== NULL
) {
3120 interned
= PyDict_New();
3121 if (interned
== NULL
)
3124 if ((t
= PyDict_GetItem(interned
, (PyObject
*)s
)) != NULL
) {
3126 *p
= s
->ob_sinterned
= t
;
3131 if (PyDict_SetItem(interned
, t
, t
) == 0) {
3132 s
->ob_sinterned
= t
;
3140 PyString_InternFromString(const char *cp
)
3142 PyObject
*s
= PyString_FromString(cp
);
3145 PyString_InternInPlace(&s
);
3155 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++) {
3156 Py_XDECREF(characters
[i
]);
3157 characters
[i
] = NULL
;
3159 #ifndef DONT_SHARE_SHORT_STRINGS
3160 Py_XDECREF(nullstring
);
3163 #ifdef INTERN_STRINGS
3166 PyObject
*key
, *value
;
3170 while (PyDict_Next(interned
, &pos
, &key
, &value
)) {
3171 if (key
->ob_refcnt
== 2 && key
== value
) {
3172 PyDict_DelItem(interned
, key
);