blob: ec49dd7f96b1f6b4f1524a3f9685d3ab0d6ef751 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* String object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum71160aa1997-06-03 18:03:18 +000036#include "mymath.h"
Guido van Rossum013142a1994-08-30 08:19:36 +000037#include <ctype.h>
38
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000039#ifdef COUNT_ALLOCS
40int null_strings, one_strings;
41#endif
42
Guido van Rossum03093a21994-09-28 15:51:32 +000043#ifdef HAVE_LIMITS_H
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000044#include <limits.h>
45#else
46#ifndef UCHAR_MAX
47#define UCHAR_MAX 255
48#endif
49#endif
50
Guido van Rossumc0b618a1997-05-02 03:12:38 +000051static PyStringObject *characters[UCHAR_MAX + 1];
Sjoerd Mullender615194a1993-11-01 13:46:50 +000052#ifndef DONT_SHARE_SHORT_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053static PyStringObject *nullstring;
Sjoerd Mullender615194a1993-11-01 13:46:50 +000054#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000055
56/*
57 Newsizedstringobject() and newstringobject() try in certain cases
58 to share string objects. When the size of the string is zero,
59 these routines always return a pointer to the same string object;
60 when the size is one, they return a pointer to an already existing
61 object if the contents of the string is known. For
62 newstringobject() this is always the case, for
63 newsizedstringobject() this is the case when the first argument in
64 not NULL.
65 A common practice to allocate a string and then fill it in or
66 change it must be done carefully. It is only allowed to change the
67 contents of the string if the obect was gotten from
68 newsizedstringobject() with a NULL first argument, because in the
69 future these routines may try to do even more sharing of objects.
70*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071PyObject *
72PyString_FromStringAndSize(str, size)
Guido van Rossum067998f1996-12-10 15:33:34 +000073 const char *str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 int size;
75{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076 register PyStringObject *op;
Sjoerd Mullender615194a1993-11-01 13:46:50 +000077#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000078 if (size == 0 && (op = nullstring) != NULL) {
79#ifdef COUNT_ALLOCS
80 null_strings++;
81#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 Py_INCREF(op);
83 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000084 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 if (size == 1 && str != NULL &&
86 (op = characters[*str & UCHAR_MAX]) != NULL)
87 {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000088#ifdef COUNT_ALLOCS
89 one_strings++;
90#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 Py_INCREF(op);
92 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000093 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +000094#endif /* DONT_SHARE_SHORT_STRINGS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 op = (PyStringObject *)
96 malloc(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +000097 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 return PyErr_NoMemory();
99 op->ob_type = &PyString_Type;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000100 op->ob_size = size;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000101#ifdef CACHE_HASH
102 op->ob_shash = -1;
103#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000104#ifdef INTERN_STRINGS
105 op->ob_sinterned = NULL;
106#endif
Guido van Rossumbffd6832000-01-20 22:32:56 +0000107 _Py_NewReference((PyObject *)op);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000108 if (str != NULL)
109 memcpy(op->ob_sval, str, size);
110 op->ob_sval[size] = '\0';
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000111#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000112 if (size == 0) {
113 nullstring = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000115 } else if (size == 1 && str != NULL) {
116 characters[*str & UCHAR_MAX] = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000118 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000119#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000123PyObject *
124PyString_FromString(str)
Guido van Rossum067998f1996-12-10 15:33:34 +0000125 const char *str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126{
127 register unsigned int size = strlen(str);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 register PyStringObject *op;
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000129#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000130 if (size == 0 && (op = nullstring) != NULL) {
131#ifdef COUNT_ALLOCS
132 null_strings++;
133#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 Py_INCREF(op);
135 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000136 }
137 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
138#ifdef COUNT_ALLOCS
139 one_strings++;
140#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 Py_INCREF(op);
142 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000143 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000144#endif /* DONT_SHARE_SHORT_STRINGS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 op = (PyStringObject *)
146 malloc(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000147 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 return PyErr_NoMemory();
149 op->ob_type = &PyString_Type;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000150 op->ob_size = size;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000151#ifdef CACHE_HASH
152 op->ob_shash = -1;
153#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000154#ifdef INTERN_STRINGS
155 op->ob_sinterned = NULL;
156#endif
Guido van Rossumbffd6832000-01-20 22:32:56 +0000157 _Py_NewReference((PyObject *)op);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000158 strcpy(op->ob_sval, str);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000159#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000160 if (size == 0) {
161 nullstring = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000163 } else if (size == 1) {
164 characters[*str & UCHAR_MAX] = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000166 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000167#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
170
Guido van Rossum234f9421993-06-17 12:35:49 +0000171static void
Guido van Rossume5372401993-03-16 12:15:04 +0000172string_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173 PyObject *op;
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000174{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175 PyMem_DEL(op);
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000176}
177
Guido van Rossumd7047b31995-01-02 19:07:15 +0000178int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179PyString_Size(op)
180 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 if (!PyString_Check(op)) {
183 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 return -1;
185 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186 return ((PyStringObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
189/*const*/ char *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190PyString_AsString(op)
191 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 if (!PyString_Check(op)) {
194 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 return NULL;
196 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 return ((PyStringObject *)op) -> ob_sval;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198}
199
200/* Methods */
201
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000202static int
Guido van Rossume5372401993-03-16 12:15:04 +0000203string_print(op, fp, flags)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyStringObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 FILE *fp;
206 int flags;
207{
208 int i;
209 char c;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000210 int quote;
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000211 /* XXX Ought to check for interrupts when writing long strings */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 if (flags & Py_PRINT_RAW) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 fwrite(op->ob_sval, 1, (int) op->ob_size, fp);
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000214 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000216
217 /* figure out which quote to use; single is prefered */
218 quote = '\'';
219 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
220 quote = '"';
221
222 fputc(quote, fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223 for (i = 0; i < op->ob_size; i++) {
224 c = op->ob_sval[i];
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000225 if (c == quote || c == '\\')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226 fprintf(fp, "\\%c", c);
227 else if (c < ' ' || c >= 0177)
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000228 fprintf(fp, "\\%03o", c & 0377);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 else
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000230 fputc(c, fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000232 fputc(quote, fp);
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000233 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234}
235
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000237string_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 register PyStringObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239{
240 /* XXX overflow? */
241 int newsize = 2 + 4 * op->ob_size * sizeof(char);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242 PyObject *v = PyString_FromStringAndSize((char *)NULL, newsize);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 if (v == NULL) {
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000244 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 }
246 else {
247 register int i;
248 register char c;
249 register char *p;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000250 int quote;
251
252 /* figure out which quote to use; single is prefered */
253 quote = '\'';
254 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
255 quote = '"';
256
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000257 p = ((PyStringObject *)v)->ob_sval;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000258 *p++ = quote;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259 for (i = 0; i < op->ob_size; i++) {
260 c = op->ob_sval[i];
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000261 if (c == quote || c == '\\')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 *p++ = '\\', *p++ = c;
263 else if (c < ' ' || c >= 0177) {
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000264 sprintf(p, "\\%03o", c & 0377);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265 while (*p != '\0')
266 p++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267 }
268 else
269 *p++ = c;
270 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000271 *p++ = quote;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 *p = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 _PyString_Resize(
274 &v, (int) (p - ((PyStringObject *)v)->ob_sval));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000275 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
279static int
Guido van Rossume5372401993-03-16 12:15:04 +0000280string_length(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 PyStringObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282{
283 return a->ob_size;
284}
285
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000287string_concat(a, bb)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 register PyStringObject *a;
289 register PyObject *bb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290{
291 register unsigned int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 register PyStringObject *op;
293 if (!PyString_Check(bb)) {
294 PyErr_BadArgument();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 return NULL;
296 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297#define b ((PyStringObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298 /* Optimize cases with empty left or right operand */
299 if (a->ob_size == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300 Py_INCREF(bb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301 return bb;
302 }
303 if (b->ob_size == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 Py_INCREF(a);
305 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 }
307 size = a->ob_size + b->ob_size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 op = (PyStringObject *)
309 malloc(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000310 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 return PyErr_NoMemory();
312 op->ob_type = &PyString_Type;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000313 op->ob_size = size;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000314#ifdef CACHE_HASH
315 op->ob_shash = -1;
316#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000317#ifdef INTERN_STRINGS
318 op->ob_sinterned = NULL;
319#endif
Guido van Rossumbffd6832000-01-20 22:32:56 +0000320 _Py_NewReference((PyObject *)op);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000321 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
322 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
323 op->ob_sval[size] = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325#undef b
326}
327
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000329string_repeat(a, n)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 register PyStringObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331 register int n;
332{
333 register int i;
Guido van Rossum2095d241997-04-09 19:41:24 +0000334 register int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 register PyStringObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 if (n < 0)
337 n = 0;
338 size = a->ob_size * n;
339 if (size == a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 Py_INCREF(a);
341 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 op = (PyStringObject *)
344 malloc(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000345 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 return PyErr_NoMemory();
347 op->ob_type = &PyString_Type;
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000348 op->ob_size = size;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000349#ifdef CACHE_HASH
350 op->ob_shash = -1;
351#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000352#ifdef INTERN_STRINGS
353 op->ob_sinterned = NULL;
354#endif
Guido van Rossumbffd6832000-01-20 22:32:56 +0000355 _Py_NewReference((PyObject *)op);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000356 for (i = 0; i < size; i += a->ob_size)
357 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
358 op->ob_sval[size] = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360}
361
362/* String slice a[i:j] consists of characters a[i] ... a[j-1] */
363
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000365string_slice(a, i, j)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366 register PyStringObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367 register int i, j; /* May be negative! */
368{
369 if (i < 0)
370 i = 0;
371 if (j < 0)
372 j = 0; /* Avoid signed/unsigned bug in next line */
373 if (j > a->ob_size)
374 j = a->ob_size;
375 if (i == 0 && j == a->ob_size) { /* It's the same as a */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376 Py_INCREF(a);
377 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378 }
379 if (j < i)
380 j = i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000381 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382}
383
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000384static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000385string_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386 PyStringObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387 register int i;
388{
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000389 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyErr_SetString(PyExc_IndexError, "string index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393 return NULL;
394 }
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000395 c = a->ob_sval[i] & UCHAR_MAX;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 v = (PyObject *) characters[c];
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000397#ifdef COUNT_ALLOCS
398 if (v != NULL)
399 one_strings++;
400#endif
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000401 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 v = PyString_FromStringAndSize((char *)NULL, 1);
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000403 if (v == NULL)
404 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 characters[c] = (PyStringObject *) v;
406 ((PyStringObject *)v)->ob_sval[0] = c;
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000407 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 Py_INCREF(v);
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000409 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410}
411
412static int
Guido van Rossume5372401993-03-16 12:15:04 +0000413string_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 PyStringObject *a, *b;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415{
Guido van Rossum253919f1991-02-13 23:18:39 +0000416 int len_a = a->ob_size, len_b = b->ob_size;
417 int min_len = (len_a < len_b) ? len_a : len_b;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000418 int cmp;
419 if (min_len > 0) {
Guido van Rossumfde7a751996-10-23 14:19:40 +0000420 cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000421 if (cmp == 0)
422 cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
423 if (cmp != 0)
424 return cmp;
425 }
Guido van Rossum253919f1991-02-13 23:18:39 +0000426 return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427}
428
Guido van Rossum9bfef441993-03-29 10:43:31 +0000429static long
430string_hash(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 PyStringObject *a;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000432{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000433 register int len;
434 register unsigned char *p;
435 register long x;
436
437#ifdef CACHE_HASH
438 if (a->ob_shash != -1)
439 return a->ob_shash;
Guido van Rossum36b9f791997-02-14 16:29:22 +0000440#ifdef INTERN_STRINGS
441 if (a->ob_sinterned != NULL)
442 return (a->ob_shash =
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 ((PyStringObject *)(a->ob_sinterned))->ob_shash);
Guido van Rossum36b9f791997-02-14 16:29:22 +0000444#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000445#endif
446 len = a->ob_size;
447 p = (unsigned char *) a->ob_sval;
448 x = *p << 7;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000449 while (--len >= 0)
Guido van Rossumeddcb3b1996-09-11 20:22:48 +0000450 x = (1000003*x) ^ *p++;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000451 x ^= a->ob_size;
452 if (x == -1)
453 x = -2;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000454#ifdef CACHE_HASH
455 a->ob_shash = x;
456#endif
Guido van Rossum9bfef441993-03-29 10:43:31 +0000457 return x;
458}
459
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000460static int
461string_buffer_getreadbuf(self, index, ptr)
462 PyStringObject *self;
463 int index;
464 const void **ptr;
465{
466 if ( index != 0 ) {
Guido van Rossum045e6881997-09-08 18:30:11 +0000467 PyErr_SetString(PyExc_SystemError,
Guido van Rossum1db70701998-10-08 02:18:52 +0000468 "accessing non-existent string segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000469 return -1;
470 }
471 *ptr = (void *)self->ob_sval;
472 return self->ob_size;
473}
474
475static int
476string_buffer_getwritebuf(self, index, ptr)
477 PyStringObject *self;
478 int index;
479 const void **ptr;
480{
Guido van Rossum045e6881997-09-08 18:30:11 +0000481 PyErr_SetString(PyExc_TypeError,
Guido van Rossum07d78001998-10-01 15:59:48 +0000482 "Cannot use string as modifiable buffer");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000483 return -1;
484}
485
486static int
487string_buffer_getsegcount(self, lenp)
488 PyStringObject *self;
489 int *lenp;
490{
491 if ( lenp )
492 *lenp = self->ob_size;
493 return 1;
494}
495
Guido van Rossum1db70701998-10-08 02:18:52 +0000496static int
497string_buffer_getcharbuf(self, index, ptr)
498 PyStringObject *self;
499 int index;
500 const char **ptr;
501{
502 if ( index != 0 ) {
503 PyErr_SetString(PyExc_SystemError,
504 "accessing non-existent string segment");
505 return -1;
506 }
507 *ptr = self->ob_sval;
508 return self->ob_size;
509}
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PySequenceMethods string_as_sequence = {
Guido van Rossum013142a1994-08-30 08:19:36 +0000512 (inquiry)string_length, /*sq_length*/
513 (binaryfunc)string_concat, /*sq_concat*/
514 (intargfunc)string_repeat, /*sq_repeat*/
515 (intargfunc)string_item, /*sq_item*/
516 (intintargfunc)string_slice, /*sq_slice*/
Guido van Rossumf380e661991-06-04 19:36:32 +0000517 0, /*sq_ass_item*/
518 0, /*sq_ass_slice*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519};
520
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000521static PyBufferProcs string_as_buffer = {
522 (getreadbufferproc)string_buffer_getreadbuf,
523 (getwritebufferproc)string_buffer_getwritebuf,
524 (getsegcountproc)string_buffer_getsegcount,
Guido van Rossum1db70701998-10-08 02:18:52 +0000525 (getcharbufferproc)string_buffer_getcharbuf,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000526};
527
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000528
529
530#define LEFTSTRIP 0
531#define RIGHTSTRIP 1
532#define BOTHSTRIP 2
533
534
535static PyObject *
536split_whitespace(s, len, maxsplit)
537 char *s;
538 int len;
539 int maxsplit;
540{
541 int i = 0, j, err;
542 int countsplit = 0;
543 PyObject* item;
544 PyObject *list = PyList_New(0);
545
546 if (list == NULL)
547 return NULL;
548
549 while (i < len) {
550 while (i < len && isspace(Py_CHARMASK(s[i]))) {
551 i = i+1;
552 }
553 j = i;
554 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
555 i = i+1;
556 }
557 if (j < i) {
558 item = PyString_FromStringAndSize(s+j, (int)(i-j));
559 if (item == NULL)
560 goto finally;
561
562 err = PyList_Append(list, item);
563 Py_DECREF(item);
564 if (err < 0)
565 goto finally;
566
567 countsplit++;
568 while (i < len && isspace(Py_CHARMASK(s[i]))) {
569 i = i+1;
570 }
571 if (maxsplit && (countsplit >= maxsplit) && i < len) {
572 item = PyString_FromStringAndSize(
573 s+i, (int)(len - i));
574 if (item == NULL)
575 goto finally;
576
577 err = PyList_Append(list, item);
578 Py_DECREF(item);
579 if (err < 0)
580 goto finally;
581
582 i = len;
583 }
584 }
585 }
586 return list;
587 finally:
588 Py_DECREF(list);
589 return NULL;
590}
591
592
593static char split__doc__[] =
594"S.split([sep [,maxsplit]]) -> list of strings\n\
595\n\
596Return a list of the words in the string S, using sep as the\n\
597delimiter string. If maxsplit is nonzero, splits into at most\n\
598maxsplit words If sep is not specified, any whitespace string\n\
599is a separator. Maxsplit defaults to 0.";
600
601static PyObject *
602string_split(self, args)
603 PyStringObject *self;
604 PyObject *args;
605{
606 int len = PyString_GET_SIZE(self), n, i, j, err;
607 int splitcount, maxsplit;
608 char *s = PyString_AS_STRING(self), *sub;
609 PyObject *list, *item;
610
611 sub = NULL;
612 n = 0;
613 splitcount = 0;
614 maxsplit = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000615 if (!PyArg_ParseTuple(args, "|z#i:split", &sub, &n, &maxsplit))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000616 return NULL;
617 if (sub == NULL)
618 return split_whitespace(s, len, maxsplit);
619 if (n == 0) {
620 PyErr_SetString(PyExc_ValueError, "empty separator");
621 return NULL;
622 }
623
624 list = PyList_New(0);
625 if (list == NULL)
626 return NULL;
627
628 i = j = 0;
629 while (i+n <= len) {
630 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
631 item = PyString_FromStringAndSize(s+j, (int)(i-j));
632 if (item == NULL)
633 goto fail;
634 err = PyList_Append(list, item);
635 Py_DECREF(item);
636 if (err < 0)
637 goto fail;
638 i = j = i + n;
639 splitcount++;
640 if (maxsplit && (splitcount >= maxsplit))
641 break;
642 }
643 else
644 i++;
645 }
646 item = PyString_FromStringAndSize(s+j, (int)(len-j));
647 if (item == NULL)
648 goto fail;
649 err = PyList_Append(list, item);
650 Py_DECREF(item);
651 if (err < 0)
652 goto fail;
653
654 return list;
655
656 fail:
657 Py_DECREF(list);
658 return NULL;
659}
660
661
662static char join__doc__[] =
663"S.join(sequence) -> string\n\
664\n\
665Return a string which is the concatenation of the string representation\n\
666of every element in the sequence. The separator between elements is S.";
667
668static PyObject *
669string_join(self, args)
670 PyStringObject *self;
671 PyObject *args;
672{
673 char *sep = PyString_AS_STRING(self);
674 int seplen = PyString_GET_SIZE(self);
675 PyObject *res = NULL;
676 int reslen = 0;
677 char *p;
678 int seqlen = 0;
679 int sz = 100;
680 int i, slen;
681 PyObject *seq;
682
Guido van Rossum43713e52000-02-29 13:59:29 +0000683 if (!PyArg_ParseTuple(args, "O:join", &seq))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000684 return NULL;
685
686 seqlen = PySequence_Length(seq);
687 if (seqlen < 0 && PyErr_Occurred())
688 return NULL;
689
690 if (seqlen == 1) {
691 /* Optimization if there's only one item */
692 PyObject *item = PySequence_GetItem(seq, 0);
693 PyObject *stritem = PyObject_Str(item);
694 Py_DECREF(item);
695 return stritem;
696 }
697 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
698 return NULL;
699 p = PyString_AsString(res);
700
701 /* optimize for lists. all others (tuples and arbitrary sequences)
702 * just use the abstract interface.
703 */
704 if (PyList_Check(seq)) {
705 for (i = 0; i < seqlen; i++) {
706 PyObject *item = PyList_GET_ITEM(seq, i);
707 PyObject *sitem = PyObject_Str(item);
708 if (!sitem)
709 goto finally;
710 slen = PyString_GET_SIZE(sitem);
711 while (reslen + slen + seplen >= sz) {
712 if (_PyString_Resize(&res, sz*2))
713 goto finally;
714 sz *= 2;
715 p = PyString_AsString(res) + reslen;
716 }
717 if (i > 0) {
718 memcpy(p, sep, seplen);
719 p += seplen;
720 reslen += seplen;
721 }
722 memcpy(p, PyString_AS_STRING(sitem), slen);
723 p += slen;
724 reslen += slen;
725 }
726 }
727 else {
728 for (i = 0; i < seqlen; i++) {
729 PyObject *item = PySequence_GetItem(seq, i);
730 PyObject *sitem;
731 if (!item || !(sitem = PyObject_Str(item))) {
732 Py_XDECREF(item);
733 goto finally;
734 }
735 slen = PyString_GET_SIZE(sitem);
736 while (reslen + slen + seplen >= sz) {
737 if (_PyString_Resize(&res, sz*2))
738 goto finally;
739 sz *= 2;
740 p = PyString_AsString(res) + reslen;
741 }
742 if (i > 0) {
743 memcpy(p, sep, seplen);
744 p += seplen;
745 reslen += seplen;
746 }
747 memcpy(p, PyString_AS_STRING(sitem), slen);
748 p += slen;
749 reslen += slen;
750 }
751 }
752 if (_PyString_Resize(&res, reslen))
753 goto finally;
754 return res;
755
756 finally:
757 Py_DECREF(res);
758 return NULL;
759}
760
761
762
763static long
764string_find_internal(self, args)
765 PyStringObject *self;
766 PyObject *args;
767{
768 char *s = PyString_AS_STRING(self), *sub;
769 int len = PyString_GET_SIZE(self);
770 int n, i = 0, last = INT_MAX;
771
Guido van Rossum43713e52000-02-29 13:59:29 +0000772 if (!PyArg_ParseTuple(args, "t#|ii:find", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000773 return -2;
774
775 if (last > len)
776 last = len;
777 if (last < 0)
778 last += len;
779 if (last < 0)
780 last = 0;
781 if (i < 0)
782 i += len;
783 if (i < 0)
784 i = 0;
785
786 if (n == 0 && i <= last)
787 return (long)i;
788
789 last -= n;
790 for (; i <= last; ++i)
791 if (s[i] == sub[0] &&
792 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
793 return (long)i;
794
795 return -1;
796}
797
798
799static char find__doc__[] =
800"S.find(sub [,start [,end]]) -> int\n\
801\n\
802Return the lowest index in S where substring sub is found,\n\
803such that sub is contained within s[start,end]. Optional\n\
804arguments start and end are interpreted as in slice notation.\n\
805\n\
806Return -1 on failure.";
807
808static PyObject *
809string_find(self, args)
810 PyStringObject *self;
811 PyObject *args;
812{
813 long result = string_find_internal(self, args);
814 if (result == -2)
815 return NULL;
816 return PyInt_FromLong(result);
817}
818
819
820static char index__doc__[] =
821"S.index(sub [,start [,end]]) -> int\n\
822\n\
823Like S.find() but raise ValueError when the substring is not found.";
824
825static PyObject *
826string_index(self, args)
827 PyStringObject *self;
828 PyObject *args;
829{
830 long result = string_find_internal(self, args);
831 if (result == -2)
832 return NULL;
833 if (result == -1) {
834 PyErr_SetString(PyExc_ValueError,
835 "substring not found in string.index");
836 return NULL;
837 }
838 return PyInt_FromLong(result);
839}
840
841
842static long
843string_rfind_internal(self, args)
844 PyStringObject *self;
845 PyObject *args;
846{
847 char *s = PyString_AS_STRING(self), *sub;
848 int len = PyString_GET_SIZE(self), n, j;
849 int i = 0, last = INT_MAX;
850
Guido van Rossum43713e52000-02-29 13:59:29 +0000851 if (!PyArg_ParseTuple(args, "t#|ii:rfind", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000852 return -2;
853
854 if (last > len)
855 last = len;
856 if (last < 0)
857 last += len;
858 if (last < 0)
859 last = 0;
860 if (i < 0)
861 i += len;
862 if (i < 0)
863 i = 0;
864
865 if (n == 0 && i <= last)
866 return (long)last;
867
868 for (j = last-n; j >= i; --j)
869 if (s[j] == sub[0] &&
870 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
871 return (long)j;
872
873 return -1;
874}
875
876
877static char rfind__doc__[] =
878"S.rfind(sub [,start [,end]]) -> int\n\
879\n\
880Return the highest index in S where substring sub is found,\n\
881such that sub is contained within s[start,end]. Optional\n\
882arguments start and end are interpreted as in slice notation.\n\
883\n\
884Return -1 on failure.";
885
886static PyObject *
887string_rfind(self, args)
888 PyStringObject *self;
889 PyObject *args;
890{
891 long result = string_rfind_internal(self, args);
892 if (result == -2)
893 return NULL;
894 return PyInt_FromLong(result);
895}
896
897
898static char rindex__doc__[] =
899"S.rindex(sub [,start [,end]]) -> int\n\
900\n\
901Like S.rfind() but raise ValueError when the substring is not found.";
902
903static PyObject *
904string_rindex(self, args)
905 PyStringObject *self;
906 PyObject *args;
907{
908 long result = string_rfind_internal(self, args);
909 if (result == -2)
910 return NULL;
911 if (result == -1) {
912 PyErr_SetString(PyExc_ValueError,
913 "substring not found in string.rindex");
914 return NULL;
915 }
916 return PyInt_FromLong(result);
917}
918
919
920static PyObject *
921do_strip(self, args, striptype)
922 PyStringObject *self;
923 PyObject *args;
924 int striptype;
925{
926 char *s = PyString_AS_STRING(self);
927 int len = PyString_GET_SIZE(self), i, j;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000928
Guido van Rossum43713e52000-02-29 13:59:29 +0000929 if (!PyArg_ParseTuple(args, ":strip"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000930 return NULL;
931
932 i = 0;
933 if (striptype != RIGHTSTRIP) {
934 while (i < len && isspace(Py_CHARMASK(s[i]))) {
935 i++;
936 }
937 }
938
939 j = len;
940 if (striptype != LEFTSTRIP) {
941 do {
942 j--;
943 } while (j >= i && isspace(Py_CHARMASK(s[j])));
944 j++;
945 }
946
947 if (i == 0 && j == len) {
948 Py_INCREF(self);
949 return (PyObject*)self;
950 }
951 else
952 return PyString_FromStringAndSize(s+i, j-i);
953}
954
955
956static char strip__doc__[] =
957"S.strip() -> string\n\
958\n\
959Return a copy of the string S with leading and trailing\n\
960whitespace removed.";
961
962static PyObject *
963string_strip(self, args)
964 PyStringObject *self;
965 PyObject *args;
966{
967 return do_strip(self, args, BOTHSTRIP);
968}
969
970
971static char lstrip__doc__[] =
972"S.lstrip() -> string\n\
973\n\
974Return a copy of the string S with leading whitespace removed.";
975
976static PyObject *
977string_lstrip(self, args)
978 PyStringObject *self;
979 PyObject *args;
980{
981 return do_strip(self, args, LEFTSTRIP);
982}
983
984
985static char rstrip__doc__[] =
986"S.rstrip() -> string\n\
987\n\
988Return a copy of the string S with trailing whitespace removed.";
989
990static PyObject *
991string_rstrip(self, args)
992 PyStringObject *self;
993 PyObject *args;
994{
995 return do_strip(self, args, RIGHTSTRIP);
996}
997
998
999static char lower__doc__[] =
1000"S.lower() -> string\n\
1001\n\
1002Return a copy of the string S converted to lowercase.";
1003
1004static PyObject *
1005string_lower(self, args)
1006 PyStringObject *self;
1007 PyObject *args;
1008{
1009 char *s = PyString_AS_STRING(self), *s_new;
1010 int i, n = PyString_GET_SIZE(self);
1011 PyObject *new;
1012
Guido van Rossum43713e52000-02-29 13:59:29 +00001013 if (!PyArg_ParseTuple(args, ":lower"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001014 return NULL;
1015 new = PyString_FromStringAndSize(NULL, n);
1016 if (new == NULL)
1017 return NULL;
1018 s_new = PyString_AsString(new);
1019 for (i = 0; i < n; i++) {
1020 int c = Py_CHARMASK(*s++);
1021 if (isupper(c)) {
1022 *s_new = tolower(c);
1023 } else
1024 *s_new = c;
1025 s_new++;
1026 }
1027 return new;
1028}
1029
1030
1031static char upper__doc__[] =
1032"S.upper() -> string\n\
1033\n\
1034Return a copy of the string S converted to uppercase.";
1035
1036static PyObject *
1037string_upper(self, args)
1038 PyStringObject *self;
1039 PyObject *args;
1040{
1041 char *s = PyString_AS_STRING(self), *s_new;
1042 int i, n = PyString_GET_SIZE(self);
1043 PyObject *new;
1044
Guido van Rossum43713e52000-02-29 13:59:29 +00001045 if (!PyArg_ParseTuple(args, ":upper"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001046 return NULL;
1047 new = PyString_FromStringAndSize(NULL, n);
1048 if (new == NULL)
1049 return NULL;
1050 s_new = PyString_AsString(new);
1051 for (i = 0; i < n; i++) {
1052 int c = Py_CHARMASK(*s++);
1053 if (islower(c)) {
1054 *s_new = toupper(c);
1055 } else
1056 *s_new = c;
1057 s_new++;
1058 }
1059 return new;
1060}
1061
1062
1063static char capitalize__doc__[] =
1064"S.capitalize() -> string\n\
1065\n\
1066Return a copy of the string S with only its first character\n\
1067capitalized.";
1068
1069static PyObject *
1070string_capitalize(self, args)
1071 PyStringObject *self;
1072 PyObject *args;
1073{
1074 char *s = PyString_AS_STRING(self), *s_new;
1075 int i, n = PyString_GET_SIZE(self);
1076 PyObject *new;
1077
Guido van Rossum43713e52000-02-29 13:59:29 +00001078 if (!PyArg_ParseTuple(args, ":capitalize"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001079 return NULL;
1080 new = PyString_FromStringAndSize(NULL, n);
1081 if (new == NULL)
1082 return NULL;
1083 s_new = PyString_AsString(new);
1084 if (0 < n) {
1085 int c = Py_CHARMASK(*s++);
1086 if (islower(c))
1087 *s_new = toupper(c);
1088 else
1089 *s_new = c;
1090 s_new++;
1091 }
1092 for (i = 1; i < n; i++) {
1093 int c = Py_CHARMASK(*s++);
1094 if (isupper(c))
1095 *s_new = tolower(c);
1096 else
1097 *s_new = c;
1098 s_new++;
1099 }
1100 return new;
1101}
1102
1103
1104static char count__doc__[] =
1105"S.count(sub[, start[, end]]) -> int\n\
1106\n\
1107Return the number of occurrences of substring sub in string\n\
1108S[start:end]. Optional arguments start and end are\n\
1109interpreted as in slice notation.";
1110
1111static PyObject *
1112string_count(self, args)
1113 PyStringObject *self;
1114 PyObject *args;
1115{
1116 char *s = PyString_AS_STRING(self), *sub;
1117 int len = PyString_GET_SIZE(self), n;
1118 int i = 0, last = INT_MAX;
1119 int m, r;
1120
Guido van Rossum43713e52000-02-29 13:59:29 +00001121 if (!PyArg_ParseTuple(args, "t#|ii:count", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001122 return NULL;
1123 if (last > len)
1124 last = len;
1125 if (last < 0)
1126 last += len;
1127 if (last < 0)
1128 last = 0;
1129 if (i < 0)
1130 i += len;
1131 if (i < 0)
1132 i = 0;
1133 m = last + 1 - n;
1134 if (n == 0)
1135 return PyInt_FromLong((long) (m-i));
1136
1137 r = 0;
1138 while (i < m) {
1139 if (!memcmp(s+i, sub, n)) {
1140 r++;
1141 i += n;
1142 } else {
1143 i++;
1144 }
1145 }
1146 return PyInt_FromLong((long) r);
1147}
1148
1149
1150static char swapcase__doc__[] =
1151"S.swapcase() -> string\n\
1152\n\
1153Return a copy of the string S with upper case characters\n\
1154converted to lowercase and vice versa.";
1155
1156static PyObject *
1157string_swapcase(self, args)
1158 PyStringObject *self;
1159 PyObject *args;
1160{
1161 char *s = PyString_AS_STRING(self), *s_new;
1162 int i, n = PyString_GET_SIZE(self);
1163 PyObject *new;
1164
Guido van Rossum43713e52000-02-29 13:59:29 +00001165 if (!PyArg_ParseTuple(args, ":swapcase"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001166 return NULL;
1167 new = PyString_FromStringAndSize(NULL, n);
1168 if (new == NULL)
1169 return NULL;
1170 s_new = PyString_AsString(new);
1171 for (i = 0; i < n; i++) {
1172 int c = Py_CHARMASK(*s++);
1173 if (islower(c)) {
1174 *s_new = toupper(c);
1175 }
1176 else if (isupper(c)) {
1177 *s_new = tolower(c);
1178 }
1179 else
1180 *s_new = c;
1181 s_new++;
1182 }
1183 return new;
1184}
1185
1186
1187static char translate__doc__[] =
1188"S.translate(table [,deletechars]) -> string\n\
1189\n\
1190Return a copy of the string S, where all characters occurring\n\
1191in the optional argument deletechars are removed, and the\n\
1192remaining characters have been mapped through the given\n\
1193translation table, which must be a string of length 256.";
1194
1195static PyObject *
1196string_translate(self, args)
1197 PyStringObject *self;
1198 PyObject *args;
1199{
1200 register char *input, *table, *output;
1201 register int i, c, changed = 0;
1202 PyObject *input_obj = (PyObject*)self;
1203 char *table1, *output_start, *del_table=NULL;
1204 int inlen, tablen, dellen = 0;
1205 PyObject *result;
1206 int trans_table[256];
1207
Guido van Rossum43713e52000-02-29 13:59:29 +00001208 if (!PyArg_ParseTuple(args, "t#|t#:translate",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001209 &table1, &tablen, &del_table, &dellen))
1210 return NULL;
1211 if (tablen != 256) {
1212 PyErr_SetString(PyExc_ValueError,
1213 "translation table must be 256 characters long");
1214 return NULL;
1215 }
1216
1217 table = table1;
1218 inlen = PyString_Size(input_obj);
1219 result = PyString_FromStringAndSize((char *)NULL, inlen);
1220 if (result == NULL)
1221 return NULL;
1222 output_start = output = PyString_AsString(result);
1223 input = PyString_AsString(input_obj);
1224
1225 if (dellen == 0) {
1226 /* If no deletions are required, use faster code */
1227 for (i = inlen; --i >= 0; ) {
1228 c = Py_CHARMASK(*input++);
1229 if (Py_CHARMASK((*output++ = table[c])) != c)
1230 changed = 1;
1231 }
1232 if (changed)
1233 return result;
1234 Py_DECREF(result);
1235 Py_INCREF(input_obj);
1236 return input_obj;
1237 }
1238
1239 for (i = 0; i < 256; i++)
1240 trans_table[i] = Py_CHARMASK(table[i]);
1241
1242 for (i = 0; i < dellen; i++)
1243 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
1244
1245 for (i = inlen; --i >= 0; ) {
1246 c = Py_CHARMASK(*input++);
1247 if (trans_table[c] != -1)
1248 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1249 continue;
1250 changed = 1;
1251 }
1252 if (!changed) {
1253 Py_DECREF(result);
1254 Py_INCREF(input_obj);
1255 return input_obj;
1256 }
1257 /* Fix the size of the resulting string */
1258 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
1259 return NULL;
1260 return result;
1261}
1262
1263
1264/* What follows is used for implementing replace(). Perry Stoll. */
1265
1266/*
1267 mymemfind
1268
1269 strstr replacement for arbitrary blocks of memory.
1270
1271 Locates the first occurance in the memory pointed to by MEM of the
1272 contents of memory pointed to by PAT. Returns the index into MEM if
1273 found, or -1 if not found. If len of PAT is greater than length of
1274 MEM, the function returns -1.
1275*/
1276static int
1277mymemfind(mem, len, pat, pat_len)
1278 char *mem;
1279 int len;
1280 char *pat;
1281 int pat_len;
1282{
1283 register int ii;
1284
1285 /* pattern can not occur in the last pat_len-1 chars */
1286 len -= pat_len;
1287
1288 for (ii = 0; ii <= len; ii++) {
1289 if (mem[ii] == pat[0] &&
1290 (pat_len == 1 ||
1291 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1292 return ii;
1293 }
1294 }
1295 return -1;
1296}
1297
1298/*
1299 mymemcnt
1300
1301 Return the number of distinct times PAT is found in MEM.
1302 meaning mem=1111 and pat==11 returns 2.
1303 mem=11111 and pat==11 also return 2.
1304 */
1305static int
1306mymemcnt(mem, len, pat, pat_len)
1307 char *mem;
1308 int len;
1309 char *pat;
1310 int pat_len;
1311{
1312 register int offset = 0;
1313 int nfound = 0;
1314
1315 while (len >= 0) {
1316 offset = mymemfind(mem, len, pat, pat_len);
1317 if (offset == -1)
1318 break;
1319 mem += offset + pat_len;
1320 len -= offset + pat_len;
1321 nfound++;
1322 }
1323 return nfound;
1324}
1325
1326/*
1327 mymemreplace
1328
1329 Return a string in which all occurences of PAT in memory STR are
1330 replaced with SUB.
1331
1332 If length of PAT is less than length of STR or there are no occurences
1333 of PAT in STR, then the original string is returned. Otherwise, a new
1334 string is allocated here and returned.
1335
1336 on return, out_len is:
1337 the length of output string, or
1338 -1 if the input string is returned, or
1339 unchanged if an error occurs (no memory).
1340
1341 return value is:
1342 the new string allocated locally, or
1343 NULL if an error occurred.
1344*/
1345static char *
1346mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
1347 char *str;
1348 int len; /* input string */
1349 char *pat;
1350 int pat_len; /* pattern string to find */
1351 char *sub;
1352 int sub_len; /* substitution string */
1353 int count; /* number of replacements, 0 == all */
1354 int *out_len;
1355
1356{
1357 char *out_s;
1358 char *new_s;
1359 int nfound, offset, new_len;
1360
1361 if (len == 0 || pat_len > len)
1362 goto return_same;
1363
1364 /* find length of output string */
1365 nfound = mymemcnt(str, len, pat, pat_len);
1366 if (count > 0)
1367 nfound = nfound > count ? count : nfound;
1368 if (nfound == 0)
1369 goto return_same;
1370 new_len = len + nfound*(sub_len - pat_len);
1371
1372 new_s = (char *)malloc(new_len);
1373 if (new_s == NULL) return NULL;
1374
1375 *out_len = new_len;
1376 out_s = new_s;
1377
1378 while (len > 0) {
1379 /* find index of next instance of pattern */
1380 offset = mymemfind(str, len, pat, pat_len);
1381 /* if not found, break out of loop */
1382 if (offset == -1) break;
1383
1384 /* copy non matching part of input string */
1385 memcpy(new_s, str, offset); /* copy part of str before pat */
1386 str += offset + pat_len; /* move str past pattern */
1387 len -= offset + pat_len; /* reduce length of str remaining */
1388
1389 /* copy substitute into the output string */
1390 new_s += offset; /* move new_s to dest for sub string */
1391 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1392 new_s += sub_len; /* offset new_s past sub string */
1393
1394 /* break when we've done count replacements */
1395 if (--count == 0) break;
1396 }
1397 /* copy any remaining values into output string */
1398 if (len > 0)
1399 memcpy(new_s, str, len);
1400 return out_s;
1401
1402 return_same:
1403 *out_len = -1;
1404 return str;
1405}
1406
1407
1408static char replace__doc__[] =
1409"S.replace (old, new[, maxsplit]) -> string\n\
1410\n\
1411Return a copy of string S with all occurrences of substring\n\
1412old replaced by new. If the optional argument maxsplit is\n\
1413given, only the first maxsplit occurrences are replaced.";
1414
1415static PyObject *
1416string_replace(self, args)
1417 PyStringObject *self;
1418 PyObject *args;
1419{
1420 char *str = PyString_AS_STRING(self), *pat,*sub,*new_s;
1421 int len = PyString_GET_SIZE(self), pat_len,sub_len,out_len;
1422 int count = 0;
1423 PyObject *new;
1424
Guido van Rossum43713e52000-02-29 13:59:29 +00001425 if (!PyArg_ParseTuple(args, "t#t#|i:replace",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001426 &pat, &pat_len, &sub, &sub_len, &count))
1427 return NULL;
1428 if (pat_len <= 0) {
1429 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1430 return NULL;
1431 }
1432 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
1433 if (new_s == NULL) {
1434 PyErr_NoMemory();
1435 return NULL;
1436 }
1437 if (out_len == -1) {
1438 /* we're returning another reference to self */
1439 new = (PyObject*)self;
1440 Py_INCREF(new);
1441 }
1442 else {
1443 new = PyString_FromStringAndSize(new_s, out_len);
1444 free(new_s);
1445 }
1446 return new;
1447}
1448
1449
1450static char startswith__doc__[] =
1451"S.startswith(prefix[, start[, end]]) -> int\n\
1452\n\
1453Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
1454optional start, test S beginning at that position. With optional end, stop\n\
1455comparing S at that position.";
1456
1457static PyObject *
1458string_startswith(self, args)
1459 PyStringObject *self;
1460 PyObject *args;
1461{
1462 char* str = PyString_AS_STRING(self);
1463 int len = PyString_GET_SIZE(self);
1464 char* prefix;
1465 int plen;
1466 int start = 0;
1467 int end = -1;
1468
Guido van Rossum43713e52000-02-29 13:59:29 +00001469 if (!PyArg_ParseTuple(args, "t#|ii:startswith", &prefix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001470 return NULL;
1471
1472 /* adopt Java semantics for index out of range. it is legal for
1473 * offset to be == plen, but this only returns true if prefix is
1474 * the empty string.
1475 */
1476 if (start < 0 || start+plen > len)
1477 return PyInt_FromLong(0);
1478
1479 if (!memcmp(str+start, prefix, plen)) {
1480 /* did the match end after the specified end? */
1481 if (end < 0)
1482 return PyInt_FromLong(1);
1483 else if (end - start < plen)
1484 return PyInt_FromLong(0);
1485 else
1486 return PyInt_FromLong(1);
1487 }
1488 else return PyInt_FromLong(0);
1489}
1490
1491
1492static char endswith__doc__[] =
1493"S.endswith(suffix[, start[, end]]) -> int\n\
1494\n\
1495Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
1496optional start, test S beginning at that position. With optional end, stop\n\
1497comparing S at that position.";
1498
1499static PyObject *
1500string_endswith(self, args)
1501 PyStringObject *self;
1502 PyObject *args;
1503{
1504 char* str = PyString_AS_STRING(self);
1505 int len = PyString_GET_SIZE(self);
1506 char* suffix;
1507 int plen;
1508 int start = 0;
1509 int end = -1;
1510 int lower, upper;
1511
Guido van Rossum43713e52000-02-29 13:59:29 +00001512 if (!PyArg_ParseTuple(args, "t#|ii:endswith", &suffix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001513 return NULL;
1514
1515 if (start < 0 || start > len || plen > len)
1516 return PyInt_FromLong(0);
1517
1518 upper = (end >= 0 && end <= len) ? end : len;
1519 lower = (upper - plen) > start ? (upper - plen) : start;
1520
1521 if (upper-lower >= plen && !memcmp(str+lower, suffix, plen))
1522 return PyInt_FromLong(1);
1523 else return PyInt_FromLong(0);
1524}
1525
1526
1527
1528static PyMethodDef
1529string_methods[] = {
1530 /* counterparts of the obsolete stropmodule functions */
1531 {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__},
1532 {"count", (PyCFunction)string_count, 1, count__doc__},
1533 {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__},
1534 {"find", (PyCFunction)string_find, 1, find__doc__},
1535 {"index", (PyCFunction)string_index, 1, index__doc__},
1536 {"join", (PyCFunction)string_join, 1, join__doc__},
1537 {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__},
1538 {"lower", (PyCFunction)string_lower, 1, lower__doc__},
1539 /* maketrans */
1540 {"replace", (PyCFunction)string_replace, 1, replace__doc__},
1541 {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__},
1542 {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__},
1543 {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__},
1544 {"split", (PyCFunction)string_split, 1, split__doc__},
1545 {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__},
1546 {"strip", (PyCFunction)string_strip, 1, strip__doc__},
1547 {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__},
1548 {"translate", (PyCFunction)string_translate, 1, strip__doc__},
1549 {"upper", (PyCFunction)string_upper, 1, upper__doc__},
1550 /* TBD */
1551/* {"ljust" (PyCFunction)string_ljust, 1, ljust__doc__}, */
1552/* {"rjust" (PyCFunction)string_rjust, 1, rjust__doc__}, */
1553/* {"center" (PyCFunction)string_center, 1, center__doc__}, */
1554/* {"zfill" (PyCFunction)string_zfill, 1, zfill__doc__}, */
1555/* {"expandtabs" (PyCFunction)string_expandtabs, 1, ljust__doc__}, */
1556/* {"capwords" (PyCFunction)string_capwords, 1, capwords__doc__}, */
1557 {NULL, NULL} /* sentinel */
1558};
1559
1560static PyObject *
1561string_getattr(s, name)
1562 PyStringObject *s;
1563 char *name;
1564{
1565 return Py_FindMethod(string_methods, (PyObject*)s, name);
1566}
1567
1568
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001569PyTypeObject PyString_Type = {
1570 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001571 0,
1572 "string",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001573 sizeof(PyStringObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001574 sizeof(char),
Guido van Rossum013142a1994-08-30 08:19:36 +00001575 (destructor)string_dealloc, /*tp_dealloc*/
1576 (printfunc)string_print, /*tp_print*/
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001577 (getattrfunc)string_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001578 0, /*tp_setattr*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001579 (cmpfunc)string_compare, /*tp_compare*/
1580 (reprfunc)string_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581 0, /*tp_as_number*/
1582 &string_as_sequence, /*tp_as_sequence*/
1583 0, /*tp_as_mapping*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001584 (hashfunc)string_hash, /*tp_hash*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001585 0, /*tp_call*/
1586 0, /*tp_str*/
1587 0, /*tp_getattro*/
1588 0, /*tp_setattro*/
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001589 &string_as_buffer, /*tp_as_buffer*/
Guido van Rossum1db70701998-10-08 02:18:52 +00001590 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001591 0, /*tp_doc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001592};
1593
1594void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001595PyString_Concat(pv, w)
1596 register PyObject **pv;
1597 register PyObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001598{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001599 register PyObject *v;
Guido van Rossum013142a1994-08-30 08:19:36 +00001600 if (*pv == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001601 return;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001602 if (w == NULL || !PyString_Check(*pv)) {
1603 Py_DECREF(*pv);
Guido van Rossum013142a1994-08-30 08:19:36 +00001604 *pv = NULL;
1605 return;
1606 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001607 v = string_concat((PyStringObject *) *pv, w);
1608 Py_DECREF(*pv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609 *pv = v;
1610}
1611
Guido van Rossum013142a1994-08-30 08:19:36 +00001612void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001613PyString_ConcatAndDel(pv, w)
1614 register PyObject **pv;
1615 register PyObject *w;
Guido van Rossum013142a1994-08-30 08:19:36 +00001616{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617 PyString_Concat(pv, w);
1618 Py_XDECREF(w);
Guido van Rossum013142a1994-08-30 08:19:36 +00001619}
1620
1621
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001622/* The following function breaks the notion that strings are immutable:
1623 it changes the size of a string. We get away with this only if there
1624 is only one module referencing the object. You can also think of it
1625 as creating a new string object and destroying the old one, only
1626 more efficiently. In any case, don't use this if the string may
1627 already be known to some other part of the code... */
1628
1629int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001630_PyString_Resize(pv, newsize)
1631 PyObject **pv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632 int newsize;
1633{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001634 register PyObject *v;
1635 register PyStringObject *sv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001636 v = *pv;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001637 if (!PyString_Check(v) || v->ob_refcnt != 1) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638 *pv = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001639 Py_DECREF(v);
1640 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001641 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001642 }
Guido van Rossum921842f1990-11-18 17:30:23 +00001643 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +00001644#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +00001645 --_Py_RefTotal;
Guido van Rossum921842f1990-11-18 17:30:23 +00001646#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001647 _Py_ForgetReference(v);
1648 *pv = (PyObject *)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001649 realloc((char *)v,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001650 sizeof(PyStringObject) + newsize * sizeof(char));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001651 if (*pv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001652 PyMem_DEL(v);
1653 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001654 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001656 _Py_NewReference(*pv);
1657 sv = (PyStringObject *) *pv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001658 sv->ob_size = newsize;
1659 sv->ob_sval[newsize] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001660 return 0;
1661}
Guido van Rossume5372401993-03-16 12:15:04 +00001662
1663/* Helpers for formatstring */
1664
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001665static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +00001666getnextarg(args, arglen, p_argidx)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001667 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001668 int arglen;
1669 int *p_argidx;
1670{
1671 int argidx = *p_argidx;
1672 if (argidx < arglen) {
1673 (*p_argidx)++;
1674 if (arglen < 0)
1675 return args;
1676 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001677 return PyTuple_GetItem(args, argidx);
Guido van Rossume5372401993-03-16 12:15:04 +00001678 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679 PyErr_SetString(PyExc_TypeError,
1680 "not enough arguments for format string");
Guido van Rossume5372401993-03-16 12:15:04 +00001681 return NULL;
1682}
1683
1684#define F_LJUST (1<<0)
1685#define F_SIGN (1<<1)
1686#define F_BLANK (1<<2)
1687#define F_ALT (1<<3)
1688#define F_ZERO (1<<4)
1689
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001690static int
1691formatfloat(buf, flags, prec, type, v)
1692 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001693 int flags;
1694 int prec;
1695 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001696 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001697{
1698 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001699 double x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001700 if (!PyArg_Parse(v, "d;float argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001701 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001702 if (prec < 0)
1703 prec = 6;
1704 if (prec > 50)
1705 prec = 50; /* Arbitrary limitation */
1706 if (type == 'f' && fabs(x)/1e25 >= 1e25)
1707 type = 'g';
1708 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
1709 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001710 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001711}
1712
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001713static int
1714formatint(buf, flags, prec, type, v)
1715 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001716 int flags;
1717 int prec;
1718 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001719 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001720{
1721 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001722 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001723 if (!PyArg_Parse(v, "l;int argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001724 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001725 if (prec < 0)
1726 prec = 1;
1727 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
1728 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001729 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001730}
1731
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001732static int
1733formatchar(buf, v)
1734 char *buf;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001735 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001736{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001737 if (PyString_Check(v)) {
1738 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001739 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001740 }
1741 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001742 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001743 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001744 }
1745 buf[1] = '\0';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001746 return 1;
Guido van Rossume5372401993-03-16 12:15:04 +00001747}
1748
Guido van Rossum013142a1994-08-30 08:19:36 +00001749
Guido van Rossume5372401993-03-16 12:15:04 +00001750/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
1751
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001752PyObject *
1753PyString_Format(format, args)
1754 PyObject *format;
1755 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001756{
1757 char *fmt, *res;
1758 int fmtcnt, rescnt, reslen, arglen, argidx;
Guido van Rossum993952b1996-05-21 22:44:20 +00001759 int args_owned = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001760 PyObject *result;
1761 PyObject *dict = NULL;
1762 if (format == NULL || !PyString_Check(format) || args == NULL) {
1763 PyErr_BadInternalCall();
Guido van Rossume5372401993-03-16 12:15:04 +00001764 return NULL;
1765 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001766 fmt = PyString_AsString(format);
1767 fmtcnt = PyString_Size(format);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001768 reslen = rescnt = fmtcnt + 100;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001769 result = PyString_FromStringAndSize((char *)NULL, reslen);
Guido van Rossume5372401993-03-16 12:15:04 +00001770 if (result == NULL)
1771 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001772 res = PyString_AsString(result);
1773 if (PyTuple_Check(args)) {
1774 arglen = PyTuple_Size(args);
Guido van Rossume5372401993-03-16 12:15:04 +00001775 argidx = 0;
1776 }
1777 else {
1778 arglen = -1;
1779 argidx = -2;
1780 }
Guido van Rossum013142a1994-08-30 08:19:36 +00001781 if (args->ob_type->tp_as_mapping)
1782 dict = args;
Guido van Rossume5372401993-03-16 12:15:04 +00001783 while (--fmtcnt >= 0) {
1784 if (*fmt != '%') {
1785 if (--rescnt < 0) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001786 rescnt = fmtcnt + 100;
1787 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001788 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00001789 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001790 res = PyString_AsString(result)
1791 + reslen - rescnt;
Guido van Rossum013142a1994-08-30 08:19:36 +00001792 --rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00001793 }
1794 *res++ = *fmt++;
1795 }
1796 else {
1797 /* Got a format specifier */
1798 int flags = 0;
Guido van Rossume5372401993-03-16 12:15:04 +00001799 int width = -1;
1800 int prec = -1;
1801 int size = 0;
Guido van Rossum6938a291993-11-11 14:51:57 +00001802 int c = '\0';
Guido van Rossume5372401993-03-16 12:15:04 +00001803 int fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001804 PyObject *v = NULL;
1805 PyObject *temp = NULL;
Guido van Rossume5372401993-03-16 12:15:04 +00001806 char *buf;
1807 int sign;
1808 int len;
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001809 char tmpbuf[120]; /* For format{float,int,char}() */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001810 fmt++;
Guido van Rossum013142a1994-08-30 08:19:36 +00001811 if (*fmt == '(') {
1812 char *keystart;
1813 int keylen;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001814 PyObject *key;
Guido van Rossum045e6881997-09-08 18:30:11 +00001815 int pcount = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001816
1817 if (dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001818 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001819 "format requires a mapping");
1820 goto error;
1821 }
1822 ++fmt;
1823 --fmtcnt;
1824 keystart = fmt;
Guido van Rossum045e6881997-09-08 18:30:11 +00001825 /* Skip over balanced parentheses */
1826 while (pcount > 0 && --fmtcnt >= 0) {
1827 if (*fmt == ')')
1828 --pcount;
1829 else if (*fmt == '(')
1830 ++pcount;
Guido van Rossum013142a1994-08-30 08:19:36 +00001831 fmt++;
Guido van Rossum045e6881997-09-08 18:30:11 +00001832 }
1833 keylen = fmt - keystart - 1;
1834 if (fmtcnt < 0 || pcount > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001835 PyErr_SetString(PyExc_ValueError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001836 "incomplete format key");
1837 goto error;
1838 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001839 key = PyString_FromStringAndSize(keystart,
1840 keylen);
Guido van Rossum013142a1994-08-30 08:19:36 +00001841 if (key == NULL)
1842 goto error;
Guido van Rossum993952b1996-05-21 22:44:20 +00001843 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001844 Py_DECREF(args);
Guido van Rossum993952b1996-05-21 22:44:20 +00001845 args_owned = 0;
1846 }
1847 args = PyObject_GetItem(dict, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001848 Py_DECREF(key);
Guido van Rossum013142a1994-08-30 08:19:36 +00001849 if (args == NULL) {
1850 goto error;
1851 }
Guido van Rossum993952b1996-05-21 22:44:20 +00001852 args_owned = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001853 arglen = -1;
1854 argidx = -2;
1855 }
Guido van Rossume5372401993-03-16 12:15:04 +00001856 while (--fmtcnt >= 0) {
1857 switch (c = *fmt++) {
1858 case '-': flags |= F_LJUST; continue;
1859 case '+': flags |= F_SIGN; continue;
1860 case ' ': flags |= F_BLANK; continue;
1861 case '#': flags |= F_ALT; continue;
1862 case '0': flags |= F_ZERO; continue;
1863 }
1864 break;
1865 }
1866 if (c == '*') {
1867 v = getnextarg(args, arglen, &argidx);
1868 if (v == NULL)
1869 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001870 if (!PyInt_Check(v)) {
1871 PyErr_SetString(PyExc_TypeError,
1872 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001873 goto error;
1874 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875 width = PyInt_AsLong(v);
Guido van Rossum98c9eba1999-06-07 15:12:32 +00001876 if (width < 0) {
1877 flags |= F_LJUST;
1878 width = -width;
1879 }
Guido van Rossume5372401993-03-16 12:15:04 +00001880 if (--fmtcnt >= 0)
1881 c = *fmt++;
1882 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001883 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001884 width = c - '0';
1885 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001886 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001887 if (!isdigit(c))
1888 break;
1889 if ((width*10) / 10 != width) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001890 PyErr_SetString(
1891 PyExc_ValueError,
1892 "width too big");
Guido van Rossume5372401993-03-16 12:15:04 +00001893 goto error;
1894 }
1895 width = width*10 + (c - '0');
1896 }
1897 }
1898 if (c == '.') {
1899 prec = 0;
1900 if (--fmtcnt >= 0)
1901 c = *fmt++;
1902 if (c == '*') {
1903 v = getnextarg(args, arglen, &argidx);
1904 if (v == NULL)
1905 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001906 if (!PyInt_Check(v)) {
1907 PyErr_SetString(
1908 PyExc_TypeError,
1909 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001910 goto error;
1911 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001912 prec = PyInt_AsLong(v);
Guido van Rossume5372401993-03-16 12:15:04 +00001913 if (prec < 0)
1914 prec = 0;
1915 if (--fmtcnt >= 0)
1916 c = *fmt++;
1917 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001918 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001919 prec = c - '0';
1920 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001921 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001922 if (!isdigit(c))
1923 break;
1924 if ((prec*10) / 10 != prec) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001925 PyErr_SetString(
1926 PyExc_ValueError,
Guido van Rossume5372401993-03-16 12:15:04 +00001927 "prec too big");
1928 goto error;
1929 }
1930 prec = prec*10 + (c - '0');
1931 }
1932 }
1933 } /* prec */
1934 if (fmtcnt >= 0) {
1935 if (c == 'h' || c == 'l' || c == 'L') {
1936 size = c;
1937 if (--fmtcnt >= 0)
1938 c = *fmt++;
1939 }
1940 }
1941 if (fmtcnt < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001942 PyErr_SetString(PyExc_ValueError,
1943 "incomplete format");
Guido van Rossume5372401993-03-16 12:15:04 +00001944 goto error;
1945 }
1946 if (c != '%') {
1947 v = getnextarg(args, arglen, &argidx);
1948 if (v == NULL)
1949 goto error;
1950 }
1951 sign = 0;
1952 fill = ' ';
1953 switch (c) {
1954 case '%':
1955 buf = "%";
1956 len = 1;
1957 break;
1958 case 's':
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001959 temp = PyObject_Str(v);
Guido van Rossum013142a1994-08-30 08:19:36 +00001960 if (temp == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +00001961 goto error;
Guido van Rossum4a0144c1998-06-09 15:08:41 +00001962 if (!PyString_Check(temp)) {
1963 PyErr_SetString(PyExc_TypeError,
1964 "%s argument has non-string str()");
1965 goto error;
1966 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001967 buf = PyString_AsString(temp);
1968 len = PyString_Size(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00001969 if (prec >= 0 && len > prec)
1970 len = prec;
1971 break;
1972 case 'i':
1973 case 'd':
1974 case 'u':
1975 case 'o':
1976 case 'x':
1977 case 'X':
1978 if (c == 'i')
1979 c = 'd';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001980 buf = tmpbuf;
1981 len = formatint(buf, flags, prec, c, v);
1982 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00001983 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00001984 sign = (c == 'd');
Guido van Rossum4acdc231997-01-29 06:00:24 +00001985 if (flags&F_ZERO) {
Guido van Rossume5372401993-03-16 12:15:04 +00001986 fill = '0';
Guido van Rossum4acdc231997-01-29 06:00:24 +00001987 if ((flags&F_ALT) &&
1988 (c == 'x' || c == 'X') &&
1989 buf[0] == '0' && buf[1] == c) {
1990 *res++ = *buf++;
1991 *res++ = *buf++;
1992 rescnt -= 2;
1993 len -= 2;
1994 width -= 2;
1995 if (width < 0)
1996 width = 0;
1997 }
1998 }
Guido van Rossume5372401993-03-16 12:15:04 +00001999 break;
2000 case 'e':
2001 case 'E':
2002 case 'f':
2003 case 'g':
2004 case 'G':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002005 buf = tmpbuf;
2006 len = formatfloat(buf, flags, prec, c, v);
2007 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002008 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002009 sign = 1;
2010 if (flags&F_ZERO)
2011 fill = '0';
2012 break;
2013 case 'c':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002014 buf = tmpbuf;
2015 len = formatchar(buf, v);
2016 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002017 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002018 break;
2019 default:
Guido van Rossum045e6881997-09-08 18:30:11 +00002020 PyErr_Format(PyExc_ValueError,
2021 "unsupported format character '%c' (0x%x)",
2022 c, c);
Guido van Rossume5372401993-03-16 12:15:04 +00002023 goto error;
2024 }
2025 if (sign) {
2026 if (*buf == '-' || *buf == '+') {
2027 sign = *buf++;
2028 len--;
2029 }
2030 else if (flags & F_SIGN)
2031 sign = '+';
2032 else if (flags & F_BLANK)
2033 sign = ' ';
2034 else
2035 sign = '\0';
2036 }
2037 if (width < len)
2038 width = len;
2039 if (rescnt < width + (sign != '\0')) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00002040 reslen -= rescnt;
2041 rescnt = width + fmtcnt + 100;
2042 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002043 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002044 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002045 res = PyString_AsString(result)
2046 + reslen - rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00002047 }
2048 if (sign) {
Guido van Rossum71e57d01993-11-11 15:03:51 +00002049 if (fill != ' ')
2050 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002051 rescnt--;
2052 if (width > len)
2053 width--;
2054 }
2055 if (width > len && !(flags&F_LJUST)) {
2056 do {
2057 --rescnt;
2058 *res++ = fill;
2059 } while (--width > len);
2060 }
Guido van Rossum71e57d01993-11-11 15:03:51 +00002061 if (sign && fill == ' ')
Guido van Rossum6938a291993-11-11 14:51:57 +00002062 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002063 memcpy(res, buf, len);
2064 res += len;
2065 rescnt -= len;
2066 while (--width >= len) {
2067 --rescnt;
2068 *res++ = ' ';
2069 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002070 if (dict && (argidx < arglen) && c != '%') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002071 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00002072 "not all arguments converted");
2073 goto error;
2074 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002075 Py_XDECREF(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00002076 } /* '%' */
2077 } /* until end */
Guido van Rossumcaeaafc1995-02-27 10:13:23 +00002078 if (argidx < arglen && !dict) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002079 PyErr_SetString(PyExc_TypeError,
2080 "not all arguments converted");
Guido van Rossume5372401993-03-16 12:15:04 +00002081 goto error;
2082 }
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002083 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002084 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002085 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002086 _PyString_Resize(&result, reslen - rescnt);
Guido van Rossume5372401993-03-16 12:15:04 +00002087 return result;
2088 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002089 Py_DECREF(result);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002090 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002091 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002092 }
Guido van Rossume5372401993-03-16 12:15:04 +00002093 return NULL;
2094}
Guido van Rossum2a61e741997-01-18 07:55:05 +00002095
2096
2097#ifdef INTERN_STRINGS
2098
2099static PyObject *interned;
2100
2101void
2102PyString_InternInPlace(p)
2103 PyObject **p;
2104{
2105 register PyStringObject *s = (PyStringObject *)(*p);
2106 PyObject *t;
2107 if (s == NULL || !PyString_Check(s))
2108 Py_FatalError("PyString_InternInPlace: strings only please!");
2109 if ((t = s->ob_sinterned) != NULL) {
2110 if (t == (PyObject *)s)
2111 return;
2112 Py_INCREF(t);
2113 *p = t;
2114 Py_DECREF(s);
2115 return;
2116 }
2117 if (interned == NULL) {
2118 interned = PyDict_New();
2119 if (interned == NULL)
2120 return;
Guido van Rossum2a61e741997-01-18 07:55:05 +00002121 }
2122 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
2123 Py_INCREF(t);
2124 *p = s->ob_sinterned = t;
2125 Py_DECREF(s);
2126 return;
2127 }
2128 t = (PyObject *)s;
2129 if (PyDict_SetItem(interned, t, t) == 0) {
2130 s->ob_sinterned = t;
2131 return;
2132 }
2133 PyErr_Clear();
2134}
2135
2136
2137PyObject *
2138PyString_InternFromString(cp)
2139 const char *cp;
2140{
2141 PyObject *s = PyString_FromString(cp);
2142 if (s == NULL)
2143 return NULL;
2144 PyString_InternInPlace(&s);
2145 return s;
2146}
2147
2148#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002149
2150void
2151PyString_Fini()
2152{
2153 int i;
Guido van Rossum8cf04761997-08-02 02:57:45 +00002154 for (i = 0; i < UCHAR_MAX + 1; i++) {
2155 Py_XDECREF(characters[i]);
2156 characters[i] = NULL;
2157 }
2158#ifndef DONT_SHARE_SHORT_STRINGS
2159 Py_XDECREF(nullstring);
2160 nullstring = NULL;
2161#endif
Guido van Rossum971a7aa1997-08-05 02:15:12 +00002162#ifdef INTERN_STRINGS
2163 if (interned) {
2164 int pos, changed;
2165 PyObject *key, *value;
2166 do {
2167 changed = 0;
2168 pos = 0;
2169 while (PyDict_Next(interned, &pos, &key, &value)) {
2170 if (key->ob_refcnt == 2 && key == value) {
2171 PyDict_DelItem(interned, key);
2172 changed = 1;
2173 }
2174 }
2175 } while (changed);
2176 }
2177#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002178}