History

add hg and python

cinap_lenrek committed 15 years ago

Commit 4bb9e221 parent 987265c3706 files · +1228772 −-233Browse files

Showing the changes to sys/src/cmd/python/Objects/stringobject.c (1 of 3706 files). Show all

Unified · Split

sys/src/cmd/python/Objects/stringobject.c+5009−0
@@ -0,0 +1,5009 @@
1/* String object implementation */
2
3#define PY_SSIZE_T_CLEAN
4
5#include "Python.h"
6
7#include <ctype.h>
8
9#ifdef COUNT_ALLOCS
10int null_strings, one_strings;
11#endif
12
13static PyStringObject *characters[UCHAR_MAX + 1];
14static PyStringObject *nullstring;
15
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.
20
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)
23*/
24static PyObject *interned;
25
26/*
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.
30
31 For PyString_FromString(), the parameter `str' points to a null-terminated
32 string containing exactly `size' bytes.
33
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.
44
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()).
51*/
52PyObject *
53PyString_FromStringAndSize(const char *str, Py_ssize_t size)
54{
55 register PyStringObject *op;
56 assert(size >= 0);
57 if (size == 0 && (op = nullstring) != NULL) {
58#ifdef COUNT_ALLOCS
59 null_strings++;
60#endif
61 Py_INCREF(op);
62 return (PyObject *)op;
63 }
64 if (size == 1 && str != NULL &&
65 (op = characters[*str & UCHAR_MAX]) != NULL)
66 {
67#ifdef COUNT_ALLOCS
68 one_strings++;
69#endif
70 Py_INCREF(op);
71 return (PyObject *)op;
72 }
73
74 /* Inline PyObject_NewVar */
75 op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
76 if (op == NULL)
77 return PyErr_NoMemory();
78 PyObject_INIT_VAR(op, &PyString_Type, size);
79 op->ob_shash = -1;
80 op->ob_sstate = SSTATE_NOT_INTERNED;
81 if (str != NULL)
82 Py_MEMCPY(op->ob_sval, str, size);
83 op->ob_sval[size] = '\0';
84 /* share short strings */
85 if (size == 0) {
86 PyObject *t = (PyObject *)op;
87 PyString_InternInPlace(&t);
88 op = (PyStringObject *)t;
89 nullstring = op;
90 Py_INCREF(op);
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;
96 Py_INCREF(op);
97 }
98 return (PyObject *) op;
99}
100
101PyObject *
102PyString_FromString(const char *str)
103{
104 register size_t size;
105 register PyStringObject *op;
106
107 assert(str != NULL);
108 size = strlen(str);
109 if (size > PY_SSIZE_T_MAX) {
110 PyErr_SetString(PyExc_OverflowError,
111 "string is too long for a Python string");
112 return NULL;
113 }
114 if (size == 0 && (op = nullstring) != NULL) {
115#ifdef COUNT_ALLOCS
116 null_strings++;
117#endif
118 Py_INCREF(op);
119 return (PyObject *)op;
120 }
121 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
122#ifdef COUNT_ALLOCS
123 one_strings++;
124#endif
125 Py_INCREF(op);
126 return (PyObject *)op;
127 }
128
129 /* Inline PyObject_NewVar */
130 op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
131 if (op == NULL)
132 return PyErr_NoMemory();
133 PyObject_INIT_VAR(op, &PyString_Type, size);
134 op->ob_shash = -1;
135 op->ob_sstate = SSTATE_NOT_INTERNED;
136 Py_MEMCPY(op->ob_sval, str, size+1);
137 /* share short strings */
138 if (size == 0) {
139 PyObject *t = (PyObject *)op;
140 PyString_InternInPlace(&t);
141 op = (PyStringObject *)t;
142 nullstring = op;
143 Py_INCREF(op);
144 } else if (size == 1) {
145 PyObject *t = (PyObject *)op;
146 PyString_InternInPlace(&t);
147 op = (PyStringObject *)t;
148 characters[*str & UCHAR_MAX] = op;
149 Py_INCREF(op);
150 }
151 return (PyObject *) op;
152}
153
154PyObject *
155PyString_FromFormatV(const char *format, va_list vargs)
156{
157 va_list count;
158 Py_ssize_t n = 0;
159 const char* f;
160 char *s;
161 PyObject* string;
162
163#ifdef VA_LIST_IS_ARRAY
164 Py_MEMCPY(count, vargs, sizeof(va_list));
165#else
166#ifdef __va_copy
167 __va_copy(count, vargs);
168#else
169 count = vargs;
170#endif
171#endif
172 /* step 1: figure out how large a buffer we need */
173 for (f = format; *f; f++) {
174 if (*f == '%') {
175 const char* p = f;
176 while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
177 ;
178
179 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since
180 * they don't affect the amount of space we reserve.
181 */
182 if ((*f == 'l' || *f == 'z') &&
183 (f[1] == 'd' || f[1] == 'u'))
184 ++f;
185
186 switch (*f) {
187 case 'c':
188 (void)va_arg(count, int);
189 /* fall through... */
190 case '%':
191 n++;
192 break;
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. */
198 n += 20;
199 break;
200 case 's':
201 s = va_arg(count, char*);
202 n += strlen(s);
203 break;
204 case 'p':
205 (void) va_arg(count, int);
206 /* maximum 64-bit pointer representation:
207 * 0xffffffffffffffff
208 * so 19 characters is enough.
209 * XXX I count 18 -- what's the extra for?
210 */
211 n += 19;
212 break;
213 default:
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) */
220 n += strlen(p);
221 goto expand;
222 }
223 } else
224 n++;
225 }
226 expand:
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);
231 if (!string)
232 return NULL;
233
234 s = PyString_AsString(string);
235
236 for (f = format; *f; f++) {
237 if (*f == '%') {
238 const char* p = f++;
239 Py_ssize_t i;
240 int longflag = 0;
241 int size_tflag = 0;
242 /* parse the width.precision part (we're only
243 interested in the precision value, if any) */
244 n = 0;
245 while (isdigit(Py_CHARMASK(*f)))
246 n = (n*10) + *f++ - '0';
247 if (*f == '.') {
248 f++;
249 n = 0;
250 while (isdigit(Py_CHARMASK(*f)))
251 n = (n*10) + *f++ - '0';
252 }
253 while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f)))
254 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')) {
258 longflag = 1;
259 ++f;
260 }
261 /* handle the size_t flag. */
262 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
263 size_tflag = 1;
264 ++f;
265 }
266
267 switch (*f) {
268 case 'c':
269 *s++ = va_arg(vargs, int);
270 break;
271 case 'd':
272 if (longflag)
273 sprintf(s, "%ld", va_arg(vargs, long));
274 else if (size_tflag)
275 sprintf(s, "%" PY_FORMAT_SIZE_T "d",
276 va_arg(vargs, Py_ssize_t));
277 else
278 sprintf(s, "%d", va_arg(vargs, int));
279 s += strlen(s);
280 break;
281 case 'u':
282 if (longflag)
283 sprintf(s, "%lu",
284 va_arg(vargs, unsigned long));
285 else if (size_tflag)
286 sprintf(s, "%" PY_FORMAT_SIZE_T "u",
287 va_arg(vargs, size_t));
288 else
289 sprintf(s, "%u",
290 va_arg(vargs, unsigned int));
291 s += strlen(s);
292 break;
293 case 'i':
294 sprintf(s, "%i", va_arg(vargs, int));
295 s += strlen(s);
296 break;
297 case 'x':
298 sprintf(s, "%x", va_arg(vargs, int));
299 s += strlen(s);
300 break;
301 case 's':
302 p = va_arg(vargs, char*);
303 i = strlen(p);
304 if (n > 0 && i > n)
305 i = n;
306 Py_MEMCPY(s, p, i);
307 s += i;
308 break;
309 case 'p':
310 sprintf(s, "%p", va_arg(vargs, void*));
311 /* %p is ill-defined: ensure leading 0x. */
312 if (s[1] == 'X')
313 s[1] = 'x';
314 else if (s[1] != 'x') {
315 memmove(s+2, s, strlen(s)+1);
316 s[0] = '0';
317 s[1] = 'x';
318 }
319 s += strlen(s);
320 break;
321 case '%':
322 *s++ = '%';
323 break;
324 default:
325 strcpy(s, p);
326 s += strlen(s);
327 goto end;
328 }
329 } else
330 *s++ = *f;
331 }
332
333 end:
334 _PyString_Resize(&string, s - PyString_AS_STRING(string));
335 return string;
336}
337
338PyObject *
339PyString_FromFormat(const char *format, ...)
340{
341 PyObject* ret;
342 va_list vargs;
343
344#ifdef HAVE_STDARG_PROTOTYPES
345 va_start(vargs, format);
346#else
347 va_start(vargs);
348#endif
349 ret = PyString_FromFormatV(format, vargs);
350 va_end(vargs);
351 return ret;
352}
353
354
355PyObject *PyString_Decode(const char *s,
356 Py_ssize_t size,
357 const char *encoding,
358 const char *errors)
359{
360 PyObject *v, *str;
361
362 str = PyString_FromStringAndSize(s, size);
363 if (str == NULL)
364 return NULL;
365 v = PyString_AsDecodedString(str, encoding, errors);
366 Py_DECREF(str);
367 return v;
368}
369
370PyObject *PyString_AsDecodedObject(PyObject *str,
371 const char *encoding,
372 const char *errors)
373{
374 PyObject *v;
375
376 if (!PyString_Check(str)) {
377 PyErr_BadArgument();
378 goto onError;
379 }
380
381 if (encoding == NULL) {
382#ifdef Py_USING_UNICODE
383 encoding = PyUnicode_GetDefaultEncoding();
384#else
385 PyErr_SetString(PyExc_ValueError, "no encoding specified");
386 goto onError;
387#endif
388 }
389
390 /* Decode via the codec registry */
391 v = PyCodec_Decode(str, encoding, errors);
392 if (v == NULL)
393 goto onError;
394
395 return v;
396
397 onError:
398 return NULL;
399}
400
401PyObject *PyString_AsDecodedString(PyObject *str,
402 const char *encoding,
403 const char *errors)
404{
405 PyObject *v;
406
407 v = PyString_AsDecodedObject(str, encoding, errors);
408 if (v == NULL)
409 goto onError;
410
411#ifdef Py_USING_UNICODE
412 /* Convert Unicode to a string using the default encoding */
413 if (PyUnicode_Check(v)) {
414 PyObject *temp = v;
415 v = PyUnicode_AsEncodedString(v, NULL, NULL);
416 Py_DECREF(temp);
417 if (v == NULL)
418 goto onError;
419 }
420#endif
421 if (!PyString_Check(v)) {
422 PyErr_Format(PyExc_TypeError,
423 "decoder did not return a string object (type=%.400s)",
424 v->ob_type->tp_name);
425 Py_DECREF(v);
426 goto onError;
427 }
428
429 return v;
430
431 onError:
432 return NULL;
433}
434
435PyObject *PyString_Encode(const char *s,
436 Py_ssize_t size,
437 const char *encoding,
438 const char *errors)
439{
440 PyObject *v, *str;
441
442 str = PyString_FromStringAndSize(s, size);
443 if (str == NULL)
444 return NULL;
445 v = PyString_AsEncodedString(str, encoding, errors);
446 Py_DECREF(str);
447 return v;
448}
449
450PyObject *PyString_AsEncodedObject(PyObject *str,
451 const char *encoding,
452 const char *errors)
453{
454 PyObject *v;
455
456 if (!PyString_Check(str)) {
457 PyErr_BadArgument();
458 goto onError;
459 }
460
461 if (encoding == NULL) {
462#ifdef Py_USING_UNICODE
463 encoding = PyUnicode_GetDefaultEncoding();
464#else
465 PyErr_SetString(PyExc_ValueError, "no encoding specified");
466 goto onError;
467#endif
468 }
469
470 /* Encode via the codec registry */
471 v = PyCodec_Encode(str, encoding, errors);
472 if (v == NULL)
473 goto onError;
474
475 return v;
476
477 onError:
478 return NULL;
479}
480
481PyObject *PyString_AsEncodedString(PyObject *str,
482 const char *encoding,
483 const char *errors)
484{
485 PyObject *v;
486
487 v = PyString_AsEncodedObject(str, encoding, errors);
488 if (v == NULL)
489 goto onError;
490
491#ifdef Py_USING_UNICODE
492 /* Convert Unicode to a string using the default encoding */
493 if (PyUnicode_Check(v)) {
494 PyObject *temp = v;
495 v = PyUnicode_AsEncodedString(v, NULL, NULL);
496 Py_DECREF(temp);
497 if (v == NULL)
498 goto onError;
499 }
500#endif
501 if (!PyString_Check(v)) {
502 PyErr_Format(PyExc_TypeError,
503 "encoder did not return a string object (type=%.400s)",
504 v->ob_type->tp_name);
505 Py_DECREF(v);
506 goto onError;
507 }
508
509 return v;
510
511 onError:
512 return NULL;
513}
514
515static void
516string_dealloc(PyObject *op)
517{
518 switch (PyString_CHECK_INTERNED(op)) {
519 case SSTATE_NOT_INTERNED:
520 break;
521
522 case SSTATE_INTERNED_MORTAL:
523 /* revive dead object temporarily for DelItem */
524 op->ob_refcnt = 3;
525 if (PyDict_DelItem(interned, op) != 0)
526 Py_FatalError(
527 "deletion of interned string failed");
528 break;
529
530 case SSTATE_INTERNED_IMMORTAL:
531 Py_FatalError("Immortal interned string died.");
532
533 default:
534 Py_FatalError("Inconsistent interned string state.");
535 }
536 op->ob_type->tp_free(op);
537}
538
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. */
543
544PyObject *PyString_DecodeEscape(const char *s,
545 Py_ssize_t len,
546 const char *errors,
547 Py_ssize_t unicode,
548 const char *recode_encoding)
549{
550 int c;
551 char *p, *buf;
552 const char *end;
553 PyObject *v;
554 Py_ssize_t newlen = recode_encoding ? 4*len:len;
555 v = PyString_FromStringAndSize((char *)NULL, newlen);
556 if (v == NULL)
557 return NULL;
558 p = buf = PyString_AsString(v);
559 end = s + len;
560 while (s < end) {
561 if (*s != '\\') {
562 non_esc:
563#ifdef Py_USING_UNICODE
564 if (recode_encoding && (*s & 0x80)) {
565 PyObject *u, *w;
566 char *r;
567 const char* t;
568 Py_ssize_t rn;
569 t = s;
570 /* Decode non-ASCII bytes as UTF-8. */
571 while (t < end && (*t & 0x80)) t++;
572 u = PyUnicode_DecodeUTF8(s, t - s, errors);
573 if(!u) goto failed;
574
575 /* Recode them in target encoding. */
576 w = PyUnicode_AsEncodedString(
577 u, recode_encoding, errors);
578 Py_DECREF(u);
579 if (!w) goto failed;
580
581 /* Append bytes to output buffer. */
582 assert(PyString_Check(w));
583 r = PyString_AS_STRING(w);
584 rn = PyString_GET_SIZE(w);
585 Py_MEMCPY(p, r, rn);
586 p += rn;
587 Py_DECREF(w);
588 s = t;
589 } else {
590 *p++ = *s++;
591 }
592#else
593 *p++ = *s++;
594#endif
595 continue;
596 }
597 s++;
598 if (s==end) {
599 PyErr_SetString(PyExc_ValueError,
600 "Trailing \\ in string");
601 goto failed;
602 }
603 switch (*s++) {
604 /* XXX This assumes ASCII! */
605 case '\n': break;
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':
618 c = s[-1] - '0';
619 if ('0' <= *s && *s <= '7') {
620 c = (c<<3) + *s++ - '0';
621 if ('0' <= *s && *s <= '7')
622 c = (c<<3) + *s++ - '0';
623 }
624 *p++ = c;
625 break;
626 case 'x':
627 if (isxdigit(Py_CHARMASK(s[0]))
628 && isxdigit(Py_CHARMASK(s[1]))) {
629 unsigned int x = 0;
630 c = Py_CHARMASK(*s);
631 s++;
632 if (isdigit(c))
633 x = c - '0';
634 else if (islower(c))
635 x = 10 + c - 'a';
636 else
637 x = 10 + c - 'A';
638 x = x << 4;
639 c = Py_CHARMASK(*s);
640 s++;
641 if (isdigit(c))
642 x += c - '0';
643 else if (islower(c))
644 x += 10 + c - 'a';
645 else
646 x += 10 + c - 'A';
647 *p++ = x;
648 break;
649 }
650 if (!errors || strcmp(errors, "strict") == 0) {
651 PyErr_SetString(PyExc_ValueError,
652 "invalid \\x escape");
653 goto failed;
654 }
655 if (strcmp(errors, "replace") == 0) {
656 *p++ = '?';
657 } else if (strcmp(errors, "ignore") == 0)
658 /* do nothing */;
659 else {
660 PyErr_Format(PyExc_ValueError,
661 "decoding error; "
662 "unknown error handling code: %.400s",
663 errors);
664 goto failed;
665 }
666#ifndef Py_USING_UNICODE
667 case 'u':
668 case 'U':
669 case 'N':
670 if (unicode) {
671 PyErr_SetString(PyExc_ValueError,
672 "Unicode escapes not legal "
673 "when Unicode disabled");
674 goto failed;
675 }
676#endif
677 default:
678 *p++ = '\\';
679 s--;
680 goto non_esc; /* an arbitry number of unescaped
681 UTF-8 bytes may follow. */
682 }
683 }
684 if (p-buf < newlen)
685 _PyString_Resize(&v, p - buf);
686 return v;
687 failed:
688 Py_DECREF(v);
689 return NULL;
690}
691
692/* -------------------------------------------------------------------- */
693/* object api */
694
695static Py_ssize_t
696string_getsize(register PyObject *op)
697{
698 char *s;
699 Py_ssize_t len;
700 if (PyString_AsStringAndSize(op, &s, &len))
701 return -1;
702 return len;
703}
704
705static /*const*/ char *
706string_getbuffer(register PyObject *op)
707{
708 char *s;
709 Py_ssize_t len;
710 if (PyString_AsStringAndSize(op, &s, &len))
711 return NULL;
712 return s;
713}
714
715Py_ssize_t
716PyString_Size(register PyObject *op)
717{
718 if (!PyString_Check(op))
719 return string_getsize(op);
720 return ((PyStringObject *)op) -> ob_size;
721}
722
723/*const*/ char *
724PyString_AsString(register PyObject *op)
725{
726 if (!PyString_Check(op))
727 return string_getbuffer(op);
728 return ((PyStringObject *)op) -> ob_sval;
729}
730
731int
732PyString_AsStringAndSize(register PyObject *obj,
733 register char **s,
734 register Py_ssize_t *len)
735{
736 if (s == NULL) {
737 PyErr_BadInternalCall();
738 return -1;
739 }
740
741 if (!PyString_Check(obj)) {
742#ifdef Py_USING_UNICODE
743 if (PyUnicode_Check(obj)) {
744 obj = _PyUnicode_AsDefaultEncodedString(obj, NULL);
745 if (obj == NULL)
746 return -1;
747 }
748 else
749#endif
750 {
751 PyErr_Format(PyExc_TypeError,
752 "expected string or Unicode object, "
753 "%.200s found", obj->ob_type->tp_name);
754 return -1;
755 }
756 }
757
758 *s = PyString_AS_STRING(obj);
759 if (len != NULL)
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");
764 return -1;
765 }
766 return 0;
767}
768
769/* -------------------------------------------------------------------- */
770/* Methods */
771
772#define STRINGLIB_CHAR char
773
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
778
779#define STRINGLIB_EMPTY nullstring
780
781#include "stringlib/fastsearch.h"
782
783#include "stringlib/count.h"
784#include "stringlib/find.h"
785#include "stringlib/partition.h"
786
787
788static int
789string_print(PyStringObject *op, FILE *fp, int flags)
790{
791 Py_ssize_t i;
792 char c;
793 int quote;
794
795 /* XXX Ought to check for interrupts when writing long strings */
796 if (! PyString_CheckExact(op)) {
797 int ret;
798 /* A str subclass may have its own __str__ method. */
799 op = (PyStringObject *) PyObject_Str((PyObject *)op);
800 if (op == NULL)
801 return -1;
802 ret = string_print(op, fp, flags);
803 Py_DECREF(op);
804 return ret;
805 }
806 if (flags & Py_PRINT_RAW) {
807 char *data = op->ob_sval;
808 Py_ssize_t size = op->ob_size;
809 while (size > INT_MAX) {
810 /* Very long strings cannot be written atomically.
811 * But don't write exactly INT_MAX bytes at a time
812 * to avoid memory aligment issues.
813 */
814 const int chunk_size = INT_MAX & ~0x3FFF;
815 fwrite(data, 1, chunk_size, fp);
816 data += chunk_size;
817 size -= chunk_size;
818 }
819#ifdef __VMS
820 if (size) fwrite(data, (int)size, 1, fp);
821#else
822 fwrite(data, 1, (int)size, fp);
823#endif
824 return 0;
825 }
826
827 /* figure out which quote to use; single is preferred */
828 quote = '\'';
829 if (memchr(op->ob_sval, '\'', op->ob_size) &&
830 !memchr(op->ob_sval, '"', op->ob_size))
831 quote = '"';
832
833 fputc(quote, fp);
834 for (i = 0; i < op->ob_size; i++) {
835 c = op->ob_sval[i];
836 if (c == quote || c == '\\')
837 fprintf(fp, "\\%c", c);
838 else if (c == '\t')
839 fprintf(fp, "\\t");
840 else if (c == '\n')
841 fprintf(fp, "\\n");
842 else if (c == '\r')
843 fprintf(fp, "\\r");
844 else if (c < ' ' || c >= 0x7f)
845 fprintf(fp, "\\x%02x", c & 0xff);
846 else
847 fputc(c, fp);
848 }
849 fputc(quote, fp);
850 return 0;
851}
852
853PyObject *
854PyString_Repr(PyObject *obj, int smartquotes)
855{
856 register PyStringObject* op = (PyStringObject*) obj;
857 size_t newsize = 2 + 4 * op->ob_size;
858 PyObject *v;
859 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != op->ob_size) {
860 PyErr_SetString(PyExc_OverflowError,
861 "string is too large to make repr");
862 }
863 v = PyString_FromStringAndSize((char *)NULL, newsize);
864 if (v == NULL) {
865 return NULL;
866 }
867 else {
868 register Py_ssize_t i;
869 register char c;
870 register char *p;
871 int quote;
872
873 /* figure out which quote to use; single is preferred */
874 quote = '\'';
875 if (smartquotes &&
876 memchr(op->ob_sval, '\'', op->ob_size) &&
877 !memchr(op->ob_sval, '"', op->ob_size))
878 quote = '"';
879
880 p = PyString_AS_STRING(v);
881 *p++ = quote;
882 for (i = 0; i < op->ob_size; i++) {
883 /* There's at least enough room for a hex escape
884 and a closing quote. */
885 assert(newsize - (p - PyString_AS_STRING(v)) >= 5);
886 c = op->ob_sval[i];
887 if (c == quote || c == '\\')
888 *p++ = '\\', *p++ = c;
889 else if (c == '\t')
890 *p++ = '\\', *p++ = 't';
891 else if (c == '\n')
892 *p++ = '\\', *p++ = 'n';
893 else if (c == '\r')
894 *p++ = '\\', *p++ = 'r';
895 else if (c < ' ' || c >= 0x7f) {
896 /* For performance, we don't want to call
897 PyOS_snprintf here (extra layers of
898 function call). */
899 sprintf(p, "\\x%02x", c & 0xff);
900 p += 4;
901 }
902 else
903 *p++ = c;
904 }
905 assert(newsize - (p - PyString_AS_STRING(v)) >= 1);
906 *p++ = quote;
907 *p = '\0';
908 _PyString_Resize(
909 &v, (p - PyString_AS_STRING(v)));
910 return v;
911 }
912}
913
914static PyObject *
915string_repr(PyObject *op)
916{
917 return PyString_Repr(op, 1);
918}
919
920static PyObject *
921string_str(PyObject *s)
922{
923 assert(PyString_Check(s));
924 if (PyString_CheckExact(s)) {
925 Py_INCREF(s);
926 return s;
927 }
928 else {
929 /* Subtype -- return genuine string with the same value. */
930 PyStringObject *t = (PyStringObject *) s;
931 return PyString_FromStringAndSize(t->ob_sval, t->ob_size);
932 }
933}
934
935static Py_ssize_t
936string_length(PyStringObject *a)
937{
938 return a->ob_size;
939}
940
941static PyObject *
942string_concat(register PyStringObject *a, register PyObject *bb)
943{
944 register Py_ssize_t size;
945 register PyStringObject *op;
946 if (!PyString_Check(bb)) {
947#ifdef Py_USING_UNICODE
948 if (PyUnicode_Check(bb))
949 return PyUnicode_Concat((PyObject *)a, bb);
950#endif
951 PyErr_Format(PyExc_TypeError,
952 "cannot concatenate 'str' and '%.200s' objects",
953 bb->ob_type->tp_name);
954 return NULL;
955 }
956#define b ((PyStringObject *)bb)
957 /* Optimize cases with empty left or right operand */
958 if ((a->ob_size == 0 || b->ob_size == 0) &&
959 PyString_CheckExact(a) && PyString_CheckExact(b)) {
960 if (a->ob_size == 0) {
961 Py_INCREF(bb);
962 return bb;
963 }
964 Py_INCREF(a);
965 return (PyObject *)a;
966 }
967 size = a->ob_size + b->ob_size;
968 if (size < 0) {
969 PyErr_SetString(PyExc_OverflowError,
970 "strings are too large to concat");
971 return NULL;
972 }
973
974 /* Inline PyObject_NewVar */
975 op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size);
976 if (op == NULL)
977 return PyErr_NoMemory();
978 PyObject_INIT_VAR(op, &PyString_Type, size);
979 op->ob_shash = -1;
980 op->ob_sstate = SSTATE_NOT_INTERNED;
981 Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size);
982 Py_MEMCPY(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size);
983 op->ob_sval[size] = '\0';
984 return (PyObject *) op;
985#undef b
986}
987
988static PyObject *
989string_repeat(register PyStringObject *a, register Py_ssize_t n)
990{
991 register Py_ssize_t i;
992 register Py_ssize_t j;
993 register Py_ssize_t size;
994 register PyStringObject *op;
995 size_t nbytes;
996 if (n < 0)
997 n = 0;
998 /* watch out for overflows: the size can overflow int,
999 * and the # of bytes needed can overflow size_t
1000 */
1001 size = a->ob_size * n;
1002 if (n && size / n != a->ob_size) {
1003 PyErr_SetString(PyExc_OverflowError,
1004 "repeated string is too long");
1005 return NULL;
1006 }
1007 if (size == a->ob_size && PyString_CheckExact(a)) {
1008 Py_INCREF(a);
1009 return (PyObject *)a;
1010 }
1011 nbytes = (size_t)size;
1012 if (nbytes + sizeof(PyStringObject) <= nbytes) {
1013 PyErr_SetString(PyExc_OverflowError,
1014 "repeated string is too long");
1015 return NULL;
1016 }
1017 op = (PyStringObject *)
1018 PyObject_MALLOC(sizeof(PyStringObject) + nbytes);
1019 if (op == NULL)
1020 return PyErr_NoMemory();
1021 PyObject_INIT_VAR(op, &PyString_Type, size);
1022 op->ob_shash = -1;
1023 op->ob_sstate = SSTATE_NOT_INTERNED;
1024 op->ob_sval[size] = '\0';
1025 if (a->ob_size == 1 && n > 0) {
1026 memset(op->ob_sval, a->ob_sval[0] , n);
1027 return (PyObject *) op;
1028 }
1029 i = 0;
1030 if (i < size) {
1031 Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size);
1032 i = a->ob_size;
1033 }
1034 while (i < size) {
1035 j = (i <= size-i) ? i : size-i;
1036 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j);
1037 i += j;
1038 }
1039 return (PyObject *) op;
1040}
1041
1042/* String slice a[i:j] consists of characters a[i] ... a[j-1] */
1043
1044static PyObject *
1045string_slice(register PyStringObject *a, register Py_ssize_t i,
1046 register Py_ssize_t j)
1047 /* j -- may be negative! */
1048{
1049 if (i < 0)
1050 i = 0;
1051 if (j < 0)
1052 j = 0; /* Avoid signed/unsigned bug in next line */
1053 if (j > a->ob_size)
1054 j = a->ob_size;
1055 if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) {
1056 /* It's the same as a */
1057 Py_INCREF(a);
1058 return (PyObject *)a;
1059 }
1060 if (j < i)
1061 j = i;
1062 return PyString_FromStringAndSize(a->ob_sval + i, j-i);
1063}
1064
1065static int
1066string_contains(PyObject *str_obj, PyObject *sub_obj)
1067{
1068 if (!PyString_CheckExact(sub_obj)) {
1069#ifdef Py_USING_UNICODE
1070 if (PyUnicode_Check(sub_obj))
1071 return PyUnicode_Contains(str_obj, sub_obj);
1072#endif
1073 if (!PyString_Check(sub_obj)) {
1074 PyErr_SetString(PyExc_TypeError,
1075 "'in <string>' requires string as left operand");
1076 return -1;
1077 }
1078 }
1079
1080 return stringlib_contains_obj(str_obj, sub_obj);
1081}
1082
1083static PyObject *
1084string_item(PyStringObject *a, register Py_ssize_t i)
1085{
1086 char pchar;
1087 PyObject *v;
1088 if (i < 0 || i >= a->ob_size) {
1089 PyErr_SetString(PyExc_IndexError, "string index out of range");
1090 return NULL;
1091 }
1092 pchar = a->ob_sval[i];
1093 v = (PyObject *)characters[pchar & UCHAR_MAX];
1094 if (v == NULL)
1095 v = PyString_FromStringAndSize(&pchar, 1);
1096 else {
1097#ifdef COUNT_ALLOCS
1098 one_strings++;
1099#endif
1100 Py_INCREF(v);
1101 }
1102 return v;
1103}
1104
1105static PyObject*
1106string_richcompare(PyStringObject *a, PyStringObject *b, int op)
1107{
1108 int c;
1109 Py_ssize_t len_a, len_b;
1110 Py_ssize_t min_len;
1111 PyObject *result;
1112
1113 /* Make sure both arguments are strings. */
1114 if (!(PyString_Check(a) && PyString_Check(b))) {
1115 result = Py_NotImplemented;
1116 goto out;
1117 }
1118 if (a == b) {
1119 switch (op) {
1120 case Py_EQ:case Py_LE:case Py_GE:
1121 result = Py_True;
1122 goto out;
1123 case Py_NE:case Py_LT:case Py_GT:
1124 result = Py_False;
1125 goto out;
1126 }
1127 }
1128 if (op == Py_EQ) {
1129 /* Supporting Py_NE here as well does not save
1130 much time, since Py_NE is rarely used. */
1131 if (a->ob_size == b->ob_size
1132 && (a->ob_sval[0] == b->ob_sval[0]
1133 && memcmp(a->ob_sval, b->ob_sval,
1134 a->ob_size) == 0)) {
1135 result = Py_True;
1136 } else {
1137 result = Py_False;
1138 }
1139 goto out;
1140 }
1141 len_a = a->ob_size; len_b = b->ob_size;
1142 min_len = (len_a < len_b) ? len_a : len_b;
1143 if (min_len > 0) {
1144 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
1145 if (c==0)
1146 c = memcmp(a->ob_sval, b->ob_sval, min_len);
1147 }else
1148 c = 0;
1149 if (c == 0)
1150 c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
1151 switch (op) {
1152 case Py_LT: c = c < 0; break;
1153 case Py_LE: c = c <= 0; break;
1154 case Py_EQ: assert(0); break; /* unreachable */
1155 case Py_NE: c = c != 0; break;
1156 case Py_GT: c = c > 0; break;
1157 case Py_GE: c = c >= 0; break;
1158 default:
1159 result = Py_NotImplemented;
1160 goto out;
1161 }
1162 result = c ? Py_True : Py_False;
1163 out:
1164 Py_INCREF(result);
1165 return result;
1166}
1167
1168int
1169_PyString_Eq(PyObject *o1, PyObject *o2)
1170{
1171 PyStringObject *a = (PyStringObject*) o1;
1172 PyStringObject *b = (PyStringObject*) o2;
1173 return a->ob_size == b->ob_size
1174 && *a->ob_sval == *b->ob_sval
1175 && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0;
1176}
1177
1178static long
1179string_hash(PyStringObject *a)
1180{
1181 register Py_ssize_t len;
1182 register unsigned char *p;
1183 register long x;
1184
1185 if (a->ob_shash != -1)
1186 return a->ob_shash;
1187 len = a->ob_size;
1188 p = (unsigned char *) a->ob_sval;
1189 x = *p << 7;
1190 while (--len >= 0)
1191 x = (1000003*x) ^ *p++;
1192 x ^= a->ob_size;
1193 if (x == -1)
1194 x = -2;
1195 a->ob_shash = x;
1196 return x;
1197}
1198
1199static PyObject*
1200string_subscript(PyStringObject* self, PyObject* item)
1201{
1202 if (PyIndex_Check(item)) {
1203 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1204 if (i == -1 && PyErr_Occurred())
1205 return NULL;
1206 if (i < 0)
1207 i += PyString_GET_SIZE(self);
1208 return string_item(self, i);
1209 }
1210 else if (PySlice_Check(item)) {
1211 Py_ssize_t start, stop, step, slicelength, cur, i;
1212 char* source_buf;
1213 char* result_buf;
1214 PyObject* result;
1215
1216 if (PySlice_GetIndicesEx((PySliceObject*)item,
1217 PyString_GET_SIZE(self),
1218 &start, &stop, &step, &slicelength) < 0) {
1219 return NULL;
1220 }
1221
1222 if (slicelength <= 0) {
1223 return PyString_FromStringAndSize("", 0);
1224 }
1225 else {
1226 source_buf = PyString_AsString((PyObject*)self);
1227 result_buf = (char *)PyMem_Malloc(slicelength);
1228 if (result_buf == NULL)
1229 return PyErr_NoMemory();
1230
1231 for (cur = start, i = 0; i < slicelength;
1232 cur += step, i++) {
1233 result_buf[i] = source_buf[cur];
1234 }
1235
1236 result = PyString_FromStringAndSize(result_buf,
1237 slicelength);
1238 PyMem_Free(result_buf);
1239 return result;
1240 }
1241 }
1242 else {
1243 PyErr_SetString(PyExc_TypeError,
1244 "string indices must be integers");
1245 return NULL;
1246 }
1247}
1248
1249static Py_ssize_t
1250string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
1251{
1252 if ( index != 0 ) {
1253 PyErr_SetString(PyExc_SystemError,
1254 "accessing non-existent string segment");
1255 return -1;
1256 }
1257 *ptr = (void *)self->ob_sval;
1258 return self->ob_size;
1259}
1260
1261static Py_ssize_t
1262string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr)
1263{
1264 PyErr_SetString(PyExc_TypeError,
1265 "Cannot use string as modifiable buffer");
1266 return -1;
1267}
1268
1269static Py_ssize_t
1270string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp)
1271{
1272 if ( lenp )
1273 *lenp = self->ob_size;
1274 return 1;
1275}
1276
1277static Py_ssize_t
1278string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr)
1279{
1280 if ( index != 0 ) {
1281 PyErr_SetString(PyExc_SystemError,
1282 "accessing non-existent string segment");
1283 return -1;
1284 }
1285 *ptr = self->ob_sval;
1286 return self->ob_size;
1287}
1288
1289static PySequenceMethods string_as_sequence = {
1290 (lenfunc)string_length, /*sq_length*/
1291 (binaryfunc)string_concat, /*sq_concat*/
1292 (ssizeargfunc)string_repeat, /*sq_repeat*/
1293 (ssizeargfunc)string_item, /*sq_item*/
1294 (ssizessizeargfunc)string_slice, /*sq_slice*/
1295 0, /*sq_ass_item*/
1296 0, /*sq_ass_slice*/
1297 (objobjproc)string_contains /*sq_contains*/
1298};
1299
1300static PyMappingMethods string_as_mapping = {
1301 (lenfunc)string_length,
1302 (binaryfunc)string_subscript,
1303 0,
1304};
1305
1306static PyBufferProcs string_as_buffer = {
1307 (readbufferproc)string_buffer_getreadbuf,
1308 (writebufferproc)string_buffer_getwritebuf,
1309 (segcountproc)string_buffer_getsegcount,
1310 (charbufferproc)string_buffer_getcharbuf,
1311};
1312
1313
1314
1315#define LEFTSTRIP 0
1316#define RIGHTSTRIP 1
1317#define BOTHSTRIP 2
1318
1319/* Arrays indexed by above */
1320static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
1321
1322#define STRIPNAME(i) (stripformat[i]+3)
1323
1324
1325/* Don't call if length < 2 */
1326#define Py_STRING_MATCH(target, offset, pattern, length) \
1327 (target[offset] == pattern[0] && \
1328 target[offset+length-1] == pattern[length-1] && \
1329 !memcmp(target+offset+1, pattern+1, length-2) )
1330
1331
1332/* Overallocate the initial list to reduce the number of reallocs for small
1333 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
1334 resizes, to sizes 4, 8, then 16. Most observed string splits are for human
1335 text (roughly 11 words per line) and field delimited data (usually 1-10
1336 fields). For large strings the split algorithms are bandwidth limited
1337 so increasing the preallocation likely will not improve things.*/
1338
1339#define MAX_PREALLOC 12
1340
1341/* 5 splits gives 6 elements */
1342#define PREALLOC_SIZE(maxsplit) \
1343 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
1344
1345#define SPLIT_APPEND(data, left, right) \
1346 str = PyString_FromStringAndSize((data) + (left), \
1347 (right) - (left)); \
1348 if (str == NULL) \
1349 goto onError; \
1350 if (PyList_Append(list, str)) { \
1351 Py_DECREF(str); \
1352 goto onError; \
1353 } \
1354 else \
1355 Py_DECREF(str);
1356
1357#define SPLIT_ADD(data, left, right) { \
1358 str = PyString_FromStringAndSize((data) + (left), \
1359 (right) - (left)); \
1360 if (str == NULL) \
1361 goto onError; \
1362 if (count < MAX_PREALLOC) { \
1363 PyList_SET_ITEM(list, count, str); \
1364 } else { \
1365 if (PyList_Append(list, str)) { \
1366 Py_DECREF(str); \
1367 goto onError; \
1368 } \
1369 else \
1370 Py_DECREF(str); \
1371 } \
1372 count++; }
1373
1374/* Always force the list to the expected size. */
1375#define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count
1376
1377#define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; }
1378#define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; }
1379#define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; }
1380#define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; }
1381
1382Py_LOCAL_INLINE(PyObject *)
1383split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
1384{
1385 Py_ssize_t i, j, count=0;
1386 PyObject *str;
1387 PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
1388
1389 if (list == NULL)
1390 return NULL;
1391
1392 i = j = 0;
1393
1394 while (maxsplit-- > 0) {
1395 SKIP_SPACE(s, i, len);
1396 if (i==len) break;
1397 j = i; i++;
1398 SKIP_NONSPACE(s, i, len);
1399 SPLIT_ADD(s, j, i);
1400 }
1401
1402 if (i < len) {
1403 /* Only occurs when maxsplit was reached */
1404 /* Skip any remaining whitespace and copy to end of string */
1405 SKIP_SPACE(s, i, len);
1406 if (i != len)
1407 SPLIT_ADD(s, i, len);
1408 }
1409 FIX_PREALLOC_SIZE(list);
1410 return list;
1411 onError:
1412 Py_DECREF(list);
1413 return NULL;
1414}
1415
1416Py_LOCAL_INLINE(PyObject *)
1417split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
1418{
1419 register Py_ssize_t i, j, count=0;
1420 PyObject *str;
1421 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
1422
1423 if (list == NULL)
1424 return NULL;
1425
1426 i = j = 0;
1427 while ((j < len) && (maxcount-- > 0)) {
1428 for(; j<len; j++) {
1429 /* I found that using memchr makes no difference */
1430 if (s[j] == ch) {
1431 SPLIT_ADD(s, i, j);
1432 i = j = j + 1;
1433 break;
1434 }
1435 }
1436 }
1437 if (i <= len) {
1438 SPLIT_ADD(s, i, len);
1439 }
1440 FIX_PREALLOC_SIZE(list);
1441 return list;
1442
1443 onError:
1444 Py_DECREF(list);
1445 return NULL;
1446}
1447
1448PyDoc_STRVAR(split__doc__,
1449"S.split([sep [,maxsplit]]) -> list of strings\n\
1450\n\
1451Return a list of the words in the string S, using sep as the\n\
1452delimiter string. If maxsplit is given, at most maxsplit\n\
1453splits are done. If sep is not specified or is None, any\n\
1454whitespace string is a separator.");
1455
1456static PyObject *
1457string_split(PyStringObject *self, PyObject *args)
1458{
1459 Py_ssize_t len = PyString_GET_SIZE(self), n, i, j;
1460 Py_ssize_t maxsplit = -1, count=0;
1461 const char *s = PyString_AS_STRING(self), *sub;
1462 PyObject *list, *str, *subobj = Py_None;
1463#ifdef USE_FAST
1464 Py_ssize_t pos;
1465#endif
1466
1467 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
1468 return NULL;
1469 if (maxsplit < 0)
1470 maxsplit = PY_SSIZE_T_MAX;
1471 if (subobj == Py_None)
1472 return split_whitespace(s, len, maxsplit);
1473 if (PyString_Check(subobj)) {
1474 sub = PyString_AS_STRING(subobj);
1475 n = PyString_GET_SIZE(subobj);
1476 }
1477#ifdef Py_USING_UNICODE
1478 else if (PyUnicode_Check(subobj))
1479 return PyUnicode_Split((PyObject *)self, subobj, maxsplit);
1480#endif
1481 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
1482 return NULL;
1483
1484 if (n == 0) {
1485 PyErr_SetString(PyExc_ValueError, "empty separator");
1486 return NULL;
1487 }
1488 else if (n == 1)
1489 return split_char(s, len, sub[0], maxsplit);
1490
1491 list = PyList_New(PREALLOC_SIZE(maxsplit));
1492 if (list == NULL)
1493 return NULL;
1494
1495#ifdef USE_FAST
1496 i = j = 0;
1497 while (maxsplit-- > 0) {
1498 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
1499 if (pos < 0)
1500 break;
1501 j = i+pos;
1502 SPLIT_ADD(s, i, j);
1503 i = j + n;
1504 }
1505#else
1506 i = j = 0;
1507 while ((j+n <= len) && (maxsplit-- > 0)) {
1508 for (; j+n <= len; j++) {
1509 if (Py_STRING_MATCH(s, j, sub, n)) {
1510 SPLIT_ADD(s, i, j);
1511 i = j = j + n;
1512 break;
1513 }
1514 }
1515 }
1516#endif
1517 SPLIT_ADD(s, i, len);
1518 FIX_PREALLOC_SIZE(list);
1519 return list;
1520
1521 onError:
1522 Py_DECREF(list);
1523 return NULL;
1524}
1525
1526PyDoc_STRVAR(partition__doc__,
1527"S.partition(sep) -> (head, sep, tail)\n\
1528\n\
1529Searches for the separator sep in S, and returns the part before it,\n\
1530the separator itself, and the part after it. If the separator is not\n\
1531found, returns S and two empty strings.");
1532
1533static PyObject *
1534string_partition(PyStringObject *self, PyObject *sep_obj)
1535{
1536 const char *sep;
1537 Py_ssize_t sep_len;
1538
1539 if (PyString_Check(sep_obj)) {
1540 sep = PyString_AS_STRING(sep_obj);
1541 sep_len = PyString_GET_SIZE(sep_obj);
1542 }
1543#ifdef Py_USING_UNICODE
1544 else if (PyUnicode_Check(sep_obj))
1545 return PyUnicode_Partition((PyObject *) self, sep_obj);
1546#endif
1547 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
1548 return NULL;
1549
1550 return stringlib_partition(
1551 (PyObject*) self,
1552 PyString_AS_STRING(self), PyString_GET_SIZE(self),
1553 sep_obj, sep, sep_len
1554 );
1555}
1556
1557PyDoc_STRVAR(rpartition__doc__,
1558"S.rpartition(sep) -> (tail, sep, head)\n\
1559\n\
1560Searches for the separator sep in S, starting at the end of S, and returns\n\
1561the part before it, the separator itself, and the part after it. If the\n\
1562separator is not found, returns two empty strings and S.");
1563
1564static PyObject *
1565string_rpartition(PyStringObject *self, PyObject *sep_obj)
1566{
1567 const char *sep;
1568 Py_ssize_t sep_len;
1569
1570 if (PyString_Check(sep_obj)) {
1571 sep = PyString_AS_STRING(sep_obj);
1572 sep_len = PyString_GET_SIZE(sep_obj);
1573 }
1574#ifdef Py_USING_UNICODE
1575 else if (PyUnicode_Check(sep_obj))
1576 return PyUnicode_Partition((PyObject *) self, sep_obj);
1577#endif
1578 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len))
1579 return NULL;
1580
1581 return stringlib_rpartition(
1582 (PyObject*) self,
1583 PyString_AS_STRING(self), PyString_GET_SIZE(self),
1584 sep_obj, sep, sep_len
1585 );
1586}
1587
1588Py_LOCAL_INLINE(PyObject *)
1589rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit)
1590{
1591 Py_ssize_t i, j, count=0;
1592 PyObject *str;
1593 PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit));
1594
1595 if (list == NULL)
1596 return NULL;
1597
1598 i = j = len-1;
1599
1600 while (maxsplit-- > 0) {
1601 RSKIP_SPACE(s, i);
1602 if (i<0) break;
1603 j = i; i--;
1604 RSKIP_NONSPACE(s, i);
1605 SPLIT_ADD(s, i + 1, j + 1);
1606 }
1607 if (i >= 0) {
1608 /* Only occurs when maxsplit was reached */
1609 /* Skip any remaining whitespace and copy to beginning of string */
1610 RSKIP_SPACE(s, i);
1611 if (i >= 0)
1612 SPLIT_ADD(s, 0, i + 1);
1613
1614 }
1615 FIX_PREALLOC_SIZE(list);
1616 if (PyList_Reverse(list) < 0)
1617 goto onError;
1618 return list;
1619 onError:
1620 Py_DECREF(list);
1621 return NULL;
1622}
1623
1624Py_LOCAL_INLINE(PyObject *)
1625rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
1626{
1627 register Py_ssize_t i, j, count=0;
1628 PyObject *str;
1629 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
1630
1631 if (list == NULL)
1632 return NULL;
1633
1634 i = j = len - 1;
1635 while ((i >= 0) && (maxcount-- > 0)) {
1636 for (; i >= 0; i--) {
1637 if (s[i] == ch) {
1638 SPLIT_ADD(s, i + 1, j + 1);
1639 j = i = i - 1;
1640 break;
1641 }
1642 }
1643 }
1644 if (j >= -1) {
1645 SPLIT_ADD(s, 0, j + 1);
1646 }
1647 FIX_PREALLOC_SIZE(list);
1648 if (PyList_Reverse(list) < 0)
1649 goto onError;
1650 return list;
1651
1652 onError:
1653 Py_DECREF(list);
1654 return NULL;
1655}
1656
1657PyDoc_STRVAR(rsplit__doc__,
1658"S.rsplit([sep [,maxsplit]]) -> list of strings\n\
1659\n\
1660Return a list of the words in the string S, using sep as the\n\
1661delimiter string, starting at the end of the string and working\n\
1662to the front. If maxsplit is given, at most maxsplit splits are\n\
1663done. If sep is not specified or is None, any whitespace string\n\
1664is a separator.");
1665
1666static PyObject *
1667string_rsplit(PyStringObject *self, PyObject *args)
1668{
1669 Py_ssize_t len = PyString_GET_SIZE(self), n, i, j;
1670 Py_ssize_t maxsplit = -1, count=0;
1671 const char *s = PyString_AS_STRING(self), *sub;
1672 PyObject *list, *str, *subobj = Py_None;
1673
1674 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
1675 return NULL;
1676 if (maxsplit < 0)
1677 maxsplit = PY_SSIZE_T_MAX;
1678 if (subobj == Py_None)
1679 return rsplit_whitespace(s, len, maxsplit);
1680 if (PyString_Check(subobj)) {
1681 sub = PyString_AS_STRING(subobj);
1682 n = PyString_GET_SIZE(subobj);
1683 }
1684#ifdef Py_USING_UNICODE
1685 else if (PyUnicode_Check(subobj))
1686 return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit);
1687#endif
1688 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
1689 return NULL;
1690
1691 if (n == 0) {
1692 PyErr_SetString(PyExc_ValueError, "empty separator");
1693 return NULL;
1694 }
1695 else if (n == 1)
1696 return rsplit_char(s, len, sub[0], maxsplit);
1697
1698 list = PyList_New(PREALLOC_SIZE(maxsplit));
1699 if (list == NULL)
1700 return NULL;
1701
1702 j = len;
1703 i = j - n;
1704
1705 while ( (i >= 0) && (maxsplit-- > 0) ) {
1706 for (; i>=0; i--) {
1707 if (Py_STRING_MATCH(s, i, sub, n)) {
1708 SPLIT_ADD(s, i + n, j);
1709 j = i;
1710 i -= n;
1711 break;
1712 }
1713 }
1714 }
1715 SPLIT_ADD(s, 0, j);
1716 FIX_PREALLOC_SIZE(list);
1717 if (PyList_Reverse(list) < 0)
1718 goto onError;
1719 return list;
1720
1721onError:
1722 Py_DECREF(list);
1723 return NULL;
1724}
1725
1726
1727PyDoc_STRVAR(join__doc__,
1728"S.join(sequence) -> string\n\
1729\n\
1730Return a string which is the concatenation of the strings in the\n\
1731sequence. The separator between elements is S.");
1732
1733static PyObject *
1734string_join(PyStringObject *self, PyObject *orig)
1735{
1736 char *sep = PyString_AS_STRING(self);
1737 const Py_ssize_t seplen = PyString_GET_SIZE(self);
1738 PyObject *res = NULL;
1739 char *p;
1740 Py_ssize_t seqlen = 0;
1741 size_t sz = 0;
1742 Py_ssize_t i;
1743 PyObject *seq, *item;
1744
1745 seq = PySequence_Fast(orig, "");
1746 if (seq == NULL) {
1747 return NULL;
1748 }
1749
1750 seqlen = PySequence_Size(seq);
1751 if (seqlen == 0) {
1752 Py_DECREF(seq);
1753 return PyString_FromString("");
1754 }
1755 if (seqlen == 1) {
1756 item = PySequence_Fast_GET_ITEM(seq, 0);
1757 if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) {
1758 Py_INCREF(item);
1759 Py_DECREF(seq);
1760 return item;
1761 }
1762 }
1763
1764 /* There are at least two things to join, or else we have a subclass
1765 * of the builtin types in the sequence.
1766 * Do a pre-pass to figure out the total amount of space we'll
1767 * need (sz), see whether any argument is absurd, and defer to
1768 * the Unicode join if appropriate.
1769 */
1770 for (i = 0; i < seqlen; i++) {
1771 const size_t old_sz = sz;
1772 item = PySequence_Fast_GET_ITEM(seq, i);
1773 if (!PyString_Check(item)){
1774#ifdef Py_USING_UNICODE
1775 if (PyUnicode_Check(item)) {
1776 /* Defer to Unicode join.
1777 * CAUTION: There's no gurantee that the
1778 * original sequence can be iterated over
1779 * again, so we must pass seq here.
1780 */
1781 PyObject *result;
1782 result = PyUnicode_Join((PyObject *)self, seq);
1783 Py_DECREF(seq);
1784 return result;
1785 }
1786#endif
1787 PyErr_Format(PyExc_TypeError,
1788 "sequence item %zd: expected string,"
1789 " %.80s found",
1790 i, item->ob_type->tp_name);
1791 Py_DECREF(seq);
1792 return NULL;
1793 }
1794 sz += PyString_GET_SIZE(item);
1795 if (i != 0)
1796 sz += seplen;
1797 if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
1798 PyErr_SetString(PyExc_OverflowError,
1799 "join() result is too long for a Python string");
1800 Py_DECREF(seq);
1801 return NULL;
1802 }
1803 }
1804
1805 /* Allocate result space. */
1806 res = PyString_FromStringAndSize((char*)NULL, sz);
1807 if (res == NULL) {
1808 Py_DECREF(seq);
1809 return NULL;
1810 }
1811
1812 /* Catenate everything. */
1813 p = PyString_AS_STRING(res);
1814 for (i = 0; i < seqlen; ++i) {
1815 size_t n;
1816 item = PySequence_Fast_GET_ITEM(seq, i);
1817 n = PyString_GET_SIZE(item);
1818 Py_MEMCPY(p, PyString_AS_STRING(item), n);
1819 p += n;
1820 if (i < seqlen - 1) {
1821 Py_MEMCPY(p, sep, seplen);
1822 p += seplen;
1823 }
1824 }
1825
1826 Py_DECREF(seq);
1827 return res;
1828}
1829
1830PyObject *
1831_PyString_Join(PyObject *sep, PyObject *x)
1832{
1833 assert(sep != NULL && PyString_Check(sep));
1834 assert(x != NULL);
1835 return string_join((PyStringObject *)sep, x);
1836}
1837
1838Py_LOCAL_INLINE(void)
1839string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
1840{
1841 if (*end > len)
1842 *end = len;
1843 else if (*end < 0)
1844 *end += len;
1845 if (*end < 0)
1846 *end = 0;
1847 if (*start < 0)
1848 *start += len;
1849 if (*start < 0)
1850 *start = 0;
1851}
1852
1853Py_LOCAL_INLINE(Py_ssize_t)
1854string_find_internal(PyStringObject *self, PyObject *args, int dir)
1855{
1856 PyObject *subobj;
1857 const char *sub;
1858 Py_ssize_t sub_len;
1859 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1860
1861 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
1862 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
1863 return -2;
1864 if (PyString_Check(subobj)) {
1865 sub = PyString_AS_STRING(subobj);
1866 sub_len = PyString_GET_SIZE(subobj);
1867 }
1868#ifdef Py_USING_UNICODE
1869 else if (PyUnicode_Check(subobj))
1870 return PyUnicode_Find(
1871 (PyObject *)self, subobj, start, end, dir);
1872#endif
1873 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
1874 /* XXX - the "expected a character buffer object" is pretty
1875 confusing for a non-expert. remap to something else ? */
1876 return -2;
1877
1878 if (dir > 0)
1879 return stringlib_find_slice(
1880 PyString_AS_STRING(self), PyString_GET_SIZE(self),
1881 sub, sub_len, start, end);
1882 else
1883 return stringlib_rfind_slice(
1884 PyString_AS_STRING(self), PyString_GET_SIZE(self),
1885 sub, sub_len, start, end);
1886}
1887
1888
1889PyDoc_STRVAR(find__doc__,
1890"S.find(sub [,start [,end]]) -> int\n\
1891\n\
1892Return the lowest index in S where substring sub is found,\n\
1893such that sub is contained within s[start,end]. Optional\n\
1894arguments start and end are interpreted as in slice notation.\n\
1895\n\
1896Return -1 on failure.");
1897
1898static PyObject *
1899string_find(PyStringObject *self, PyObject *args)
1900{
1901 Py_ssize_t result = string_find_internal(self, args, +1);
1902 if (result == -2)
1903 return NULL;
1904 return PyInt_FromSsize_t(result);
1905}
1906
1907
1908PyDoc_STRVAR(index__doc__,
1909"S.index(sub [,start [,end]]) -> int\n\
1910\n\
1911Like S.find() but raise ValueError when the substring is not found.");
1912
1913static PyObject *
1914string_index(PyStringObject *self, PyObject *args)
1915{
1916 Py_ssize_t result = string_find_internal(self, args, +1);
1917 if (result == -2)
1918 return NULL;
1919 if (result == -1) {
1920 PyErr_SetString(PyExc_ValueError,
1921 "substring not found");
1922 return NULL;
1923 }
1924 return PyInt_FromSsize_t(result);
1925}
1926
1927
1928PyDoc_STRVAR(rfind__doc__,
1929"S.rfind(sub [,start [,end]]) -> int\n\
1930\n\
1931Return the highest index in S where substring sub is found,\n\
1932such that sub is contained within s[start,end]. Optional\n\
1933arguments start and end are interpreted as in slice notation.\n\
1934\n\
1935Return -1 on failure.");
1936
1937static PyObject *
1938string_rfind(PyStringObject *self, PyObject *args)
1939{
1940 Py_ssize_t result = string_find_internal(self, args, -1);
1941 if (result == -2)
1942 return NULL;
1943 return PyInt_FromSsize_t(result);
1944}
1945
1946
1947PyDoc_STRVAR(rindex__doc__,
1948"S.rindex(sub [,start [,end]]) -> int\n\
1949\n\
1950Like S.rfind() but raise ValueError when the substring is not found.");
1951
1952static PyObject *
1953string_rindex(PyStringObject *self, PyObject *args)
1954{
1955 Py_ssize_t result = string_find_internal(self, args, -1);
1956 if (result == -2)
1957 return NULL;
1958 if (result == -1) {
1959 PyErr_SetString(PyExc_ValueError,
1960 "substring not found");
1961 return NULL;
1962 }
1963 return PyInt_FromSsize_t(result);
1964}
1965
1966
1967Py_LOCAL_INLINE(PyObject *)
1968do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj)
1969{
1970 char *s = PyString_AS_STRING(self);
1971 Py_ssize_t len = PyString_GET_SIZE(self);
1972 char *sep = PyString_AS_STRING(sepobj);
1973 Py_ssize_t seplen = PyString_GET_SIZE(sepobj);
1974 Py_ssize_t i, j;
1975
1976 i = 0;
1977 if (striptype != RIGHTSTRIP) {
1978 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
1979 i++;
1980 }
1981 }
1982
1983 j = len;
1984 if (striptype != LEFTSTRIP) {
1985 do {
1986 j--;
1987 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
1988 j++;
1989 }
1990
1991 if (i == 0 && j == len && PyString_CheckExact(self)) {
1992 Py_INCREF(self);
1993 return (PyObject*)self;
1994 }
1995 else
1996 return PyString_FromStringAndSize(s+i, j-i);
1997}
1998
1999
2000Py_LOCAL_INLINE(PyObject *)
2001do_strip(PyStringObject *self, int striptype)
2002{
2003 char *s = PyString_AS_STRING(self);
2004 Py_ssize_t len = PyString_GET_SIZE(self), i, j;
2005
2006 i = 0;
2007 if (striptype != RIGHTSTRIP) {
2008 while (i < len && isspace(Py_CHARMASK(s[i]))) {
2009 i++;
2010 }
2011 }
2012
2013 j = len;
2014 if (striptype != LEFTSTRIP) {
2015 do {
2016 j--;
2017 } while (j >= i && isspace(Py_CHARMASK(s[j])));
2018 j++;
2019 }
2020
2021 if (i == 0 && j == len && PyString_CheckExact(self)) {
2022 Py_INCREF(self);
2023 return (PyObject*)self;
2024 }
2025 else
2026 return PyString_FromStringAndSize(s+i, j-i);
2027}
2028
2029
2030Py_LOCAL_INLINE(PyObject *)
2031do_argstrip(PyStringObject *self, int striptype, PyObject *args)
2032{
2033 PyObject *sep = NULL;
2034
2035 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
2036 return NULL;
2037
2038 if (sep != NULL && sep != Py_None) {
2039 if (PyString_Check(sep))
2040 return do_xstrip(self, striptype, sep);
2041#ifdef Py_USING_UNICODE
2042 else if (PyUnicode_Check(sep)) {
2043 PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
2044 PyObject *res;
2045 if (uniself==NULL)
2046 return NULL;
2047 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
2048 striptype, sep);
2049 Py_DECREF(uniself);
2050 return res;
2051 }
2052#endif
2053 PyErr_Format(PyExc_TypeError,
2054#ifdef Py_USING_UNICODE
2055 "%s arg must be None, str or unicode",
2056#else
2057 "%s arg must be None or str",
2058#endif
2059 STRIPNAME(striptype));
2060 return NULL;
2061 }
2062
2063 return do_strip(self, striptype);
2064}
2065
2066
2067PyDoc_STRVAR(strip__doc__,
2068"S.strip([chars]) -> string or unicode\n\
2069\n\
2070Return a copy of the string S with leading and trailing\n\
2071whitespace removed.\n\
2072If chars is given and not None, remove characters in chars instead.\n\
2073If chars is unicode, S will be converted to unicode before stripping");
2074
2075static PyObject *
2076string_strip(PyStringObject *self, PyObject *args)
2077{
2078 if (PyTuple_GET_SIZE(args) == 0)
2079 return do_strip(self, BOTHSTRIP); /* Common case */
2080 else
2081 return do_argstrip(self, BOTHSTRIP, args);
2082}
2083
2084
2085PyDoc_STRVAR(lstrip__doc__,
2086"S.lstrip([chars]) -> string or unicode\n\
2087\n\
2088Return a copy of the string S with leading whitespace removed.\n\
2089If chars is given and not None, remove characters in chars instead.\n\
2090If chars is unicode, S will be converted to unicode before stripping");
2091
2092static PyObject *
2093string_lstrip(PyStringObject *self, PyObject *args)
2094{
2095 if (PyTuple_GET_SIZE(args) == 0)
2096 return do_strip(self, LEFTSTRIP); /* Common case */
2097 else
2098 return do_argstrip(self, LEFTSTRIP, args);
2099}
2100
2101
2102PyDoc_STRVAR(rstrip__doc__,
2103"S.rstrip([chars]) -> string or unicode\n\
2104\n\
2105Return a copy of the string S with trailing whitespace removed.\n\
2106If chars is given and not None, remove characters in chars instead.\n\
2107If chars is unicode, S will be converted to unicode before stripping");
2108
2109static PyObject *
2110string_rstrip(PyStringObject *self, PyObject *args)
2111{
2112 if (PyTuple_GET_SIZE(args) == 0)
2113 return do_strip(self, RIGHTSTRIP); /* Common case */
2114 else
2115 return do_argstrip(self, RIGHTSTRIP, args);
2116}
2117
2118
2119PyDoc_STRVAR(lower__doc__,
2120"S.lower() -> string\n\
2121\n\
2122Return a copy of the string S converted to lowercase.");
2123
2124/* _tolower and _toupper are defined by SUSv2, but they're not ISO C */
2125#ifndef _tolower
2126#define _tolower tolower
2127#endif
2128
2129static PyObject *
2130string_lower(PyStringObject *self)
2131{
2132 char *s;
2133 Py_ssize_t i, n = PyString_GET_SIZE(self);
2134 PyObject *newobj;
2135
2136 newobj = PyString_FromStringAndSize(NULL, n);
2137 if (!newobj)
2138 return NULL;
2139
2140 s = PyString_AS_STRING(newobj);
2141
2142 Py_MEMCPY(s, PyString_AS_STRING(self), n);
2143
2144 for (i = 0; i < n; i++) {
2145 int c = Py_CHARMASK(s[i]);
2146 if (isupper(c))
2147 s[i] = _tolower(c);
2148 }
2149
2150 return newobj;
2151}
2152
2153PyDoc_STRVAR(upper__doc__,
2154"S.upper() -> string\n\
2155\n\
2156Return a copy of the string S converted to uppercase.");
2157
2158#ifndef _toupper
2159#define _toupper toupper
2160#endif
2161
2162static PyObject *
2163string_upper(PyStringObject *self)
2164{
2165 char *s;
2166 Py_ssize_t i, n = PyString_GET_SIZE(self);
2167 PyObject *newobj;
2168
2169 newobj = PyString_FromStringAndSize(NULL, n);
2170 if (!newobj)
2171 return NULL;
2172
2173 s = PyString_AS_STRING(newobj);
2174
2175 Py_MEMCPY(s, PyString_AS_STRING(self), n);
2176
2177 for (i = 0; i < n; i++) {
2178 int c = Py_CHARMASK(s[i]);
2179 if (islower(c))
2180 s[i] = _toupper(c);
2181 }
2182
2183 return newobj;
2184}
2185
2186PyDoc_STRVAR(title__doc__,
2187"S.title() -> string\n\
2188\n\
2189Return a titlecased version of S, i.e. words start with uppercase\n\
2190characters, all remaining cased characters have lowercase.");
2191
2192static PyObject*
2193string_title(PyStringObject *self)
2194{
2195 char *s = PyString_AS_STRING(self), *s_new;
2196 Py_ssize_t i, n = PyString_GET_SIZE(self);
2197 int previous_is_cased = 0;
2198 PyObject *newobj;
2199
2200 newobj = PyString_FromStringAndSize(NULL, n);
2201 if (newobj == NULL)
2202 return NULL;
2203 s_new = PyString_AsString(newobj);
2204 for (i = 0; i < n; i++) {
2205 int c = Py_CHARMASK(*s++);
2206 if (islower(c)) {
2207 if (!previous_is_cased)
2208 c = toupper(c);
2209 previous_is_cased = 1;
2210 } else if (isupper(c)) {
2211 if (previous_is_cased)
2212 c = tolower(c);
2213 previous_is_cased = 1;
2214 } else
2215 previous_is_cased = 0;
2216 *s_new++ = c;
2217 }
2218 return newobj;
2219}
2220
2221PyDoc_STRVAR(capitalize__doc__,
2222"S.capitalize() -> string\n\
2223\n\
2224Return a copy of the string S with only its first character\n\
2225capitalized.");
2226
2227static PyObject *
2228string_capitalize(PyStringObject *self)
2229{
2230 char *s = PyString_AS_STRING(self), *s_new;
2231 Py_ssize_t i, n = PyString_GET_SIZE(self);
2232 PyObject *newobj;
2233
2234 newobj = PyString_FromStringAndSize(NULL, n);
2235 if (newobj == NULL)
2236 return NULL;
2237 s_new = PyString_AsString(newobj);
2238 if (0 < n) {
2239 int c = Py_CHARMASK(*s++);
2240 if (islower(c))
2241 *s_new = toupper(c);
2242 else
2243 *s_new = c;
2244 s_new++;
2245 }
2246 for (i = 1; i < n; i++) {
2247 int c = Py_CHARMASK(*s++);
2248 if (isupper(c))
2249 *s_new = tolower(c);
2250 else
2251 *s_new = c;
2252 s_new++;
2253 }
2254 return newobj;
2255}
2256
2257
2258PyDoc_STRVAR(count__doc__,
2259"S.count(sub[, start[, end]]) -> int\n\
2260\n\
2261Return the number of non-overlapping occurrences of substring sub in\n\
2262string S[start:end]. Optional arguments start and end are interpreted\n\
2263as in slice notation.");
2264
2265static PyObject *
2266string_count(PyStringObject *self, PyObject *args)
2267{
2268 PyObject *sub_obj;
2269 const char *str = PyString_AS_STRING(self), *sub;
2270 Py_ssize_t sub_len;
2271 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
2272
2273 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
2274 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
2275 return NULL;
2276
2277 if (PyString_Check(sub_obj)) {
2278 sub = PyString_AS_STRING(sub_obj);
2279 sub_len = PyString_GET_SIZE(sub_obj);
2280 }
2281#ifdef Py_USING_UNICODE
2282 else if (PyUnicode_Check(sub_obj)) {
2283 Py_ssize_t count;
2284 count = PyUnicode_Count((PyObject *)self, sub_obj, start, end);
2285 if (count == -1)
2286 return NULL;
2287 else
2288 return PyInt_FromSsize_t(count);
2289 }
2290#endif
2291 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
2292 return NULL;
2293
2294 string_adjust_indices(&start, &end, PyString_GET_SIZE(self));
2295
2296 return PyInt_FromSsize_t(
2297 stringlib_count(str + start, end - start, sub, sub_len)
2298 );
2299}
2300
2301PyDoc_STRVAR(swapcase__doc__,
2302"S.swapcase() -> string\n\
2303\n\
2304Return a copy of the string S with uppercase characters\n\
2305converted to lowercase and vice versa.");
2306
2307static PyObject *
2308string_swapcase(PyStringObject *self)
2309{
2310 char *s = PyString_AS_STRING(self), *s_new;
2311 Py_ssize_t i, n = PyString_GET_SIZE(self);
2312 PyObject *newobj;
2313
2314 newobj = PyString_FromStringAndSize(NULL, n);
2315 if (newobj == NULL)
2316 return NULL;
2317 s_new = PyString_AsString(newobj);
2318 for (i = 0; i < n; i++) {
2319 int c = Py_CHARMASK(*s++);
2320 if (islower(c)) {
2321 *s_new = toupper(c);
2322 }
2323 else if (isupper(c)) {
2324 *s_new = tolower(c);
2325 }
2326 else
2327 *s_new = c;
2328 s_new++;
2329 }
2330 return newobj;
2331}
2332
2333
2334PyDoc_STRVAR(translate__doc__,
2335"S.translate(table [,deletechars]) -> string\n\
2336\n\
2337Return a copy of the string S, where all characters occurring\n\
2338in the optional argument deletechars are removed, and the\n\
2339remaining characters have been mapped through the given\n\
2340translation table, which must be a string of length 256.");
2341
2342static PyObject *
2343string_translate(PyStringObject *self, PyObject *args)
2344{
2345 register char *input, *output;
2346 register const char *table;
2347 register Py_ssize_t i, c, changed = 0;
2348 PyObject *input_obj = (PyObject*)self;
2349 const char *table1, *output_start, *del_table=NULL;
2350 Py_ssize_t inlen, tablen, dellen = 0;
2351 PyObject *result;
2352 int trans_table[256];
2353 PyObject *tableobj, *delobj = NULL;
2354
2355 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
2356 &tableobj, &delobj))
2357 return NULL;
2358
2359 if (PyString_Check(tableobj)) {
2360 table1 = PyString_AS_STRING(tableobj);
2361 tablen = PyString_GET_SIZE(tableobj);
2362 }
2363#ifdef Py_USING_UNICODE
2364 else if (PyUnicode_Check(tableobj)) {
2365 /* Unicode .translate() does not support the deletechars
2366 parameter; instead a mapping to None will cause characters
2367 to be deleted. */
2368 if (delobj != NULL) {
2369 PyErr_SetString(PyExc_TypeError,
2370 "deletions are implemented differently for unicode");
2371 return NULL;
2372 }
2373 return PyUnicode_Translate((PyObject *)self, tableobj, NULL);
2374 }
2375#endif
2376 else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen))
2377 return NULL;
2378
2379 if (tablen != 256) {
2380 PyErr_SetString(PyExc_ValueError,
2381 "translation table must be 256 characters long");
2382 return NULL;
2383 }
2384
2385 if (delobj != NULL) {
2386 if (PyString_Check(delobj)) {
2387 del_table = PyString_AS_STRING(delobj);
2388 dellen = PyString_GET_SIZE(delobj);
2389 }
2390#ifdef Py_USING_UNICODE
2391 else if (PyUnicode_Check(delobj)) {
2392 PyErr_SetString(PyExc_TypeError,
2393 "deletions are implemented differently for unicode");
2394 return NULL;
2395 }
2396#endif
2397 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
2398 return NULL;
2399 }
2400 else {
2401 del_table = NULL;
2402 dellen = 0;
2403 }
2404
2405 table = table1;
2406 inlen = PyString_GET_SIZE(input_obj);
2407 result = PyString_FromStringAndSize((char *)NULL, inlen);
2408 if (result == NULL)
2409 return NULL;
2410 output_start = output = PyString_AsString(result);
2411 input = PyString_AS_STRING(input_obj);
2412
2413 if (dellen == 0) {
2414 /* If no deletions are required, use faster code */
2415 for (i = inlen; --i >= 0; ) {
2416 c = Py_CHARMASK(*input++);
2417 if (Py_CHARMASK((*output++ = table[c])) != c)
2418 changed = 1;
2419 }
2420 if (changed || !PyString_CheckExact(input_obj))
2421 return result;
2422 Py_DECREF(result);
2423 Py_INCREF(input_obj);
2424 return input_obj;
2425 }
2426
2427 for (i = 0; i < 256; i++)
2428 trans_table[i] = Py_CHARMASK(table[i]);
2429
2430 for (i = 0; i < dellen; i++)
2431 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
2432
2433 for (i = inlen; --i >= 0; ) {
2434 c = Py_CHARMASK(*input++);
2435 if (trans_table[c] != -1)
2436 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
2437 continue;
2438 changed = 1;
2439 }
2440 if (!changed && PyString_CheckExact(input_obj)) {
2441 Py_DECREF(result);
2442 Py_INCREF(input_obj);
2443 return input_obj;
2444 }
2445 /* Fix the size of the resulting string */
2446 if (inlen > 0)
2447 _PyString_Resize(&result, output - output_start);
2448 return result;
2449}
2450
2451
2452#define FORWARD 1
2453#define REVERSE -1
2454
2455/* find and count characters and substrings */
2456
2457#define findchar(target, target_len, c) \
2458 ((char *)memchr((const void *)(target), c, target_len))
2459
2460/* String ops must return a string. */
2461/* If the object is subclass of string, create a copy */
2462Py_LOCAL(PyStringObject *)
2463return_self(PyStringObject *self)
2464{
2465 if (PyString_CheckExact(self)) {
2466 Py_INCREF(self);
2467 return self;
2468 }
2469 return (PyStringObject *)PyString_FromStringAndSize(
2470 PyString_AS_STRING(self),
2471 PyString_GET_SIZE(self));
2472}
2473
2474Py_LOCAL_INLINE(Py_ssize_t)
2475countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
2476{
2477 Py_ssize_t count=0;
2478 const char *start=target;
2479 const char *end=target+target_len;
2480
2481 while ( (start=findchar(start, end-start, c)) != NULL ) {
2482 count++;
2483 if (count >= maxcount)
2484 break;
2485 start += 1;
2486 }
2487 return count;
2488}
2489
2490Py_LOCAL(Py_ssize_t)
2491findstring(const char *target, Py_ssize_t target_len,
2492 const char *pattern, Py_ssize_t pattern_len,
2493 Py_ssize_t start,
2494 Py_ssize_t end,
2495 int direction)
2496{
2497 if (start < 0) {
2498 start += target_len;
2499 if (start < 0)
2500 start = 0;
2501 }
2502 if (end > target_len) {
2503 end = target_len;
2504 } else if (end < 0) {
2505 end += target_len;
2506 if (end < 0)
2507 end = 0;
2508 }
2509
2510 /* zero-length substrings always match at the first attempt */
2511 if (pattern_len == 0)
2512 return (direction > 0) ? start : end;
2513
2514 end -= pattern_len;
2515
2516 if (direction < 0) {
2517 for (; end >= start; end--)
2518 if (Py_STRING_MATCH(target, end, pattern, pattern_len))
2519 return end;
2520 } else {
2521 for (; start <= end; start++)
2522 if (Py_STRING_MATCH(target, start, pattern, pattern_len))
2523 return start;
2524 }
2525 return -1;
2526}
2527
2528Py_LOCAL_INLINE(Py_ssize_t)
2529countstring(const char *target, Py_ssize_t target_len,
2530 const char *pattern, Py_ssize_t pattern_len,
2531 Py_ssize_t start,
2532 Py_ssize_t end,
2533 int direction, Py_ssize_t maxcount)
2534{
2535 Py_ssize_t count=0;
2536
2537 if (start < 0) {
2538 start += target_len;
2539 if (start < 0)
2540 start = 0;
2541 }
2542 if (end > target_len) {
2543 end = target_len;
2544 } else if (end < 0) {
2545 end += target_len;
2546 if (end < 0)
2547 end = 0;
2548 }
2549
2550 /* zero-length substrings match everywhere */
2551 if (pattern_len == 0 || maxcount == 0) {
2552 if (target_len+1 < maxcount)
2553 return target_len+1;
2554 return maxcount;
2555 }
2556
2557 end -= pattern_len;
2558 if (direction < 0) {
2559 for (; (end >= start); end--)
2560 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) {
2561 count++;
2562 if (--maxcount <= 0) break;
2563 end -= pattern_len-1;
2564 }
2565 } else {
2566 for (; (start <= end); start++)
2567 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) {
2568 count++;
2569 if (--maxcount <= 0)
2570 break;
2571 start += pattern_len-1;
2572 }
2573 }
2574 return count;
2575}
2576
2577
2578/* Algorithms for different cases of string replacement */
2579
2580/* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
2581Py_LOCAL(PyStringObject *)
2582replace_interleave(PyStringObject *self,
2583 const char *to_s, Py_ssize_t to_len,
2584 Py_ssize_t maxcount)
2585{
2586 char *self_s, *result_s;
2587 Py_ssize_t self_len, result_len;
2588 Py_ssize_t count, i, product;
2589 PyStringObject *result;
2590
2591 self_len = PyString_GET_SIZE(self);
2592
2593 /* 1 at the end plus 1 after every character */
2594 count = self_len+1;
2595 if (maxcount < count)
2596 count = maxcount;
2597
2598 /* Check for overflow */
2599 /* result_len = count * to_len + self_len; */
2600 product = count * to_len;
2601 if (product / to_len != count) {
2602 PyErr_SetString(PyExc_OverflowError,
2603 "replace string is too long");
2604 return NULL;
2605 }
2606 result_len = product + self_len;
2607 if (result_len < 0) {
2608 PyErr_SetString(PyExc_OverflowError,
2609 "replace string is too long");
2610 return NULL;
2611 }
2612
2613 if (! (result = (PyStringObject *)
2614 PyString_FromStringAndSize(NULL, result_len)) )
2615 return NULL;
2616
2617 self_s = PyString_AS_STRING(self);
2618 result_s = PyString_AS_STRING(result);
2619
2620 /* TODO: special case single character, which doesn't need memcpy */
2621
2622 /* Lay the first one down (guaranteed this will occur) */
2623 Py_MEMCPY(result_s, to_s, to_len);
2624 result_s += to_len;
2625 count -= 1;
2626
2627 for (i=0; i<count; i++) {
2628 *result_s++ = *self_s++;
2629 Py_MEMCPY(result_s, to_s, to_len);
2630 result_s += to_len;
2631 }
2632
2633 /* Copy the rest of the original string */
2634 Py_MEMCPY(result_s, self_s, self_len-i);
2635
2636 return result;
2637}
2638
2639/* Special case for deleting a single character */
2640/* len(self)>=1, len(from)==1, to="", maxcount>=1 */
2641Py_LOCAL(PyStringObject *)
2642replace_delete_single_character(PyStringObject *self,
2643 char from_c, Py_ssize_t maxcount)
2644{
2645 char *self_s, *result_s;
2646 char *start, *next, *end;
2647 Py_ssize_t self_len, result_len;
2648 Py_ssize_t count;
2649 PyStringObject *result;
2650
2651 self_len = PyString_GET_SIZE(self);
2652 self_s = PyString_AS_STRING(self);
2653
2654 count = countchar(self_s, self_len, from_c, maxcount);
2655 if (count == 0) {
2656 return return_self(self);
2657 }
2658
2659 result_len = self_len - count; /* from_len == 1 */
2660 assert(result_len>=0);
2661
2662 if ( (result = (PyStringObject *)
2663 PyString_FromStringAndSize(NULL, result_len)) == NULL)
2664 return NULL;
2665 result_s = PyString_AS_STRING(result);
2666
2667 start = self_s;
2668 end = self_s + self_len;
2669 while (count-- > 0) {
2670 next = findchar(start, end-start, from_c);
2671 if (next == NULL)
2672 break;
2673 Py_MEMCPY(result_s, start, next-start);
2674 result_s += (next-start);
2675 start = next+1;
2676 }
2677 Py_MEMCPY(result_s, start, end-start);
2678
2679 return result;
2680}
2681
2682/* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
2683
2684Py_LOCAL(PyStringObject *)
2685replace_delete_substring(PyStringObject *self,
2686 const char *from_s, Py_ssize_t from_len,
2687 Py_ssize_t maxcount) {
2688 char *self_s, *result_s;
2689 char *start, *next, *end;
2690 Py_ssize_t self_len, result_len;
2691 Py_ssize_t count, offset;
2692 PyStringObject *result;
2693
2694 self_len = PyString_GET_SIZE(self);
2695 self_s = PyString_AS_STRING(self);
2696
2697 count = countstring(self_s, self_len,
2698 from_s, from_len,
2699 0, self_len, 1,
2700 maxcount);
2701
2702 if (count == 0) {
2703 /* no matches */
2704 return return_self(self);
2705 }
2706
2707 result_len = self_len - (count * from_len);
2708 assert (result_len>=0);
2709
2710 if ( (result = (PyStringObject *)
2711 PyString_FromStringAndSize(NULL, result_len)) == NULL )
2712 return NULL;
2713
2714 result_s = PyString_AS_STRING(result);
2715
2716 start = self_s;
2717 end = self_s + self_len;
2718 while (count-- > 0) {
2719 offset = findstring(start, end-start,
2720 from_s, from_len,
2721 0, end-start, FORWARD);
2722 if (offset == -1)
2723 break;
2724 next = start + offset;
2725
2726 Py_MEMCPY(result_s, start, next-start);
2727
2728 result_s += (next-start);
2729 start = next+from_len;
2730 }
2731 Py_MEMCPY(result_s, start, end-start);
2732 return result;
2733}
2734
2735/* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
2736Py_LOCAL(PyStringObject *)
2737replace_single_character_in_place(PyStringObject *self,
2738 char from_c, char to_c,
2739 Py_ssize_t maxcount)
2740{
2741 char *self_s, *result_s, *start, *end, *next;
2742 Py_ssize_t self_len;
2743 PyStringObject *result;
2744
2745 /* The result string will be the same size */
2746 self_s = PyString_AS_STRING(self);
2747 self_len = PyString_GET_SIZE(self);
2748
2749 next = findchar(self_s, self_len, from_c);
2750
2751 if (next == NULL) {
2752 /* No matches; return the original string */
2753 return return_self(self);
2754 }
2755
2756 /* Need to make a new string */
2757 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
2758 if (result == NULL)
2759 return NULL;
2760 result_s = PyString_AS_STRING(result);
2761 Py_MEMCPY(result_s, self_s, self_len);
2762
2763 /* change everything in-place, starting with this one */
2764 start = result_s + (next-self_s);
2765 *start = to_c;
2766 start++;
2767 end = result_s + self_len;
2768
2769 while (--maxcount > 0) {
2770 next = findchar(start, end-start, from_c);
2771 if (next == NULL)
2772 break;
2773 *next = to_c;
2774 start = next+1;
2775 }
2776
2777 return result;
2778}
2779
2780/* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
2781Py_LOCAL(PyStringObject *)
2782replace_substring_in_place(PyStringObject *self,
2783 const char *from_s, Py_ssize_t from_len,
2784 const char *to_s, Py_ssize_t to_len,
2785 Py_ssize_t maxcount)
2786{
2787 char *result_s, *start, *end;
2788 char *self_s;
2789 Py_ssize_t self_len, offset;
2790 PyStringObject *result;
2791
2792 /* The result string will be the same size */
2793
2794 self_s = PyString_AS_STRING(self);
2795 self_len = PyString_GET_SIZE(self);
2796
2797 offset = findstring(self_s, self_len,
2798 from_s, from_len,
2799 0, self_len, FORWARD);
2800 if (offset == -1) {
2801 /* No matches; return the original string */
2802 return return_self(self);
2803 }
2804
2805 /* Need to make a new string */
2806 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len);
2807 if (result == NULL)
2808 return NULL;
2809 result_s = PyString_AS_STRING(result);
2810 Py_MEMCPY(result_s, self_s, self_len);
2811
2812 /* change everything in-place, starting with this one */
2813 start = result_s + offset;
2814 Py_MEMCPY(start, to_s, from_len);
2815 start += from_len;
2816 end = result_s + self_len;
2817
2818 while ( --maxcount > 0) {
2819 offset = findstring(start, end-start,
2820 from_s, from_len,
2821 0, end-start, FORWARD);
2822 if (offset==-1)
2823 break;
2824 Py_MEMCPY(start+offset, to_s, from_len);
2825 start += offset+from_len;
2826 }
2827
2828 return result;
2829}
2830
2831/* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
2832Py_LOCAL(PyStringObject *)
2833replace_single_character(PyStringObject *self,
2834 char from_c,
2835 const char *to_s, Py_ssize_t to_len,
2836 Py_ssize_t maxcount)
2837{
2838 char *self_s, *result_s;
2839 char *start, *next, *end;
2840 Py_ssize_t self_len, result_len;
2841 Py_ssize_t count, product;
2842 PyStringObject *result;
2843
2844 self_s = PyString_AS_STRING(self);
2845 self_len = PyString_GET_SIZE(self);
2846
2847 count = countchar(self_s, self_len, from_c, maxcount);
2848 if (count == 0) {
2849 /* no matches, return unchanged */
2850 return return_self(self);
2851 }
2852
2853 /* use the difference between current and new, hence the "-1" */
2854 /* result_len = self_len + count * (to_len-1) */
2855 product = count * (to_len-1);
2856 if (product / (to_len-1) != count) {
2857 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
2858 return NULL;
2859 }
2860 result_len = self_len + product;
2861 if (result_len < 0) {
2862 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
2863 return NULL;
2864 }
2865
2866 if ( (result = (PyStringObject *)
2867 PyString_FromStringAndSize(NULL, result_len)) == NULL)
2868 return NULL;
2869 result_s = PyString_AS_STRING(result);
2870
2871 start = self_s;
2872 end = self_s + self_len;
2873 while (count-- > 0) {
2874 next = findchar(start, end-start, from_c);
2875 if (next == NULL)
2876 break;
2877
2878 if (next == start) {
2879 /* replace with the 'to' */
2880 Py_MEMCPY(result_s, to_s, to_len);
2881 result_s += to_len;
2882 start += 1;
2883 } else {
2884 /* copy the unchanged old then the 'to' */
2885 Py_MEMCPY(result_s, start, next-start);
2886 result_s += (next-start);
2887 Py_MEMCPY(result_s, to_s, to_len);
2888 result_s += to_len;
2889 start = next+1;
2890 }
2891 }
2892 /* Copy the remainder of the remaining string */
2893 Py_MEMCPY(result_s, start, end-start);
2894
2895 return result;
2896}
2897
2898/* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
2899Py_LOCAL(PyStringObject *)
2900replace_substring(PyStringObject *self,
2901 const char *from_s, Py_ssize_t from_len,
2902 const char *to_s, Py_ssize_t to_len,
2903 Py_ssize_t maxcount) {
2904 char *self_s, *result_s;
2905 char *start, *next, *end;
2906 Py_ssize_t self_len, result_len;
2907 Py_ssize_t count, offset, product;
2908 PyStringObject *result;
2909
2910 self_s = PyString_AS_STRING(self);
2911 self_len = PyString_GET_SIZE(self);
2912
2913 count = countstring(self_s, self_len,
2914 from_s, from_len,
2915 0, self_len, FORWARD, maxcount);
2916 if (count == 0) {
2917 /* no matches, return unchanged */
2918 return return_self(self);
2919 }
2920
2921 /* Check for overflow */
2922 /* result_len = self_len + count * (to_len-from_len) */
2923 product = count * (to_len-from_len);
2924 if (product / (to_len-from_len) != count) {
2925 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
2926 return NULL;
2927 }
2928 result_len = self_len + product;
2929 if (result_len < 0) {
2930 PyErr_SetString(PyExc_OverflowError, "replace string is too long");
2931 return NULL;
2932 }
2933
2934 if ( (result = (PyStringObject *)
2935 PyString_FromStringAndSize(NULL, result_len)) == NULL)
2936 return NULL;
2937 result_s = PyString_AS_STRING(result);
2938
2939 start = self_s;
2940 end = self_s + self_len;
2941 while (count-- > 0) {
2942 offset = findstring(start, end-start,
2943 from_s, from_len,
2944 0, end-start, FORWARD);
2945 if (offset == -1)
2946 break;
2947 next = start+offset;
2948 if (next == start) {
2949 /* replace with the 'to' */
2950 Py_MEMCPY(result_s, to_s, to_len);
2951 result_s += to_len;
2952 start += from_len;
2953 } else {
2954 /* copy the unchanged old then the 'to' */
2955 Py_MEMCPY(result_s, start, next-start);
2956 result_s += (next-start);
2957 Py_MEMCPY(result_s, to_s, to_len);
2958 result_s += to_len;
2959 start = next+from_len;
2960 }
2961 }
2962 /* Copy the remainder of the remaining string */
2963 Py_MEMCPY(result_s, start, end-start);
2964
2965 return result;
2966}
2967
2968
2969Py_LOCAL(PyStringObject *)
2970replace(PyStringObject *self,
2971 const char *from_s, Py_ssize_t from_len,
2972 const char *to_s, Py_ssize_t to_len,
2973 Py_ssize_t maxcount)
2974{
2975 if (maxcount < 0) {
2976 maxcount = PY_SSIZE_T_MAX;
2977 } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) {
2978 /* nothing to do; return the original string */
2979 return return_self(self);
2980 }
2981
2982 if (maxcount == 0 ||
2983 (from_len == 0 && to_len == 0)) {
2984 /* nothing to do; return the original string */
2985 return return_self(self);
2986 }
2987
2988 /* Handle zero-length special cases */
2989
2990 if (from_len == 0) {
2991 /* insert the 'to' string everywhere. */
2992 /* >>> "Python".replace("", ".") */
2993 /* '.P.y.t.h.o.n.' */
2994 return replace_interleave(self, to_s, to_len, maxcount);
2995 }
2996
2997 /* Except for "".replace("", "A") == "A" there is no way beyond this */
2998 /* point for an empty self string to generate a non-empty string */
2999 /* Special case so the remaining code always gets a non-empty string */
3000 if (PyString_GET_SIZE(self) == 0) {
3001 return return_self(self);
3002 }
3003
3004 if (to_len == 0) {
3005 /* delete all occurances of 'from' string */
3006 if (from_len == 1) {
3007 return replace_delete_single_character(
3008 self, from_s[0], maxcount);
3009 } else {
3010 return replace_delete_substring(self, from_s, from_len, maxcount);
3011 }
3012 }
3013
3014 /* Handle special case where both strings have the same length */
3015
3016 if (from_len == to_len) {
3017 if (from_len == 1) {
3018 return replace_single_character_in_place(
3019 self,
3020 from_s[0],
3021 to_s[0],
3022 maxcount);
3023 } else {
3024 return replace_substring_in_place(
3025 self, from_s, from_len, to_s, to_len, maxcount);
3026 }
3027 }
3028
3029 /* Otherwise use the more generic algorithms */
3030 if (from_len == 1) {
3031 return replace_single_character(self, from_s[0],
3032 to_s, to_len, maxcount);
3033 } else {
3034 /* len('from')>=2, len('to')>=1 */
3035 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
3036 }
3037}
3038
3039PyDoc_STRVAR(replace__doc__,
3040"S.replace (old, new[, count]) -> string\n\
3041\n\
3042Return a copy of string S with all occurrences of substring\n\
3043old replaced by new. If the optional argument count is\n\
3044given, only the first count occurrences are replaced.");
3045
3046static PyObject *
3047string_replace(PyStringObject *self, PyObject *args)
3048{
3049 Py_ssize_t count = -1;
3050 PyObject *from, *to;
3051 const char *from_s, *to_s;
3052 Py_ssize_t from_len, to_len;
3053
3054 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
3055 return NULL;
3056
3057 if (PyString_Check(from)) {
3058 from_s = PyString_AS_STRING(from);
3059 from_len = PyString_GET_SIZE(from);
3060 }
3061#ifdef Py_USING_UNICODE
3062 if (PyUnicode_Check(from))
3063 return PyUnicode_Replace((PyObject *)self,
3064 from, to, count);
3065#endif
3066 else if (PyObject_AsCharBuffer(from, &from_s, &from_len))
3067 return NULL;
3068
3069 if (PyString_Check(to)) {
3070 to_s = PyString_AS_STRING(to);
3071 to_len = PyString_GET_SIZE(to);
3072 }
3073#ifdef Py_USING_UNICODE
3074 else if (PyUnicode_Check(to))
3075 return PyUnicode_Replace((PyObject *)self,
3076 from, to, count);
3077#endif
3078 else if (PyObject_AsCharBuffer(to, &to_s, &to_len))
3079 return NULL;
3080
3081 return (PyObject *)replace((PyStringObject *) self,
3082 from_s, from_len,
3083 to_s, to_len, count);
3084}
3085
3086/** End DALKE **/
3087
3088/* Matches the end (direction >= 0) or start (direction < 0) of self
3089 * against substr, using the start and end arguments. Returns
3090 * -1 on error, 0 if not found and 1 if found.
3091 */
3092Py_LOCAL(int)
3093_string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start,
3094 Py_ssize_t end, int direction)
3095{
3096 Py_ssize_t len = PyString_GET_SIZE(self);
3097 Py_ssize_t slen;
3098 const char* sub;
3099 const char* str;
3100
3101 if (PyString_Check(substr)) {
3102 sub = PyString_AS_STRING(substr);
3103 slen = PyString_GET_SIZE(substr);
3104 }
3105#ifdef Py_USING_UNICODE
3106 else if (PyUnicode_Check(substr))
3107 return PyUnicode_Tailmatch((PyObject *)self,
3108 substr, start, end, direction);
3109#endif
3110 else if (PyObject_AsCharBuffer(substr, &sub, &slen))
3111 return -1;
3112 str = PyString_AS_STRING(self);
3113
3114 string_adjust_indices(&start, &end, len);
3115
3116 if (direction < 0) {
3117 /* startswith */
3118 if (start+slen > len)
3119 return 0;
3120 } else {
3121 /* endswith */
3122 if (end-start < slen || start > len)
3123 return 0;
3124
3125 if (end-slen > start)
3126 start = end - slen;
3127 }
3128 if (end-start >= slen)
3129 return ! memcmp(str+start, sub, slen);
3130 return 0;
3131}
3132
3133
3134PyDoc_STRVAR(startswith__doc__,
3135"S.startswith(prefix[, start[, end]]) -> bool\n\
3136\n\
3137Return True if S starts with the specified prefix, False otherwise.\n\
3138With optional start, test S beginning at that position.\n\
3139With optional end, stop comparing S at that position.\n\
3140prefix can also be a tuple of strings to try.");
3141
3142static PyObject *
3143string_startswith(PyStringObject *self, PyObject *args)
3144{
3145 Py_ssize_t start = 0;
3146 Py_ssize_t end = PY_SSIZE_T_MAX;
3147 PyObject *subobj;
3148 int result;
3149
3150 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
3151 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
3152 return NULL;
3153 if (PyTuple_Check(subobj)) {
3154 Py_ssize_t i;
3155 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
3156 result = _string_tailmatch(self,
3157 PyTuple_GET_ITEM(subobj, i),
3158 start, end, -1);
3159 if (result == -1)
3160 return NULL;
3161 else if (result) {
3162 Py_RETURN_TRUE;
3163 }
3164 }
3165 Py_RETURN_FALSE;
3166 }
3167 result = _string_tailmatch(self, subobj, start, end, -1);
3168 if (result == -1)
3169 return NULL;
3170 else
3171 return PyBool_FromLong(result);
3172}
3173
3174
3175PyDoc_STRVAR(endswith__doc__,
3176"S.endswith(suffix[, start[, end]]) -> bool\n\
3177\n\
3178Return True if S ends with the specified suffix, False otherwise.\n\
3179With optional start, test S beginning at that position.\n\
3180With optional end, stop comparing S at that position.\n\
3181suffix can also be a tuple of strings to try.");
3182
3183static PyObject *
3184string_endswith(PyStringObject *self, PyObject *args)
3185{
3186 Py_ssize_t start = 0;
3187 Py_ssize_t end = PY_SSIZE_T_MAX;
3188 PyObject *subobj;
3189 int result;
3190
3191 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
3192 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
3193 return NULL;
3194 if (PyTuple_Check(subobj)) {
3195 Py_ssize_t i;
3196 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
3197 result = _string_tailmatch(self,
3198 PyTuple_GET_ITEM(subobj, i),
3199 start, end, +1);
3200 if (result == -1)
3201 return NULL;
3202 else if (result) {
3203 Py_RETURN_TRUE;
3204 }
3205 }
3206 Py_RETURN_FALSE;
3207 }
3208 result = _string_tailmatch(self, subobj, start, end, +1);
3209 if (result == -1)
3210 return NULL;
3211 else
3212 return PyBool_FromLong(result);
3213}
3214
3215
3216PyDoc_STRVAR(encode__doc__,
3217"S.encode([encoding[,errors]]) -> object\n\
3218\n\
3219Encodes S using the codec registered for encoding. encoding defaults\n\
3220to the default encoding. errors may be given to set a different error\n\
3221handling scheme. Default is 'strict' meaning that encoding errors raise\n\
3222a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\
3223'xmlcharrefreplace' as well as any other name registered with\n\
3224codecs.register_error that is able to handle UnicodeEncodeErrors.");
3225
3226static PyObject *
3227string_encode(PyStringObject *self, PyObject *args)
3228{
3229 char *encoding = NULL;
3230 char *errors = NULL;
3231 PyObject *v;
3232
3233 if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
3234 return NULL;
3235 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
3236 if (v == NULL)
3237 goto onError;
3238 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
3239 PyErr_Format(PyExc_TypeError,
3240 "encoder did not return a string/unicode object "
3241 "(type=%.400s)",
3242 v->ob_type->tp_name);
3243 Py_DECREF(v);
3244 return NULL;
3245 }
3246 return v;
3247
3248 onError:
3249 return NULL;
3250}
3251
3252
3253PyDoc_STRVAR(decode__doc__,
3254"S.decode([encoding[,errors]]) -> object\n\
3255\n\
3256Decodes S using the codec registered for encoding. encoding defaults\n\
3257to the default encoding. errors may be given to set a different error\n\
3258handling scheme. Default is 'strict' meaning that encoding errors raise\n\
3259a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
3260as well as any other name registerd with codecs.register_error that is\n\
3261able to handle UnicodeDecodeErrors.");
3262
3263static PyObject *
3264string_decode(PyStringObject *self, PyObject *args)
3265{
3266 char *encoding = NULL;
3267 char *errors = NULL;
3268 PyObject *v;
3269
3270 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
3271 return NULL;
3272 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
3273 if (v == NULL)
3274 goto onError;
3275 if (!PyString_Check(v) && !PyUnicode_Check(v)) {
3276 PyErr_Format(PyExc_TypeError,
3277 "decoder did not return a string/unicode object "
3278 "(type=%.400s)",
3279 v->ob_type->tp_name);
3280 Py_DECREF(v);
3281 return NULL;
3282 }
3283 return v;
3284
3285 onError:
3286 return NULL;
3287}
3288
3289
3290PyDoc_STRVAR(expandtabs__doc__,
3291"S.expandtabs([tabsize]) -> string\n\
3292\n\
3293Return a copy of S where all tab characters are expanded using spaces.\n\
3294If tabsize is not given, a tab size of 8 characters is assumed.");
3295
3296static PyObject*
3297string_expandtabs(PyStringObject *self, PyObject *args)
3298{
3299 const char *e, *p;
3300 char *q;
3301 Py_ssize_t i, j;
3302 PyObject *u;
3303 int tabsize = 8;
3304
3305 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
3306 return NULL;
3307
3308 /* First pass: determine size of output string */
3309 i = j = 0;
3310 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);
3311 for (p = PyString_AS_STRING(self); p < e; p++)
3312 if (*p == '\t') {
3313 if (tabsize > 0)
3314 j += tabsize - (j % tabsize);
3315 }
3316 else {
3317 j++;
3318 if (*p == '\n' || *p == '\r') {
3319 i += j;
3320 j = 0;
3321 }
3322 }
3323
3324 /* Second pass: create output string and fill it */
3325 u = PyString_FromStringAndSize(NULL, i + j);
3326 if (!u)
3327 return NULL;
3328
3329 j = 0;
3330 q = PyString_AS_STRING(u);
3331
3332 for (p = PyString_AS_STRING(self); p < e; p++)
3333 if (*p == '\t') {
3334 if (tabsize > 0) {
3335 i = tabsize - (j % tabsize);
3336 j += i;
3337 while (i--)
3338 *q++ = ' ';
3339 }
3340 }
3341 else {
3342 j++;
3343 *q++ = *p;
3344 if (*p == '\n' || *p == '\r')
3345 j = 0;
3346 }
3347
3348 return u;
3349}
3350
3351Py_LOCAL_INLINE(PyObject *)
3352pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
3353{
3354 PyObject *u;
3355
3356 if (left < 0)
3357 left = 0;
3358 if (right < 0)
3359 right = 0;
3360
3361 if (left == 0 && right == 0 && PyString_CheckExact(self)) {
3362 Py_INCREF(self);
3363 return (PyObject *)self;
3364 }
3365
3366 u = PyString_FromStringAndSize(NULL,
3367 left + PyString_GET_SIZE(self) + right);
3368 if (u) {
3369 if (left)
3370 memset(PyString_AS_STRING(u), fill, left);
3371 Py_MEMCPY(PyString_AS_STRING(u) + left,
3372 PyString_AS_STRING(self),
3373 PyString_GET_SIZE(self));
3374 if (right)
3375 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),
3376 fill, right);
3377 }
3378
3379 return u;
3380}
3381
3382PyDoc_STRVAR(ljust__doc__,
3383"S.ljust(width[, fillchar]) -> string\n"
3384"\n"
3385"Return S left justified in a string of length width. Padding is\n"
3386"done using the specified fill character (default is a space).");
3387
3388static PyObject *
3389string_ljust(PyStringObject *self, PyObject *args)
3390{
3391 Py_ssize_t width;
3392 char fillchar = ' ';
3393
3394 if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar))
3395 return NULL;
3396
3397 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
3398 Py_INCREF(self);
3399 return (PyObject*) self;
3400 }
3401
3402 return pad(self, 0, width - PyString_GET_SIZE(self), fillchar);
3403}
3404
3405
3406PyDoc_STRVAR(rjust__doc__,
3407"S.rjust(width[, fillchar]) -> string\n"
3408"\n"
3409"Return S right justified in a string of length width. Padding is\n"
3410"done using the specified fill character (default is a space)");
3411
3412static PyObject *
3413string_rjust(PyStringObject *self, PyObject *args)
3414{
3415 Py_ssize_t width;
3416 char fillchar = ' ';
3417
3418 if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar))
3419 return NULL;
3420
3421 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
3422 Py_INCREF(self);
3423 return (PyObject*) self;
3424 }
3425
3426 return pad(self, width - PyString_GET_SIZE(self), 0, fillchar);
3427}
3428
3429
3430PyDoc_STRVAR(center__doc__,
3431"S.center(width[, fillchar]) -> string\n"
3432"\n"
3433"Return S centered in a string of length width. Padding is\n"
3434"done using the specified fill character (default is a space)");
3435
3436static PyObject *
3437string_center(PyStringObject *self, PyObject *args)
3438{
3439 Py_ssize_t marg, left;
3440 Py_ssize_t width;
3441 char fillchar = ' ';
3442
3443 if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar))
3444 return NULL;
3445
3446 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) {
3447 Py_INCREF(self);
3448 return (PyObject*) self;
3449 }
3450
3451 marg = width - PyString_GET_SIZE(self);
3452 left = marg / 2 + (marg & width & 1);
3453
3454 return pad(self, left, marg - left, fillchar);
3455}
3456
3457PyDoc_STRVAR(zfill__doc__,
3458"S.zfill(width) -> string\n"
3459"\n"
3460"Pad a numeric string S with zeros on the left, to fill a field\n"
3461"of the specified width. The string S is never truncated.");
3462
3463static PyObject *
3464string_zfill(PyStringObject *self, PyObject *args)
3465{
3466 Py_ssize_t fill;
3467 PyObject *s;
3468 char *p;
3469 Py_ssize_t width;
3470
3471 if (!PyArg_ParseTuple(args, "n:zfill", &width))
3472 return NULL;
3473
3474 if (PyString_GET_SIZE(self) >= width) {
3475 if (PyString_CheckExact(self)) {
3476 Py_INCREF(self);
3477 return (PyObject*) self;
3478 }
3479 else
3480 return PyString_FromStringAndSize(
3481 PyString_AS_STRING(self),
3482 PyString_GET_SIZE(self)
3483 );
3484 }
3485
3486 fill = width - PyString_GET_SIZE(self);
3487
3488 s = pad(self, fill, 0, '0');
3489
3490 if (s == NULL)
3491 return NULL;
3492
3493 p = PyString_AS_STRING(s);
3494 if (p[fill] == '+' || p[fill] == '-') {
3495 /* move sign to beginning of string */
3496 p[0] = p[fill];
3497 p[fill] = '0';
3498 }
3499
3500 return (PyObject*) s;
3501}
3502
3503PyDoc_STRVAR(isspace__doc__,
3504"S.isspace() -> bool\n\
3505\n\
3506Return True if all characters in S are whitespace\n\
3507and there is at least one character in S, False otherwise.");
3508
3509static PyObject*
3510string_isspace(PyStringObject *self)
3511{
3512 register const unsigned char *p
3513 = (unsigned char *) PyString_AS_STRING(self);
3514 register const unsigned char *e;
3515
3516 /* Shortcut for single character strings */
3517 if (PyString_GET_SIZE(self) == 1 &&
3518 isspace(*p))
3519 return PyBool_FromLong(1);
3520
3521 /* Special case for empty strings */
3522 if (PyString_GET_SIZE(self) == 0)
3523 return PyBool_FromLong(0);
3524
3525 e = p + PyString_GET_SIZE(self);
3526 for (; p < e; p++) {
3527 if (!isspace(*p))
3528 return PyBool_FromLong(0);
3529 }
3530 return PyBool_FromLong(1);
3531}
3532
3533
3534PyDoc_STRVAR(isalpha__doc__,
3535"S.isalpha() -> bool\n\
3536\n\
3537Return True if all characters in S are alphabetic\n\
3538and there is at least one character in S, False otherwise.");
3539
3540static PyObject*
3541string_isalpha(PyStringObject *self)
3542{
3543 register const unsigned char *p
3544 = (unsigned char *) PyString_AS_STRING(self);
3545 register const unsigned char *e;
3546
3547 /* Shortcut for single character strings */
3548 if (PyString_GET_SIZE(self) == 1 &&
3549 isalpha(*p))
3550 return PyBool_FromLong(1);
3551
3552 /* Special case for empty strings */
3553 if (PyString_GET_SIZE(self) == 0)
3554 return PyBool_FromLong(0);
3555
3556 e = p + PyString_GET_SIZE(self);
3557 for (; p < e; p++) {
3558 if (!isalpha(*p))
3559 return PyBool_FromLong(0);
3560 }
3561 return PyBool_FromLong(1);
3562}
3563
3564
3565PyDoc_STRVAR(isalnum__doc__,
3566"S.isalnum() -> bool\n\
3567\n\
3568Return True if all characters in S are alphanumeric\n\
3569and there is at least one character in S, False otherwise.");
3570
3571static PyObject*
3572string_isalnum(PyStringObject *self)
3573{
3574 register const unsigned char *p
3575 = (unsigned char *) PyString_AS_STRING(self);
3576 register const unsigned char *e;
3577
3578 /* Shortcut for single character strings */
3579 if (PyString_GET_SIZE(self) == 1 &&
3580 isalnum(*p))
3581 return PyBool_FromLong(1);
3582
3583 /* Special case for empty strings */
3584 if (PyString_GET_SIZE(self) == 0)
3585 return PyBool_FromLong(0);
3586
3587 e = p + PyString_GET_SIZE(self);
3588 for (; p < e; p++) {
3589 if (!isalnum(*p))
3590 return PyBool_FromLong(0);
3591 }
3592 return PyBool_FromLong(1);
3593}
3594
3595
3596PyDoc_STRVAR(isdigit__doc__,
3597"S.isdigit() -> bool\n\
3598\n\
3599Return True if all characters in S are digits\n\
3600and there is at least one character in S, False otherwise.");
3601
3602static PyObject*
3603string_isdigit(PyStringObject *self)
3604{
3605 register const unsigned char *p
3606 = (unsigned char *) PyString_AS_STRING(self);
3607 register const unsigned char *e;
3608
3609 /* Shortcut for single character strings */
3610 if (PyString_GET_SIZE(self) == 1 &&
3611 isdigit(*p))
3612 return PyBool_FromLong(1);
3613
3614 /* Special case for empty strings */
3615 if (PyString_GET_SIZE(self) == 0)
3616 return PyBool_FromLong(0);
3617
3618 e = p + PyString_GET_SIZE(self);
3619 for (; p < e; p++) {
3620 if (!isdigit(*p))
3621 return PyBool_FromLong(0);
3622 }
3623 return PyBool_FromLong(1);
3624}
3625
3626
3627PyDoc_STRVAR(islower__doc__,
3628"S.islower() -> bool\n\
3629\n\
3630Return True if all cased characters in S are lowercase and there is\n\
3631at least one cased character in S, False otherwise.");
3632
3633static PyObject*
3634string_islower(PyStringObject *self)
3635{
3636 register const unsigned char *p
3637 = (unsigned char *) PyString_AS_STRING(self);
3638 register const unsigned char *e;
3639 int cased;
3640
3641 /* Shortcut for single character strings */
3642 if (PyString_GET_SIZE(self) == 1)
3643 return PyBool_FromLong(islower(*p) != 0);
3644
3645 /* Special case for empty strings */
3646 if (PyString_GET_SIZE(self) == 0)
3647 return PyBool_FromLong(0);
3648
3649 e = p + PyString_GET_SIZE(self);
3650 cased = 0;
3651 for (; p < e; p++) {
3652 if (isupper(*p))
3653 return PyBool_FromLong(0);
3654 else if (!cased && islower(*p))
3655 cased = 1;
3656 }
3657 return PyBool_FromLong(cased);
3658}
3659
3660
3661PyDoc_STRVAR(isupper__doc__,
3662"S.isupper() -> bool\n\
3663\n\
3664Return True if all cased characters in S are uppercase and there is\n\
3665at least one cased character in S, False otherwise.");
3666
3667static PyObject*
3668string_isupper(PyStringObject *self)
3669{
3670 register const unsigned char *p
3671 = (unsigned char *) PyString_AS_STRING(self);
3672 register const unsigned char *e;
3673 int cased;
3674
3675 /* Shortcut for single character strings */
3676 if (PyString_GET_SIZE(self) == 1)
3677 return PyBool_FromLong(isupper(*p) != 0);
3678
3679 /* Special case for empty strings */
3680 if (PyString_GET_SIZE(self) == 0)
3681 return PyBool_FromLong(0);
3682
3683 e = p + PyString_GET_SIZE(self);
3684 cased = 0;
3685 for (; p < e; p++) {
3686 if (islower(*p))
3687 return PyBool_FromLong(0);
3688 else if (!cased && isupper(*p))
3689 cased = 1;
3690 }
3691 return PyBool_FromLong(cased);
3692}
3693
3694
3695PyDoc_STRVAR(istitle__doc__,
3696"S.istitle() -> bool\n\
3697\n\
3698Return True if S is a titlecased string and there is at least one\n\
3699character in S, i.e. uppercase characters may only follow uncased\n\
3700characters and lowercase characters only cased ones. Return False\n\
3701otherwise.");
3702
3703static PyObject*
3704string_istitle(PyStringObject *self, PyObject *uncased)
3705{
3706 register const unsigned char *p
3707 = (unsigned char *) PyString_AS_STRING(self);
3708 register const unsigned char *e;
3709 int cased, previous_is_cased;
3710
3711 /* Shortcut for single character strings */
3712 if (PyString_GET_SIZE(self) == 1)
3713 return PyBool_FromLong(isupper(*p) != 0);
3714
3715 /* Special case for empty strings */
3716 if (PyString_GET_SIZE(self) == 0)
3717 return PyBool_FromLong(0);
3718
3719 e = p + PyString_GET_SIZE(self);
3720 cased = 0;
3721 previous_is_cased = 0;
3722 for (; p < e; p++) {
3723 register const unsigned char ch = *p;
3724
3725 if (isupper(ch)) {
3726 if (previous_is_cased)
3727 return PyBool_FromLong(0);
3728 previous_is_cased = 1;
3729 cased = 1;
3730 }
3731 else if (islower(ch)) {
3732 if (!previous_is_cased)
3733 return PyBool_FromLong(0);
3734 previous_is_cased = 1;
3735 cased = 1;
3736 }
3737 else
3738 previous_is_cased = 0;
3739 }
3740 return PyBool_FromLong(cased);
3741}
3742
3743
3744PyDoc_STRVAR(splitlines__doc__,
3745"S.splitlines([keepends]) -> list of strings\n\
3746\n\
3747Return a list of the lines in S, breaking at line boundaries.\n\
3748Line breaks are not included in the resulting list unless keepends\n\
3749is given and true.");
3750
3751static PyObject*
3752string_splitlines(PyStringObject *self, PyObject *args)
3753{
3754 register Py_ssize_t i;
3755 register Py_ssize_t j;
3756 Py_ssize_t len;
3757 int keepends = 0;
3758 PyObject *list;
3759 PyObject *str;
3760 char *data;
3761
3762 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
3763 return NULL;
3764
3765 data = PyString_AS_STRING(self);
3766 len = PyString_GET_SIZE(self);
3767
3768 /* This does not use the preallocated list because splitlines is
3769 usually run with hundreds of newlines. The overhead of
3770 switching between PyList_SET_ITEM and append causes about a
3771 2-3% slowdown for that common case. A smarter implementation
3772 could move the if check out, so the SET_ITEMs are done first
3773 and the appends only done when the prealloc buffer is full.
3774 That's too much work for little gain.*/
3775
3776 list = PyList_New(0);
3777 if (!list)
3778 goto onError;
3779
3780 for (i = j = 0; i < len; ) {
3781 Py_ssize_t eol;
3782
3783 /* Find a line and append it */
3784 while (i < len && data[i] != '\n' && data[i] != '\r')
3785 i++;
3786
3787 /* Skip the line break reading CRLF as one line break */
3788 eol = i;
3789 if (i < len) {
3790 if (data[i] == '\r' && i + 1 < len &&
3791 data[i+1] == '\n')
3792 i += 2;
3793 else
3794 i++;
3795 if (keepends)
3796 eol = i;
3797 }
3798 SPLIT_APPEND(data, j, eol);
3799 j = i;
3800 }
3801 if (j < len) {
3802 SPLIT_APPEND(data, j, len);
3803 }
3804
3805 return list;
3806
3807 onError:
3808 Py_XDECREF(list);
3809 return NULL;
3810}
3811
3812#undef SPLIT_APPEND
3813#undef SPLIT_ADD
3814#undef MAX_PREALLOC
3815#undef PREALLOC_SIZE
3816
3817static PyObject *
3818string_getnewargs(PyStringObject *v)
3819{
3820 return Py_BuildValue("(s#)", v->ob_sval, v->ob_size);
3821}
3822
3823
3824static PyMethodDef
3825string_methods[] = {
3826 /* Counterparts of the obsolete stropmodule functions; except
3827 string.maketrans(). */
3828 {"join", (PyCFunction)string_join, METH_O, join__doc__},
3829 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__},
3830 {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__},
3831 {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__},
3832 {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__},
3833 {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__},
3834 {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__},
3835 {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__},
3836 {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__},
3837 {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__},
3838 {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
3839 {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
3840 {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS,
3841 capitalize__doc__},
3842 {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
3843 {"endswith", (PyCFunction)string_endswith, METH_VARARGS,
3844 endswith__doc__},
3845 {"partition", (PyCFunction)string_partition, METH_O, partition__doc__},
3846 {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__},
3847 {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__},
3848 {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__},
3849 {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__},
3850 {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__},
3851 {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__},
3852 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__},
3853 {"rpartition", (PyCFunction)string_rpartition, METH_O,
3854 rpartition__doc__},
3855 {"startswith", (PyCFunction)string_startswith, METH_VARARGS,
3856 startswith__doc__},
3857 {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__},
3858 {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS,
3859 swapcase__doc__},
3860 {"translate", (PyCFunction)string_translate, METH_VARARGS,
3861 translate__doc__},
3862 {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__},
3863 {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__},
3864 {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__},
3865 {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__},
3866 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__},
3867 {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__},
3868 {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__},
3869 {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS,
3870 expandtabs__doc__},
3871 {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
3872 splitlines__doc__},
3873 {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS},
3874 {NULL, NULL} /* sentinel */
3875};
3876
3877static PyObject *
3878str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
3879
3880static PyObject *
3881string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3882{
3883 PyObject *x = NULL;
3884 static char *kwlist[] = {"object", 0};
3885
3886 if (type != &PyString_Type)
3887 return str_subtype_new(type, args, kwds);
3888 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
3889 return NULL;
3890 if (x == NULL)
3891 return PyString_FromString("");
3892 return PyObject_Str(x);
3893}
3894
3895static PyObject *
3896str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3897{
3898 PyObject *tmp, *pnew;
3899 Py_ssize_t n;
3900
3901 assert(PyType_IsSubtype(type, &PyString_Type));
3902 tmp = string_new(&PyString_Type, args, kwds);
3903 if (tmp == NULL)
3904 return NULL;
3905 assert(PyString_CheckExact(tmp));
3906 n = PyString_GET_SIZE(tmp);
3907 pnew = type->tp_alloc(type, n);
3908 if (pnew != NULL) {
3909 Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1);
3910 ((PyStringObject *)pnew)->ob_shash =
3911 ((PyStringObject *)tmp)->ob_shash;
3912 ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED;
3913 }
3914 Py_DECREF(tmp);
3915 return pnew;
3916}
3917
3918static PyObject *
3919basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3920{
3921 PyErr_SetString(PyExc_TypeError,
3922 "The basestring type cannot be instantiated");
3923 return NULL;
3924}
3925
3926static PyObject *
3927string_mod(PyObject *v, PyObject *w)
3928{
3929 if (!PyString_Check(v)) {
3930 Py_INCREF(Py_NotImplemented);
3931 return Py_NotImplemented;
3932 }
3933 return PyString_Format(v, w);
3934}
3935
3936PyDoc_STRVAR(basestring_doc,
3937"Type basestring cannot be instantiated; it is the base for str and unicode.");
3938
3939static PyNumberMethods string_as_number = {
3940 0, /*nb_add*/
3941 0, /*nb_subtract*/
3942 0, /*nb_multiply*/
3943 0, /*nb_divide*/
3944 string_mod, /*nb_remainder*/
3945};
3946
3947
3948PyTypeObject PyBaseString_Type = {
3949 PyObject_HEAD_INIT(&PyType_Type)
3950 0,
3951 "basestring",
3952 0,
3953 0,
3954 0, /* tp_dealloc */
3955 0, /* tp_print */
3956 0, /* tp_getattr */
3957 0, /* tp_setattr */
3958 0, /* tp_compare */
3959 0, /* tp_repr */
3960 0, /* tp_as_number */
3961 0, /* tp_as_sequence */
3962 0, /* tp_as_mapping */
3963 0, /* tp_hash */
3964 0, /* tp_call */
3965 0, /* tp_str */
3966 0, /* tp_getattro */
3967 0, /* tp_setattro */
3968 0, /* tp_as_buffer */
3969 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3970 basestring_doc, /* tp_doc */
3971 0, /* tp_traverse */
3972 0, /* tp_clear */
3973 0, /* tp_richcompare */
3974 0, /* tp_weaklistoffset */
3975 0, /* tp_iter */
3976 0, /* tp_iternext */
3977 0, /* tp_methods */
3978 0, /* tp_members */
3979 0, /* tp_getset */
3980 &PyBaseObject_Type, /* tp_base */
3981 0, /* tp_dict */
3982 0, /* tp_descr_get */
3983 0, /* tp_descr_set */
3984 0, /* tp_dictoffset */
3985 0, /* tp_init */
3986 0, /* tp_alloc */
3987 basestring_new, /* tp_new */
3988 0, /* tp_free */
3989};
3990
3991PyDoc_STRVAR(string_doc,
3992"str(object) -> string\n\
3993\n\
3994Return a nice string representation of the object.\n\
3995If the argument is a string, the return value is the same object.");
3996
3997PyTypeObject PyString_Type = {
3998 PyObject_HEAD_INIT(&PyType_Type)
3999 0,
4000 "str",
4001 sizeof(PyStringObject),
4002 sizeof(char),
4003 string_dealloc, /* tp_dealloc */
4004 (printfunc)string_print, /* tp_print */
4005 0, /* tp_getattr */
4006 0, /* tp_setattr */
4007 0, /* tp_compare */
4008 string_repr, /* tp_repr */
4009 &string_as_number, /* tp_as_number */
4010 &string_as_sequence, /* tp_as_sequence */
4011 &string_as_mapping, /* tp_as_mapping */
4012 (hashfunc)string_hash, /* tp_hash */
4013 0, /* tp_call */
4014 string_str, /* tp_str */
4015 PyObject_GenericGetAttr, /* tp_getattro */
4016 0, /* tp_setattro */
4017 &string_as_buffer, /* tp_as_buffer */
4018 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
4019 Py_TPFLAGS_BASETYPE, /* tp_flags */
4020 string_doc, /* tp_doc */
4021 0, /* tp_traverse */
4022 0, /* tp_clear */
4023 (richcmpfunc)string_richcompare, /* tp_richcompare */
4024 0, /* tp_weaklistoffset */
4025 0, /* tp_iter */
4026 0, /* tp_iternext */
4027 string_methods, /* tp_methods */
4028 0, /* tp_members */
4029 0, /* tp_getset */
4030 &PyBaseString_Type, /* tp_base */
4031 0, /* tp_dict */
4032 0, /* tp_descr_get */
4033 0, /* tp_descr_set */
4034 0, /* tp_dictoffset */
4035 0, /* tp_init */
4036 0, /* tp_alloc */
4037 string_new, /* tp_new */
4038 PyObject_Del, /* tp_free */
4039};
4040
4041void
4042PyString_Concat(register PyObject **pv, register PyObject *w)
4043{
4044 register PyObject *v;
4045 if (*pv == NULL)
4046 return;
4047 if (w == NULL || !PyString_Check(*pv)) {
4048 Py_DECREF(*pv);
4049 *pv = NULL;
4050 return;
4051 }
4052 v = string_concat((PyStringObject *) *pv, w);
4053 Py_DECREF(*pv);
4054 *pv = v;
4055}
4056
4057void
4058PyString_ConcatAndDel(register PyObject **pv, register PyObject *w)
4059{
4060 PyString_Concat(pv, w);
4061 Py_XDECREF(w);
4062}
4063
4064
4065/* The following function breaks the notion that strings are immutable:
4066 it changes the size of a string. We get away with this only if there
4067 is only one module referencing the object. You can also think of it
4068 as creating a new string object and destroying the old one, only
4069 more efficiently. In any case, don't use this if the string may
4070 already be known to some other part of the code...
4071 Note that if there's not enough memory to resize the string, the original
4072 string object at *pv is deallocated, *pv is set to NULL, an "out of
4073 memory" exception is set, and -1 is returned. Else (on success) 0 is
4074 returned, and the value in *pv may or may not be the same as on input.
4075 As always, an extra byte is allocated for a trailing \0 byte (newsize
4076 does *not* include that), and a trailing \0 byte is stored.
4077*/
4078
4079int
4080_PyString_Resize(PyObject **pv, Py_ssize_t newsize)
4081{
4082 register PyObject *v;
4083 register PyStringObject *sv;
4084 v = *pv;
4085 if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 ||
4086 PyString_CHECK_INTERNED(v)) {
4087 *pv = 0;
4088 Py_DECREF(v);
4089 PyErr_BadInternalCall();
4090 return -1;
4091 }
4092 /* XXX UNREF/NEWREF interface should be more symmetrical */
4093 _Py_DEC_REFTOTAL;
4094 _Py_ForgetReference(v);
4095 *pv = (PyObject *)
4096 PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize);
4097 if (*pv == NULL) {
4098 PyObject_Del(v);
4099 PyErr_NoMemory();
4100 return -1;
4101 }
4102 _Py_NewReference(*pv);
4103 sv = (PyStringObject *) *pv;
4104 sv->ob_size = newsize;
4105 sv->ob_sval[newsize] = '\0';
4106 sv->ob_shash = -1; /* invalidate cached hash value */
4107 return 0;
4108}
4109
4110/* Helpers for formatstring */
4111
4112Py_LOCAL_INLINE(PyObject *)
4113getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
4114{
4115 Py_ssize_t argidx = *p_argidx;
4116 if (argidx < arglen) {
4117 (*p_argidx)++;
4118 if (arglen < 0)
4119 return args;
4120 else
4121 return PyTuple_GetItem(args, argidx);
4122 }
4123 PyErr_SetString(PyExc_TypeError,
4124 "not enough arguments for format string");
4125 return NULL;
4126}
4127
4128/* Format codes
4129 * F_LJUST '-'
4130 * F_SIGN '+'
4131 * F_BLANK ' '
4132 * F_ALT '#'
4133 * F_ZERO '0'
4134 */
4135#define F_LJUST (1<<0)
4136#define F_SIGN (1<<1)
4137#define F_BLANK (1<<2)
4138#define F_ALT (1<<3)
4139#define F_ZERO (1<<4)
4140
4141Py_LOCAL_INLINE(int)
4142formatfloat(char *buf, size_t buflen, int flags,
4143 int prec, int type, PyObject *v)
4144{
4145 /* fmt = '%#.' + `prec` + `type`
4146 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
4147 char fmt[20];
4148 double x;
4149 x = PyFloat_AsDouble(v);
4150 if (x == -1.0 && PyErr_Occurred()) {
4151 PyErr_SetString(PyExc_TypeError, "float argument required");
4152 return -1;
4153 }
4154 if (prec < 0)
4155 prec = 6;
4156 if (type == 'f' && fabs(x)/1e25 >= 1e25)
4157 type = 'g';
4158 /* Worst case length calc to ensure no buffer overrun:
4159
4160 'g' formats:
4161 fmt = %#.<prec>g
4162 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
4163 for any double rep.)
4164 len = 1 + prec + 1 + 2 + 5 = 9 + prec
4165
4166 'f' formats:
4167 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
4168 len = 1 + 50 + 1 + prec = 52 + prec
4169
4170 If prec=0 the effective precision is 1 (the leading digit is
4171 always given), therefore increase the length by one.
4172
4173 */
4174 if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) ||
4175 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
4176 PyErr_SetString(PyExc_OverflowError,
4177 "formatted float is too long (precision too large?)");
4178 return -1;
4179 }
4180 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
4181 (flags&F_ALT) ? "#" : "",
4182 prec, type);
4183 PyOS_ascii_formatd(buf, buflen, fmt, x);
4184 return (int)strlen(buf);
4185}
4186
4187/* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
4188 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
4189 * Python's regular ints.
4190 * Return value: a new PyString*, or NULL if error.
4191 * . *pbuf is set to point into it,
4192 * *plen set to the # of chars following that.
4193 * Caller must decref it when done using pbuf.
4194 * The string starting at *pbuf is of the form
4195 * "-"? ("0x" | "0X")? digit+
4196 * "0x"/"0X" are present only for x and X conversions, with F_ALT
4197 * set in flags. The case of hex digits will be correct,
4198 * There will be at least prec digits, zero-filled on the left if
4199 * necessary to get that many.
4200 * val object to be converted
4201 * flags bitmask of format flags; only F_ALT is looked at
4202 * prec minimum number of digits; 0-fill on left if needed
4203 * type a character in [duoxX]; u acts the same as d
4204 *
4205 * CAUTION: o, x and X conversions on regular ints can never
4206 * produce a '-' sign, but can for Python's unbounded ints.
4207 */
4208PyObject*
4209_PyString_FormatLong(PyObject *val, int flags, int prec, int type,
4210 char **pbuf, int *plen)
4211{
4212 PyObject *result = NULL;
4213 char *buf;
4214 Py_ssize_t i;
4215 int sign; /* 1 if '-', else 0 */
4216 int len; /* number of characters */
4217 Py_ssize_t llen;
4218 int numdigits; /* len == numnondigits + numdigits */
4219 int numnondigits = 0;
4220
4221 switch (type) {
4222 case 'd':
4223 case 'u':
4224 result = val->ob_type->tp_str(val);
4225 break;
4226 case 'o':
4227 result = val->ob_type->tp_as_number->nb_oct(val);
4228 break;
4229 case 'x':
4230 case 'X':
4231 numnondigits = 2;
4232 result = val->ob_type->tp_as_number->nb_hex(val);
4233 break;
4234 default:
4235 assert(!"'type' not in [duoxX]");
4236 }
4237 if (!result)
4238 return NULL;
4239
4240 buf = PyString_AsString(result);
4241 if (!buf) {
4242 Py_DECREF(result);
4243 return NULL;
4244 }
4245
4246 /* To modify the string in-place, there can only be one reference. */
4247 if (result->ob_refcnt != 1) {
4248 PyErr_BadInternalCall();
4249 return NULL;
4250 }
4251 llen = PyString_Size(result);
4252 if (llen > INT_MAX) {
4253 PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong");
4254 return NULL;
4255 }
4256 len = (int)llen;
4257 if (buf[len-1] == 'L') {
4258 --len;
4259 buf[len] = '\0';
4260 }
4261 sign = buf[0] == '-';
4262 numnondigits += sign;
4263 numdigits = len - numnondigits;
4264 assert(numdigits > 0);
4265
4266 /* Get rid of base marker unless F_ALT */
4267 if ((flags & F_ALT) == 0) {
4268 /* Need to skip 0x, 0X or 0. */
4269 int skipped = 0;
4270 switch (type) {
4271 case 'o':
4272 assert(buf[sign] == '0');
4273 /* If 0 is only digit, leave it alone. */
4274 if (numdigits > 1) {
4275 skipped = 1;
4276 --numdigits;
4277 }
4278 break;
4279 case 'x':
4280 case 'X':
4281 assert(buf[sign] == '0');
4282 assert(buf[sign + 1] == 'x');
4283 skipped = 2;
4284 numnondigits -= 2;
4285 break;
4286 }
4287 if (skipped) {
4288 buf += skipped;
4289 len -= skipped;
4290 if (sign)
4291 buf[0] = '-';
4292 }
4293 assert(len == numnondigits + numdigits);
4294 assert(numdigits > 0);
4295 }
4296
4297 /* Fill with leading zeroes to meet minimum width. */
4298 if (prec > numdigits) {
4299 PyObject *r1 = PyString_FromStringAndSize(NULL,
4300 numnondigits + prec);
4301 char *b1;
4302 if (!r1) {
4303 Py_DECREF(result);
4304 return NULL;
4305 }
4306 b1 = PyString_AS_STRING(r1);
4307 for (i = 0; i < numnondigits; ++i)
4308 *b1++ = *buf++;
4309 for (i = 0; i < prec - numdigits; i++)
4310 *b1++ = '0';
4311 for (i = 0; i < numdigits; i++)
4312 *b1++ = *buf++;
4313 *b1 = '\0';
4314 Py_DECREF(result);
4315 result = r1;
4316 buf = PyString_AS_STRING(result);
4317 len = numnondigits + prec;
4318 }
4319
4320 /* Fix up case for hex conversions. */
4321 if (type == 'X') {
4322 /* Need to convert all lower case letters to upper case.
4323 and need to convert 0x to 0X (and -0x to -0X). */
4324 for (i = 0; i < len; i++)
4325 if (buf[i] >= 'a' && buf[i] <= 'x')
4326 buf[i] -= 'a'-'A';
4327 }
4328 *pbuf = buf;
4329 *plen = len;
4330 return result;
4331}
4332
4333Py_LOCAL_INLINE(int)
4334formatint(char *buf, size_t buflen, int flags,
4335 int prec, int type, PyObject *v)
4336{
4337 /* fmt = '%#.' + `prec` + 'l' + `type`
4338 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
4339 + 1 + 1 = 24 */
4340 char fmt[64]; /* plenty big enough! */
4341 char *sign;
4342 long x;
4343
4344 x = PyInt_AsLong(v);
4345 if (x == -1 && PyErr_Occurred()) {
4346 PyErr_SetString(PyExc_TypeError, "int argument required");
4347 return -1;
4348 }
4349 if (x < 0 && type == 'u') {
4350 type = 'd';
4351 }
4352 if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
4353 sign = "-";
4354 else
4355 sign = "";
4356 if (prec < 0)
4357 prec = 1;
4358
4359 if ((flags & F_ALT) &&
4360 (type == 'x' || type == 'X')) {
4361 /* When converting under %#x or %#X, there are a number
4362 * of issues that cause pain:
4363 * - when 0 is being converted, the C standard leaves off
4364 * the '0x' or '0X', which is inconsistent with other
4365 * %#x/%#X conversions and inconsistent with Python's
4366 * hex() function
4367 * - there are platforms that violate the standard and
4368 * convert 0 with the '0x' or '0X'
4369 * (Metrowerks, Compaq Tru64)
4370 * - there are platforms that give '0x' when converting
4371 * under %#X, but convert 0 in accordance with the
4372 * standard (OS/2 EMX)
4373 *
4374 * We can achieve the desired consistency by inserting our
4375 * own '0x' or '0X' prefix, and substituting %x/%X in place
4376 * of %#x/%#X.
4377 *
4378 * Note that this is the same approach as used in
4379 * formatint() in unicodeobject.c
4380 */
4381 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
4382 sign, type, prec, type);
4383 }
4384 else {
4385 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
4386 sign, (flags&F_ALT) ? "#" : "",
4387 prec, type);
4388 }
4389
4390 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
4391 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
4392 */
4393 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
4394 PyErr_SetString(PyExc_OverflowError,
4395 "formatted integer is too long (precision too large?)");
4396 return -1;
4397 }
4398 if (sign[0])
4399 PyOS_snprintf(buf, buflen, fmt, -x);
4400 else
4401 PyOS_snprintf(buf, buflen, fmt, x);
4402 return (int)strlen(buf);
4403}
4404
4405Py_LOCAL_INLINE(int)
4406formatchar(char *buf, size_t buflen, PyObject *v)
4407{
4408 /* presume that the buffer is at least 2 characters long */
4409 if (PyString_Check(v)) {
4410 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
4411 return -1;
4412 }
4413 else {
4414 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
4415 return -1;
4416 }
4417 buf[1] = '\0';
4418 return 1;
4419}
4420
4421/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
4422
4423 FORMATBUFLEN is the length of the buffer in which the floats, ints, &
4424 chars are formatted. XXX This is a magic number. Each formatting
4425 routine does bounds checking to ensure no overflow, but a better
4426 solution may be to malloc a buffer of appropriate size for each
4427 format. For now, the current solution is sufficient.
4428*/
4429#define FORMATBUFLEN (size_t)120
4430
4431PyObject *
4432PyString_Format(PyObject *format, PyObject *args)
4433{
4434 char *fmt, *res;
4435 Py_ssize_t arglen, argidx;
4436 Py_ssize_t reslen, rescnt, fmtcnt;
4437 int args_owned = 0;
4438 PyObject *result, *orig_args;
4439#ifdef Py_USING_UNICODE
4440 PyObject *v, *w;
4441#endif
4442 PyObject *dict = NULL;
4443 if (format == NULL || !PyString_Check(format) || args == NULL) {
4444 PyErr_BadInternalCall();
4445 return NULL;
4446 }
4447 orig_args = args;
4448 fmt = PyString_AS_STRING(format);
4449 fmtcnt = PyString_GET_SIZE(format);
4450 reslen = rescnt = fmtcnt + 100;
4451 result = PyString_FromStringAndSize((char *)NULL, reslen);
4452 if (result == NULL)
4453 return NULL;
4454 res = PyString_AsString(result);
4455 if (PyTuple_Check(args)) {
4456 arglen = PyTuple_GET_SIZE(args);
4457 argidx = 0;
4458 }
4459 else {
4460 arglen = -1;
4461 argidx = -2;
4462 }
4463 if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) &&
4464 !PyObject_TypeCheck(args, &PyBaseString_Type))
4465 dict = args;
4466 while (--fmtcnt >= 0) {
4467 if (*fmt != '%') {
4468 if (--rescnt < 0) {
4469 rescnt = fmtcnt + 100;
4470 reslen += rescnt;
4471 if (_PyString_Resize(&result, reslen) < 0)
4472 return NULL;
4473 res = PyString_AS_STRING(result)
4474 + reslen - rescnt;
4475 --rescnt;
4476 }
4477 *res++ = *fmt++;
4478 }
4479 else {
4480 /* Got a format specifier */
4481 int flags = 0;
4482 Py_ssize_t width = -1;
4483 int prec = -1;
4484 int c = '\0';
4485 int fill;
4486 PyObject *v = NULL;
4487 PyObject *temp = NULL;
4488 char *pbuf;
4489 int sign;
4490 Py_ssize_t len;
4491 char formatbuf[FORMATBUFLEN];
4492 /* For format{float,int,char}() */
4493#ifdef Py_USING_UNICODE
4494 char *fmt_start = fmt;
4495 Py_ssize_t argidx_start = argidx;
4496#endif
4497
4498 fmt++;
4499 if (*fmt == '(') {
4500 char *keystart;
4501 Py_ssize_t keylen;
4502 PyObject *key;
4503 int pcount = 1;
4504
4505 if (dict == NULL) {
4506 PyErr_SetString(PyExc_TypeError,
4507 "format requires a mapping");
4508 goto error;
4509 }
4510 ++fmt;
4511 --fmtcnt;
4512 keystart = fmt;
4513 /* Skip over balanced parentheses */
4514 while (pcount > 0 && --fmtcnt >= 0) {
4515 if (*fmt == ')')
4516 --pcount;
4517 else if (*fmt == '(')
4518 ++pcount;
4519 fmt++;
4520 }
4521 keylen = fmt - keystart - 1;
4522 if (fmtcnt < 0 || pcount > 0) {
4523 PyErr_SetString(PyExc_ValueError,
4524 "incomplete format key");
4525 goto error;
4526 }
4527 key = PyString_FromStringAndSize(keystart,
4528 keylen);
4529 if (key == NULL)
4530 goto error;
4531 if (args_owned) {
4532 Py_DECREF(args);
4533 args_owned = 0;
4534 }
4535 args = PyObject_GetItem(dict, key);
4536 Py_DECREF(key);
4537 if (args == NULL) {
4538 goto error;
4539 }
4540 args_owned = 1;
4541 arglen = -1;
4542 argidx = -2;
4543 }
4544 while (--fmtcnt >= 0) {
4545 switch (c = *fmt++) {
4546 case '-': flags |= F_LJUST; continue;
4547 case '+': flags |= F_SIGN; continue;
4548 case ' ': flags |= F_BLANK; continue;
4549 case '#': flags |= F_ALT; continue;
4550 case '0': flags |= F_ZERO; continue;
4551 }
4552 break;
4553 }
4554 if (c == '*') {
4555 v = getnextarg(args, arglen, &argidx);
4556 if (v == NULL)
4557 goto error;
4558 if (!PyInt_Check(v)) {
4559 PyErr_SetString(PyExc_TypeError,
4560 "* wants int");
4561 goto error;
4562 }
4563 width = PyInt_AsLong(v);
4564 if (width < 0) {
4565 flags |= F_LJUST;
4566 width = -width;
4567 }
4568 if (--fmtcnt >= 0)
4569 c = *fmt++;
4570 }
4571 else if (c >= 0 && isdigit(c)) {
4572 width = c - '0';
4573 while (--fmtcnt >= 0) {
4574 c = Py_CHARMASK(*fmt++);
4575 if (!isdigit(c))
4576 break;
4577 if ((width*10) / 10 != width) {
4578 PyErr_SetString(
4579 PyExc_ValueError,
4580 "width too big");
4581 goto error;
4582 }
4583 width = width*10 + (c - '0');
4584 }
4585 }
4586 if (c == '.') {
4587 prec = 0;
4588 if (--fmtcnt >= 0)
4589 c = *fmt++;
4590 if (c == '*') {
4591 v = getnextarg(args, arglen, &argidx);
4592 if (v == NULL)
4593 goto error;
4594 if (!PyInt_Check(v)) {
4595 PyErr_SetString(
4596 PyExc_TypeError,
4597 "* wants int");
4598 goto error;
4599 }
4600 prec = PyInt_AsLong(v);
4601 if (prec < 0)
4602 prec = 0;
4603 if (--fmtcnt >= 0)
4604 c = *fmt++;
4605 }
4606 else if (c >= 0 && isdigit(c)) {
4607 prec = c - '0';
4608 while (--fmtcnt >= 0) {
4609 c = Py_CHARMASK(*fmt++);
4610 if (!isdigit(c))
4611 break;
4612 if ((prec*10) / 10 != prec) {
4613 PyErr_SetString(
4614 PyExc_ValueError,
4615 "prec too big");
4616 goto error;
4617 }
4618 prec = prec*10 + (c - '0');
4619 }
4620 }
4621 } /* prec */
4622 if (fmtcnt >= 0) {
4623 if (c == 'h' || c == 'l' || c == 'L') {
4624 if (--fmtcnt >= 0)
4625 c = *fmt++;
4626 }
4627 }
4628 if (fmtcnt < 0) {
4629 PyErr_SetString(PyExc_ValueError,
4630 "incomplete format");
4631 goto error;
4632 }
4633 if (c != '%') {
4634 v = getnextarg(args, arglen, &argidx);
4635 if (v == NULL)
4636 goto error;
4637 }
4638 sign = 0;
4639 fill = ' ';
4640 switch (c) {
4641 case '%':
4642 pbuf = "%";
4643 len = 1;
4644 break;
4645 case 's':
4646#ifdef Py_USING_UNICODE
4647 if (PyUnicode_Check(v)) {
4648 fmt = fmt_start;
4649 argidx = argidx_start;
4650 goto unicode;
4651 }
4652#endif
4653 temp = _PyObject_Str(v);
4654#ifdef Py_USING_UNICODE
4655 if (temp != NULL && PyUnicode_Check(temp)) {
4656 Py_DECREF(temp);
4657 fmt = fmt_start;
4658 argidx = argidx_start;
4659 goto unicode;
4660 }
4661#endif
4662 /* Fall through */
4663 case 'r':
4664 if (c == 'r')
4665 temp = PyObject_Repr(v);
4666 if (temp == NULL)
4667 goto error;
4668 if (!PyString_Check(temp)) {
4669 PyErr_SetString(PyExc_TypeError,
4670 "%s argument has non-string str()");
4671 Py_DECREF(temp);
4672 goto error;
4673 }
4674 pbuf = PyString_AS_STRING(temp);
4675 len = PyString_GET_SIZE(temp);
4676 if (prec >= 0 && len > prec)
4677 len = prec;
4678 break;
4679 case 'i':
4680 case 'd':
4681 case 'u':
4682 case 'o':
4683 case 'x':
4684 case 'X':
4685 if (c == 'i')
4686 c = 'd';
4687 if (PyLong_Check(v)) {
4688 int ilen;
4689 temp = _PyString_FormatLong(v, flags,
4690 prec, c, &pbuf, &ilen);
4691 len = ilen;
4692 if (!temp)
4693 goto error;
4694 sign = 1;
4695 }
4696 else {
4697 pbuf = formatbuf;
4698 len = formatint(pbuf,
4699 sizeof(formatbuf),
4700 flags, prec, c, v);
4701 if (len < 0)
4702 goto error;
4703 sign = 1;
4704 }
4705 if (flags & F_ZERO)
4706 fill = '0';
4707 break;
4708 case 'e':
4709 case 'E':
4710 case 'f':
4711 case 'F':
4712 case 'g':
4713 case 'G':
4714 if (c == 'F')
4715 c = 'f';
4716 pbuf = formatbuf;
4717 len = formatfloat(pbuf, sizeof(formatbuf),
4718 flags, prec, c, v);
4719 if (len < 0)
4720 goto error;
4721 sign = 1;
4722 if (flags & F_ZERO)
4723 fill = '0';
4724 break;
4725 case 'c':
4726#ifdef Py_USING_UNICODE
4727 if (PyUnicode_Check(v)) {
4728 fmt = fmt_start;
4729 argidx = argidx_start;
4730 goto unicode;
4731 }
4732#endif
4733 pbuf = formatbuf;
4734 len = formatchar(pbuf, sizeof(formatbuf), v);
4735 if (len < 0)
4736 goto error;
4737 break;
4738 default:
4739 PyErr_Format(PyExc_ValueError,
4740 "unsupported format character '%c' (0x%x) "
4741 "at index %zd",
4742 c, c,
4743 (Py_ssize_t)(fmt - 1 -
4744 PyString_AsString(format)));
4745 goto error;
4746 }
4747 if (sign) {
4748 if (*pbuf == '-' || *pbuf == '+') {
4749 sign = *pbuf++;
4750 len--;
4751 }
4752 else if (flags & F_SIGN)
4753 sign = '+';
4754 else if (flags & F_BLANK)
4755 sign = ' ';
4756 else
4757 sign = 0;
4758 }
4759 if (width < len)
4760 width = len;
4761 if (rescnt - (sign != 0) < width) {
4762 reslen -= rescnt;
4763 rescnt = width + fmtcnt + 100;
4764 reslen += rescnt;
4765 if (reslen < 0) {
4766 Py_DECREF(result);
4767 Py_XDECREF(temp);
4768 return PyErr_NoMemory();
4769 }
4770 if (_PyString_Resize(&result, reslen) < 0) {
4771 Py_XDECREF(temp);
4772 return NULL;
4773 }
4774 res = PyString_AS_STRING(result)
4775 + reslen - rescnt;
4776 }
4777 if (sign) {
4778 if (fill != ' ')
4779 *res++ = sign;
4780 rescnt--;
4781 if (width > len)
4782 width--;
4783 }
4784 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
4785 assert(pbuf[0] == '0');
4786 assert(pbuf[1] == c);
4787 if (fill != ' ') {
4788 *res++ = *pbuf++;
4789 *res++ = *pbuf++;
4790 }
4791 rescnt -= 2;
4792 width -= 2;
4793 if (width < 0)
4794 width = 0;
4795 len -= 2;
4796 }
4797 if (width > len && !(flags & F_LJUST)) {
4798 do {
4799 --rescnt;
4800 *res++ = fill;
4801 } while (--width > len);
4802 }
4803 if (fill == ' ') {
4804 if (sign)
4805 *res++ = sign;
4806 if ((flags & F_ALT) &&
4807 (c == 'x' || c == 'X')) {
4808 assert(pbuf[0] == '0');
4809 assert(pbuf[1] == c);
4810 *res++ = *pbuf++;
4811 *res++ = *pbuf++;
4812 }
4813 }
4814 Py_MEMCPY(res, pbuf, len);
4815 res += len;
4816 rescnt -= len;
4817 while (--width >= len) {
4818 --rescnt;
4819 *res++ = ' ';
4820 }
4821 if (dict && (argidx < arglen) && c != '%') {
4822 PyErr_SetString(PyExc_TypeError,
4823 "not all arguments converted during string formatting");
4824 Py_XDECREF(temp);
4825 goto error;
4826 }
4827 Py_XDECREF(temp);
4828 } /* '%' */
4829 } /* until end */
4830 if (argidx < arglen && !dict) {
4831 PyErr_SetString(PyExc_TypeError,
4832 "not all arguments converted during string formatting");
4833 goto error;
4834 }
4835 if (args_owned) {
4836 Py_DECREF(args);
4837 }
4838 _PyString_Resize(&result, reslen - rescnt);
4839 return result;
4840
4841#ifdef Py_USING_UNICODE
4842 unicode:
4843 if (args_owned) {
4844 Py_DECREF(args);
4845 args_owned = 0;
4846 }
4847 /* Fiddle args right (remove the first argidx arguments) */
4848 if (PyTuple_Check(orig_args) && argidx > 0) {
4849 PyObject *v;
4850 Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx;
4851 v = PyTuple_New(n);
4852 if (v == NULL)
4853 goto error;
4854 while (--n >= 0) {
4855 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx);
4856 Py_INCREF(w);
4857 PyTuple_SET_ITEM(v, n, w);
4858 }
4859 args = v;
4860 } else {
4861 Py_INCREF(orig_args);
4862 args = orig_args;
4863 }
4864 args_owned = 1;
4865 /* Take what we have of the result and let the Unicode formatting
4866 function format the rest of the input. */
4867 rescnt = res - PyString_AS_STRING(result);
4868 if (_PyString_Resize(&result, rescnt))
4869 goto error;
4870 fmtcnt = PyString_GET_SIZE(format) - \
4871 (fmt - PyString_AS_STRING(format));
4872 format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL);
4873 if (format == NULL)
4874 goto error;
4875 v = PyUnicode_Format(format, args);
4876 Py_DECREF(format);
4877 if (v == NULL)
4878 goto error;
4879 /* Paste what we have (result) to what the Unicode formatting
4880 function returned (v) and return the result (or error) */
4881 w = PyUnicode_Concat(result, v);
4882 Py_DECREF(result);
4883 Py_DECREF(v);
4884 Py_DECREF(args);
4885 return w;
4886#endif /* Py_USING_UNICODE */
4887
4888 error:
4889 Py_DECREF(result);
4890 if (args_owned) {
4891 Py_DECREF(args);
4892 }
4893 return NULL;
4894}
4895
4896void
4897PyString_InternInPlace(PyObject **p)
4898{
4899 register PyStringObject *s = (PyStringObject *)(*p);
4900 PyObject *t;
4901 if (s == NULL || !PyString_Check(s))
4902 Py_FatalError("PyString_InternInPlace: strings only please!");
4903 /* If it's a string subclass, we don't really know what putting
4904 it in the interned dict might do. */
4905 if (!PyString_CheckExact(s))
4906 return;
4907 if (PyString_CHECK_INTERNED(s))
4908 return;
4909 if (interned == NULL) {
4910 interned = PyDict_New();
4911 if (interned == NULL) {
4912 PyErr_Clear(); /* Don't leave an exception */
4913 return;
4914 }
4915 }
4916 t = PyDict_GetItem(interned, (PyObject *)s);
4917 if (t) {
4918 Py_INCREF(t);
4919 Py_DECREF(*p);
4920 *p = t;
4921 return;
4922 }
4923
4924 if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) {
4925 PyErr_Clear();
4926 return;
4927 }
4928 /* The two references in interned are not counted by refcnt.
4929 The string deallocator will take care of this */
4930 s->ob_refcnt -= 2;
4931 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
4932}
4933
4934void
4935PyString_InternImmortal(PyObject **p)
4936{
4937 PyString_InternInPlace(p);
4938 if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
4939 PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
4940 Py_INCREF(*p);
4941 }
4942}
4943
4944
4945PyObject *
4946PyString_InternFromString(const char *cp)
4947{
4948 PyObject *s = PyString_FromString(cp);
4949 if (s == NULL)
4950 return NULL;
4951 PyString_InternInPlace(&s);
4952 return s;
4953}
4954
4955void
4956PyString_Fini(void)
4957{
4958 int i;
4959 for (i = 0; i < UCHAR_MAX + 1; i++) {
4960 Py_XDECREF(characters[i]);
4961 characters[i] = NULL;
4962 }
4963 Py_XDECREF(nullstring);
4964 nullstring = NULL;
4965}
4966
4967void _Py_ReleaseInternedStrings(void)
4968{
4969 PyObject *keys;
4970 PyStringObject *s;
4971 Py_ssize_t i, n;
4972
4973 if (interned == NULL || !PyDict_Check(interned))
4974 return;
4975 keys = PyDict_Keys(interned);
4976 if (keys == NULL || !PyList_Check(keys)) {
4977 PyErr_Clear();
4978 return;
4979 }
4980
4981 /* Since _Py_ReleaseInternedStrings() is intended to help a leak
4982 detector, interned strings are not forcibly deallocated; rather, we
4983 give them their stolen references back, and then clear and DECREF
4984 the interned dict. */
4985
4986 fprintf(stderr, "releasing interned strings\n");
4987 n = PyList_GET_SIZE(keys);
4988 for (i = 0; i < n; i++) {
4989 s = (PyStringObject *) PyList_GET_ITEM(keys, i);
4990 switch (s->ob_sstate) {
4991 case SSTATE_NOT_INTERNED:
4992 /* XXX Shouldn't happen */
4993 break;
4994 case SSTATE_INTERNED_IMMORTAL:
4995 s->ob_refcnt += 1;
4996 break;
4997 case SSTATE_INTERNED_MORTAL:
4998 s->ob_refcnt += 2;
4999 break;

10 more lines aren’t shown here.