blob: bc1bb41538b38535ca4bd53e977e6773fd9633dc [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) {
Barry Warsawbf325832000-03-06 14:52:18 +0000712 if (_PyString_Resize(&res, sz*2)) {
713 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000714 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000715 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000716 sz *= 2;
717 p = PyString_AsString(res) + reslen;
718 }
719 if (i > 0) {
720 memcpy(p, sep, seplen);
721 p += seplen;
722 reslen += seplen;
723 }
724 memcpy(p, PyString_AS_STRING(sitem), slen);
Barry Warsawbf325832000-03-06 14:52:18 +0000725 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000726 p += slen;
727 reslen += slen;
728 }
729 }
730 else {
731 for (i = 0; i < seqlen; i++) {
732 PyObject *item = PySequence_GetItem(seq, i);
733 PyObject *sitem;
Barry Warsawbf325832000-03-06 14:52:18 +0000734
735 if (!item)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000736 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000737 sitem = PyObject_Str(item);
738 Py_DECREF(item);
739 if (!sitem)
740 goto finally;
741
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000742 slen = PyString_GET_SIZE(sitem);
743 while (reslen + slen + seplen >= sz) {
Barry Warsawbf325832000-03-06 14:52:18 +0000744 if (_PyString_Resize(&res, sz*2)) {
745 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000746 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000747 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000748 sz *= 2;
749 p = PyString_AsString(res) + reslen;
750 }
751 if (i > 0) {
752 memcpy(p, sep, seplen);
753 p += seplen;
754 reslen += seplen;
755 }
756 memcpy(p, PyString_AS_STRING(sitem), slen);
Barry Warsawbf325832000-03-06 14:52:18 +0000757 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000758 p += slen;
759 reslen += slen;
760 }
761 }
762 if (_PyString_Resize(&res, reslen))
763 goto finally;
764 return res;
765
766 finally:
767 Py_DECREF(res);
768 return NULL;
769}
770
771
772
773static long
774string_find_internal(self, args)
775 PyStringObject *self;
776 PyObject *args;
777{
778 char *s = PyString_AS_STRING(self), *sub;
779 int len = PyString_GET_SIZE(self);
780 int n, i = 0, last = INT_MAX;
781
Guido van Rossum43713e52000-02-29 13:59:29 +0000782 if (!PyArg_ParseTuple(args, "t#|ii:find", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000783 return -2;
784
785 if (last > len)
786 last = len;
787 if (last < 0)
788 last += len;
789 if (last < 0)
790 last = 0;
791 if (i < 0)
792 i += len;
793 if (i < 0)
794 i = 0;
795
796 if (n == 0 && i <= last)
797 return (long)i;
798
799 last -= n;
800 for (; i <= last; ++i)
801 if (s[i] == sub[0] &&
802 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
803 return (long)i;
804
805 return -1;
806}
807
808
809static char find__doc__[] =
810"S.find(sub [,start [,end]]) -> int\n\
811\n\
812Return the lowest index in S where substring sub is found,\n\
813such that sub is contained within s[start,end]. Optional\n\
814arguments start and end are interpreted as in slice notation.\n\
815\n\
816Return -1 on failure.";
817
818static PyObject *
819string_find(self, args)
820 PyStringObject *self;
821 PyObject *args;
822{
823 long result = string_find_internal(self, args);
824 if (result == -2)
825 return NULL;
826 return PyInt_FromLong(result);
827}
828
829
830static char index__doc__[] =
831"S.index(sub [,start [,end]]) -> int\n\
832\n\
833Like S.find() but raise ValueError when the substring is not found.";
834
835static PyObject *
836string_index(self, args)
837 PyStringObject *self;
838 PyObject *args;
839{
840 long result = string_find_internal(self, args);
841 if (result == -2)
842 return NULL;
843 if (result == -1) {
844 PyErr_SetString(PyExc_ValueError,
845 "substring not found in string.index");
846 return NULL;
847 }
848 return PyInt_FromLong(result);
849}
850
851
852static long
853string_rfind_internal(self, args)
854 PyStringObject *self;
855 PyObject *args;
856{
857 char *s = PyString_AS_STRING(self), *sub;
858 int len = PyString_GET_SIZE(self), n, j;
859 int i = 0, last = INT_MAX;
860
Guido van Rossum43713e52000-02-29 13:59:29 +0000861 if (!PyArg_ParseTuple(args, "t#|ii:rfind", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000862 return -2;
863
864 if (last > len)
865 last = len;
866 if (last < 0)
867 last += len;
868 if (last < 0)
869 last = 0;
870 if (i < 0)
871 i += len;
872 if (i < 0)
873 i = 0;
874
875 if (n == 0 && i <= last)
876 return (long)last;
877
878 for (j = last-n; j >= i; --j)
879 if (s[j] == sub[0] &&
880 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
881 return (long)j;
882
883 return -1;
884}
885
886
887static char rfind__doc__[] =
888"S.rfind(sub [,start [,end]]) -> int\n\
889\n\
890Return the highest index in S where substring sub is found,\n\
891such that sub is contained within s[start,end]. Optional\n\
892arguments start and end are interpreted as in slice notation.\n\
893\n\
894Return -1 on failure.";
895
896static PyObject *
897string_rfind(self, args)
898 PyStringObject *self;
899 PyObject *args;
900{
901 long result = string_rfind_internal(self, args);
902 if (result == -2)
903 return NULL;
904 return PyInt_FromLong(result);
905}
906
907
908static char rindex__doc__[] =
909"S.rindex(sub [,start [,end]]) -> int\n\
910\n\
911Like S.rfind() but raise ValueError when the substring is not found.";
912
913static PyObject *
914string_rindex(self, args)
915 PyStringObject *self;
916 PyObject *args;
917{
918 long result = string_rfind_internal(self, args);
919 if (result == -2)
920 return NULL;
921 if (result == -1) {
922 PyErr_SetString(PyExc_ValueError,
923 "substring not found in string.rindex");
924 return NULL;
925 }
926 return PyInt_FromLong(result);
927}
928
929
930static PyObject *
931do_strip(self, args, striptype)
932 PyStringObject *self;
933 PyObject *args;
934 int striptype;
935{
936 char *s = PyString_AS_STRING(self);
937 int len = PyString_GET_SIZE(self), i, j;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000938
Guido van Rossum43713e52000-02-29 13:59:29 +0000939 if (!PyArg_ParseTuple(args, ":strip"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000940 return NULL;
941
942 i = 0;
943 if (striptype != RIGHTSTRIP) {
944 while (i < len && isspace(Py_CHARMASK(s[i]))) {
945 i++;
946 }
947 }
948
949 j = len;
950 if (striptype != LEFTSTRIP) {
951 do {
952 j--;
953 } while (j >= i && isspace(Py_CHARMASK(s[j])));
954 j++;
955 }
956
957 if (i == 0 && j == len) {
958 Py_INCREF(self);
959 return (PyObject*)self;
960 }
961 else
962 return PyString_FromStringAndSize(s+i, j-i);
963}
964
965
966static char strip__doc__[] =
967"S.strip() -> string\n\
968\n\
969Return a copy of the string S with leading and trailing\n\
970whitespace removed.";
971
972static PyObject *
973string_strip(self, args)
974 PyStringObject *self;
975 PyObject *args;
976{
977 return do_strip(self, args, BOTHSTRIP);
978}
979
980
981static char lstrip__doc__[] =
982"S.lstrip() -> string\n\
983\n\
984Return a copy of the string S with leading whitespace removed.";
985
986static PyObject *
987string_lstrip(self, args)
988 PyStringObject *self;
989 PyObject *args;
990{
991 return do_strip(self, args, LEFTSTRIP);
992}
993
994
995static char rstrip__doc__[] =
996"S.rstrip() -> string\n\
997\n\
998Return a copy of the string S with trailing whitespace removed.";
999
1000static PyObject *
1001string_rstrip(self, args)
1002 PyStringObject *self;
1003 PyObject *args;
1004{
1005 return do_strip(self, args, RIGHTSTRIP);
1006}
1007
1008
1009static char lower__doc__[] =
1010"S.lower() -> string\n\
1011\n\
1012Return a copy of the string S converted to lowercase.";
1013
1014static PyObject *
1015string_lower(self, args)
1016 PyStringObject *self;
1017 PyObject *args;
1018{
1019 char *s = PyString_AS_STRING(self), *s_new;
1020 int i, n = PyString_GET_SIZE(self);
1021 PyObject *new;
1022
Guido van Rossum43713e52000-02-29 13:59:29 +00001023 if (!PyArg_ParseTuple(args, ":lower"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001024 return NULL;
1025 new = PyString_FromStringAndSize(NULL, n);
1026 if (new == NULL)
1027 return NULL;
1028 s_new = PyString_AsString(new);
1029 for (i = 0; i < n; i++) {
1030 int c = Py_CHARMASK(*s++);
1031 if (isupper(c)) {
1032 *s_new = tolower(c);
1033 } else
1034 *s_new = c;
1035 s_new++;
1036 }
1037 return new;
1038}
1039
1040
1041static char upper__doc__[] =
1042"S.upper() -> string\n\
1043\n\
1044Return a copy of the string S converted to uppercase.";
1045
1046static PyObject *
1047string_upper(self, args)
1048 PyStringObject *self;
1049 PyObject *args;
1050{
1051 char *s = PyString_AS_STRING(self), *s_new;
1052 int i, n = PyString_GET_SIZE(self);
1053 PyObject *new;
1054
Guido van Rossum43713e52000-02-29 13:59:29 +00001055 if (!PyArg_ParseTuple(args, ":upper"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001056 return NULL;
1057 new = PyString_FromStringAndSize(NULL, n);
1058 if (new == NULL)
1059 return NULL;
1060 s_new = PyString_AsString(new);
1061 for (i = 0; i < n; i++) {
1062 int c = Py_CHARMASK(*s++);
1063 if (islower(c)) {
1064 *s_new = toupper(c);
1065 } else
1066 *s_new = c;
1067 s_new++;
1068 }
1069 return new;
1070}
1071
1072
1073static char capitalize__doc__[] =
1074"S.capitalize() -> string\n\
1075\n\
1076Return a copy of the string S with only its first character\n\
1077capitalized.";
1078
1079static PyObject *
1080string_capitalize(self, args)
1081 PyStringObject *self;
1082 PyObject *args;
1083{
1084 char *s = PyString_AS_STRING(self), *s_new;
1085 int i, n = PyString_GET_SIZE(self);
1086 PyObject *new;
1087
Guido van Rossum43713e52000-02-29 13:59:29 +00001088 if (!PyArg_ParseTuple(args, ":capitalize"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001089 return NULL;
1090 new = PyString_FromStringAndSize(NULL, n);
1091 if (new == NULL)
1092 return NULL;
1093 s_new = PyString_AsString(new);
1094 if (0 < n) {
1095 int c = Py_CHARMASK(*s++);
1096 if (islower(c))
1097 *s_new = toupper(c);
1098 else
1099 *s_new = c;
1100 s_new++;
1101 }
1102 for (i = 1; i < n; i++) {
1103 int c = Py_CHARMASK(*s++);
1104 if (isupper(c))
1105 *s_new = tolower(c);
1106 else
1107 *s_new = c;
1108 s_new++;
1109 }
1110 return new;
1111}
1112
1113
1114static char count__doc__[] =
1115"S.count(sub[, start[, end]]) -> int\n\
1116\n\
1117Return the number of occurrences of substring sub in string\n\
1118S[start:end]. Optional arguments start and end are\n\
1119interpreted as in slice notation.";
1120
1121static PyObject *
1122string_count(self, args)
1123 PyStringObject *self;
1124 PyObject *args;
1125{
1126 char *s = PyString_AS_STRING(self), *sub;
1127 int len = PyString_GET_SIZE(self), n;
1128 int i = 0, last = INT_MAX;
1129 int m, r;
1130
Guido van Rossum43713e52000-02-29 13:59:29 +00001131 if (!PyArg_ParseTuple(args, "t#|ii:count", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001132 return NULL;
1133 if (last > len)
1134 last = len;
1135 if (last < 0)
1136 last += len;
1137 if (last < 0)
1138 last = 0;
1139 if (i < 0)
1140 i += len;
1141 if (i < 0)
1142 i = 0;
1143 m = last + 1 - n;
1144 if (n == 0)
1145 return PyInt_FromLong((long) (m-i));
1146
1147 r = 0;
1148 while (i < m) {
1149 if (!memcmp(s+i, sub, n)) {
1150 r++;
1151 i += n;
1152 } else {
1153 i++;
1154 }
1155 }
1156 return PyInt_FromLong((long) r);
1157}
1158
1159
1160static char swapcase__doc__[] =
1161"S.swapcase() -> string\n\
1162\n\
1163Return a copy of the string S with upper case characters\n\
1164converted to lowercase and vice versa.";
1165
1166static PyObject *
1167string_swapcase(self, args)
1168 PyStringObject *self;
1169 PyObject *args;
1170{
1171 char *s = PyString_AS_STRING(self), *s_new;
1172 int i, n = PyString_GET_SIZE(self);
1173 PyObject *new;
1174
Guido van Rossum43713e52000-02-29 13:59:29 +00001175 if (!PyArg_ParseTuple(args, ":swapcase"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001176 return NULL;
1177 new = PyString_FromStringAndSize(NULL, n);
1178 if (new == NULL)
1179 return NULL;
1180 s_new = PyString_AsString(new);
1181 for (i = 0; i < n; i++) {
1182 int c = Py_CHARMASK(*s++);
1183 if (islower(c)) {
1184 *s_new = toupper(c);
1185 }
1186 else if (isupper(c)) {
1187 *s_new = tolower(c);
1188 }
1189 else
1190 *s_new = c;
1191 s_new++;
1192 }
1193 return new;
1194}
1195
1196
1197static char translate__doc__[] =
1198"S.translate(table [,deletechars]) -> string\n\
1199\n\
1200Return a copy of the string S, where all characters occurring\n\
1201in the optional argument deletechars are removed, and the\n\
1202remaining characters have been mapped through the given\n\
1203translation table, which must be a string of length 256.";
1204
1205static PyObject *
1206string_translate(self, args)
1207 PyStringObject *self;
1208 PyObject *args;
1209{
1210 register char *input, *table, *output;
1211 register int i, c, changed = 0;
1212 PyObject *input_obj = (PyObject*)self;
1213 char *table1, *output_start, *del_table=NULL;
1214 int inlen, tablen, dellen = 0;
1215 PyObject *result;
1216 int trans_table[256];
1217
Guido van Rossum43713e52000-02-29 13:59:29 +00001218 if (!PyArg_ParseTuple(args, "t#|t#:translate",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001219 &table1, &tablen, &del_table, &dellen))
1220 return NULL;
1221 if (tablen != 256) {
1222 PyErr_SetString(PyExc_ValueError,
1223 "translation table must be 256 characters long");
1224 return NULL;
1225 }
1226
1227 table = table1;
1228 inlen = PyString_Size(input_obj);
1229 result = PyString_FromStringAndSize((char *)NULL, inlen);
1230 if (result == NULL)
1231 return NULL;
1232 output_start = output = PyString_AsString(result);
1233 input = PyString_AsString(input_obj);
1234
1235 if (dellen == 0) {
1236 /* If no deletions are required, use faster code */
1237 for (i = inlen; --i >= 0; ) {
1238 c = Py_CHARMASK(*input++);
1239 if (Py_CHARMASK((*output++ = table[c])) != c)
1240 changed = 1;
1241 }
1242 if (changed)
1243 return result;
1244 Py_DECREF(result);
1245 Py_INCREF(input_obj);
1246 return input_obj;
1247 }
1248
1249 for (i = 0; i < 256; i++)
1250 trans_table[i] = Py_CHARMASK(table[i]);
1251
1252 for (i = 0; i < dellen; i++)
1253 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
1254
1255 for (i = inlen; --i >= 0; ) {
1256 c = Py_CHARMASK(*input++);
1257 if (trans_table[c] != -1)
1258 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1259 continue;
1260 changed = 1;
1261 }
1262 if (!changed) {
1263 Py_DECREF(result);
1264 Py_INCREF(input_obj);
1265 return input_obj;
1266 }
1267 /* Fix the size of the resulting string */
1268 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
1269 return NULL;
1270 return result;
1271}
1272
1273
1274/* What follows is used for implementing replace(). Perry Stoll. */
1275
1276/*
1277 mymemfind
1278
1279 strstr replacement for arbitrary blocks of memory.
1280
1281 Locates the first occurance in the memory pointed to by MEM of the
1282 contents of memory pointed to by PAT. Returns the index into MEM if
1283 found, or -1 if not found. If len of PAT is greater than length of
1284 MEM, the function returns -1.
1285*/
1286static int
1287mymemfind(mem, len, pat, pat_len)
1288 char *mem;
1289 int len;
1290 char *pat;
1291 int pat_len;
1292{
1293 register int ii;
1294
1295 /* pattern can not occur in the last pat_len-1 chars */
1296 len -= pat_len;
1297
1298 for (ii = 0; ii <= len; ii++) {
1299 if (mem[ii] == pat[0] &&
1300 (pat_len == 1 ||
1301 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1302 return ii;
1303 }
1304 }
1305 return -1;
1306}
1307
1308/*
1309 mymemcnt
1310
1311 Return the number of distinct times PAT is found in MEM.
1312 meaning mem=1111 and pat==11 returns 2.
1313 mem=11111 and pat==11 also return 2.
1314 */
1315static int
1316mymemcnt(mem, len, pat, pat_len)
1317 char *mem;
1318 int len;
1319 char *pat;
1320 int pat_len;
1321{
1322 register int offset = 0;
1323 int nfound = 0;
1324
1325 while (len >= 0) {
1326 offset = mymemfind(mem, len, pat, pat_len);
1327 if (offset == -1)
1328 break;
1329 mem += offset + pat_len;
1330 len -= offset + pat_len;
1331 nfound++;
1332 }
1333 return nfound;
1334}
1335
1336/*
1337 mymemreplace
1338
1339 Return a string in which all occurences of PAT in memory STR are
1340 replaced with SUB.
1341
1342 If length of PAT is less than length of STR or there are no occurences
1343 of PAT in STR, then the original string is returned. Otherwise, a new
1344 string is allocated here and returned.
1345
1346 on return, out_len is:
1347 the length of output string, or
1348 -1 if the input string is returned, or
1349 unchanged if an error occurs (no memory).
1350
1351 return value is:
1352 the new string allocated locally, or
1353 NULL if an error occurred.
1354*/
1355static char *
1356mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
1357 char *str;
1358 int len; /* input string */
1359 char *pat;
1360 int pat_len; /* pattern string to find */
1361 char *sub;
1362 int sub_len; /* substitution string */
1363 int count; /* number of replacements, 0 == all */
1364 int *out_len;
1365
1366{
1367 char *out_s;
1368 char *new_s;
1369 int nfound, offset, new_len;
1370
1371 if (len == 0 || pat_len > len)
1372 goto return_same;
1373
1374 /* find length of output string */
1375 nfound = mymemcnt(str, len, pat, pat_len);
1376 if (count > 0)
1377 nfound = nfound > count ? count : nfound;
1378 if (nfound == 0)
1379 goto return_same;
1380 new_len = len + nfound*(sub_len - pat_len);
1381
1382 new_s = (char *)malloc(new_len);
1383 if (new_s == NULL) return NULL;
1384
1385 *out_len = new_len;
1386 out_s = new_s;
1387
1388 while (len > 0) {
1389 /* find index of next instance of pattern */
1390 offset = mymemfind(str, len, pat, pat_len);
1391 /* if not found, break out of loop */
1392 if (offset == -1) break;
1393
1394 /* copy non matching part of input string */
1395 memcpy(new_s, str, offset); /* copy part of str before pat */
1396 str += offset + pat_len; /* move str past pattern */
1397 len -= offset + pat_len; /* reduce length of str remaining */
1398
1399 /* copy substitute into the output string */
1400 new_s += offset; /* move new_s to dest for sub string */
1401 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1402 new_s += sub_len; /* offset new_s past sub string */
1403
1404 /* break when we've done count replacements */
1405 if (--count == 0) break;
1406 }
1407 /* copy any remaining values into output string */
1408 if (len > 0)
1409 memcpy(new_s, str, len);
1410 return out_s;
1411
1412 return_same:
1413 *out_len = -1;
1414 return str;
1415}
1416
1417
1418static char replace__doc__[] =
1419"S.replace (old, new[, maxsplit]) -> string\n\
1420\n\
1421Return a copy of string S with all occurrences of substring\n\
1422old replaced by new. If the optional argument maxsplit is\n\
1423given, only the first maxsplit occurrences are replaced.";
1424
1425static PyObject *
1426string_replace(self, args)
1427 PyStringObject *self;
1428 PyObject *args;
1429{
1430 char *str = PyString_AS_STRING(self), *pat,*sub,*new_s;
1431 int len = PyString_GET_SIZE(self), pat_len,sub_len,out_len;
1432 int count = 0;
1433 PyObject *new;
1434
Guido van Rossum43713e52000-02-29 13:59:29 +00001435 if (!PyArg_ParseTuple(args, "t#t#|i:replace",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001436 &pat, &pat_len, &sub, &sub_len, &count))
1437 return NULL;
1438 if (pat_len <= 0) {
1439 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1440 return NULL;
1441 }
1442 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
1443 if (new_s == NULL) {
1444 PyErr_NoMemory();
1445 return NULL;
1446 }
1447 if (out_len == -1) {
1448 /* we're returning another reference to self */
1449 new = (PyObject*)self;
1450 Py_INCREF(new);
1451 }
1452 else {
1453 new = PyString_FromStringAndSize(new_s, out_len);
1454 free(new_s);
1455 }
1456 return new;
1457}
1458
1459
1460static char startswith__doc__[] =
1461"S.startswith(prefix[, start[, end]]) -> int\n\
1462\n\
1463Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
1464optional start, test S beginning at that position. With optional end, stop\n\
1465comparing S at that position.";
1466
1467static PyObject *
1468string_startswith(self, args)
1469 PyStringObject *self;
1470 PyObject *args;
1471{
1472 char* str = PyString_AS_STRING(self);
1473 int len = PyString_GET_SIZE(self);
1474 char* prefix;
1475 int plen;
1476 int start = 0;
1477 int end = -1;
1478
Guido van Rossum43713e52000-02-29 13:59:29 +00001479 if (!PyArg_ParseTuple(args, "t#|ii:startswith", &prefix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001480 return NULL;
1481
1482 /* adopt Java semantics for index out of range. it is legal for
1483 * offset to be == plen, but this only returns true if prefix is
1484 * the empty string.
1485 */
1486 if (start < 0 || start+plen > len)
1487 return PyInt_FromLong(0);
1488
1489 if (!memcmp(str+start, prefix, plen)) {
1490 /* did the match end after the specified end? */
1491 if (end < 0)
1492 return PyInt_FromLong(1);
1493 else if (end - start < plen)
1494 return PyInt_FromLong(0);
1495 else
1496 return PyInt_FromLong(1);
1497 }
1498 else return PyInt_FromLong(0);
1499}
1500
1501
1502static char endswith__doc__[] =
1503"S.endswith(suffix[, start[, end]]) -> int\n\
1504\n\
1505Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
1506optional start, test S beginning at that position. With optional end, stop\n\
1507comparing S at that position.";
1508
1509static PyObject *
1510string_endswith(self, args)
1511 PyStringObject *self;
1512 PyObject *args;
1513{
1514 char* str = PyString_AS_STRING(self);
1515 int len = PyString_GET_SIZE(self);
1516 char* suffix;
1517 int plen;
1518 int start = 0;
1519 int end = -1;
1520 int lower, upper;
1521
Guido van Rossum43713e52000-02-29 13:59:29 +00001522 if (!PyArg_ParseTuple(args, "t#|ii:endswith", &suffix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001523 return NULL;
1524
1525 if (start < 0 || start > len || plen > len)
1526 return PyInt_FromLong(0);
1527
1528 upper = (end >= 0 && end <= len) ? end : len;
1529 lower = (upper - plen) > start ? (upper - plen) : start;
1530
1531 if (upper-lower >= plen && !memcmp(str+lower, suffix, plen))
1532 return PyInt_FromLong(1);
1533 else return PyInt_FromLong(0);
1534}
1535
1536
1537
1538static PyMethodDef
1539string_methods[] = {
1540 /* counterparts of the obsolete stropmodule functions */
1541 {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__},
1542 {"count", (PyCFunction)string_count, 1, count__doc__},
1543 {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__},
1544 {"find", (PyCFunction)string_find, 1, find__doc__},
1545 {"index", (PyCFunction)string_index, 1, index__doc__},
1546 {"join", (PyCFunction)string_join, 1, join__doc__},
1547 {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__},
1548 {"lower", (PyCFunction)string_lower, 1, lower__doc__},
1549 /* maketrans */
1550 {"replace", (PyCFunction)string_replace, 1, replace__doc__},
1551 {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__},
1552 {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__},
1553 {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__},
1554 {"split", (PyCFunction)string_split, 1, split__doc__},
1555 {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__},
1556 {"strip", (PyCFunction)string_strip, 1, strip__doc__},
1557 {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__},
1558 {"translate", (PyCFunction)string_translate, 1, strip__doc__},
1559 {"upper", (PyCFunction)string_upper, 1, upper__doc__},
1560 /* TBD */
1561/* {"ljust" (PyCFunction)string_ljust, 1, ljust__doc__}, */
1562/* {"rjust" (PyCFunction)string_rjust, 1, rjust__doc__}, */
1563/* {"center" (PyCFunction)string_center, 1, center__doc__}, */
1564/* {"zfill" (PyCFunction)string_zfill, 1, zfill__doc__}, */
1565/* {"expandtabs" (PyCFunction)string_expandtabs, 1, ljust__doc__}, */
1566/* {"capwords" (PyCFunction)string_capwords, 1, capwords__doc__}, */
1567 {NULL, NULL} /* sentinel */
1568};
1569
1570static PyObject *
1571string_getattr(s, name)
1572 PyStringObject *s;
1573 char *name;
1574{
1575 return Py_FindMethod(string_methods, (PyObject*)s, name);
1576}
1577
1578
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001579PyTypeObject PyString_Type = {
1580 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581 0,
1582 "string",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001583 sizeof(PyStringObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001584 sizeof(char),
Guido van Rossum013142a1994-08-30 08:19:36 +00001585 (destructor)string_dealloc, /*tp_dealloc*/
1586 (printfunc)string_print, /*tp_print*/
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001587 (getattrfunc)string_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001588 0, /*tp_setattr*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001589 (cmpfunc)string_compare, /*tp_compare*/
1590 (reprfunc)string_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001591 0, /*tp_as_number*/
1592 &string_as_sequence, /*tp_as_sequence*/
1593 0, /*tp_as_mapping*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001594 (hashfunc)string_hash, /*tp_hash*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001595 0, /*tp_call*/
1596 0, /*tp_str*/
1597 0, /*tp_getattro*/
1598 0, /*tp_setattro*/
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001599 &string_as_buffer, /*tp_as_buffer*/
Guido van Rossum1db70701998-10-08 02:18:52 +00001600 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001601 0, /*tp_doc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001602};
1603
1604void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001605PyString_Concat(pv, w)
1606 register PyObject **pv;
1607 register PyObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001608{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609 register PyObject *v;
Guido van Rossum013142a1994-08-30 08:19:36 +00001610 if (*pv == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001611 return;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001612 if (w == NULL || !PyString_Check(*pv)) {
1613 Py_DECREF(*pv);
Guido van Rossum013142a1994-08-30 08:19:36 +00001614 *pv = NULL;
1615 return;
1616 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617 v = string_concat((PyStringObject *) *pv, w);
1618 Py_DECREF(*pv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001619 *pv = v;
1620}
1621
Guido van Rossum013142a1994-08-30 08:19:36 +00001622void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001623PyString_ConcatAndDel(pv, w)
1624 register PyObject **pv;
1625 register PyObject *w;
Guido van Rossum013142a1994-08-30 08:19:36 +00001626{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627 PyString_Concat(pv, w);
1628 Py_XDECREF(w);
Guido van Rossum013142a1994-08-30 08:19:36 +00001629}
1630
1631
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632/* The following function breaks the notion that strings are immutable:
1633 it changes the size of a string. We get away with this only if there
1634 is only one module referencing the object. You can also think of it
1635 as creating a new string object and destroying the old one, only
1636 more efficiently. In any case, don't use this if the string may
1637 already be known to some other part of the code... */
1638
1639int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001640_PyString_Resize(pv, newsize)
1641 PyObject **pv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001642 int newsize;
1643{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001644 register PyObject *v;
1645 register PyStringObject *sv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001646 v = *pv;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001647 if (!PyString_Check(v) || v->ob_refcnt != 1) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648 *pv = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001649 Py_DECREF(v);
1650 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001651 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001652 }
Guido van Rossum921842f1990-11-18 17:30:23 +00001653 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +00001654#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +00001655 --_Py_RefTotal;
Guido van Rossum921842f1990-11-18 17:30:23 +00001656#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001657 _Py_ForgetReference(v);
1658 *pv = (PyObject *)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659 realloc((char *)v,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001660 sizeof(PyStringObject) + newsize * sizeof(char));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661 if (*pv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001662 PyMem_DEL(v);
1663 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001664 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001666 _Py_NewReference(*pv);
1667 sv = (PyStringObject *) *pv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001668 sv->ob_size = newsize;
1669 sv->ob_sval[newsize] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670 return 0;
1671}
Guido van Rossume5372401993-03-16 12:15:04 +00001672
1673/* Helpers for formatstring */
1674
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001675static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +00001676getnextarg(args, arglen, p_argidx)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001677 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001678 int arglen;
1679 int *p_argidx;
1680{
1681 int argidx = *p_argidx;
1682 if (argidx < arglen) {
1683 (*p_argidx)++;
1684 if (arglen < 0)
1685 return args;
1686 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001687 return PyTuple_GetItem(args, argidx);
Guido van Rossume5372401993-03-16 12:15:04 +00001688 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001689 PyErr_SetString(PyExc_TypeError,
1690 "not enough arguments for format string");
Guido van Rossume5372401993-03-16 12:15:04 +00001691 return NULL;
1692}
1693
1694#define F_LJUST (1<<0)
1695#define F_SIGN (1<<1)
1696#define F_BLANK (1<<2)
1697#define F_ALT (1<<3)
1698#define F_ZERO (1<<4)
1699
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001700static int
1701formatfloat(buf, flags, prec, type, v)
1702 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001703 int flags;
1704 int prec;
1705 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001706 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001707{
1708 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001709 double x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001710 if (!PyArg_Parse(v, "d;float argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001711 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001712 if (prec < 0)
1713 prec = 6;
1714 if (prec > 50)
1715 prec = 50; /* Arbitrary limitation */
1716 if (type == 'f' && fabs(x)/1e25 >= 1e25)
1717 type = 'g';
1718 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
1719 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001720 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001721}
1722
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001723static int
1724formatint(buf, flags, prec, type, v)
1725 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001726 int flags;
1727 int prec;
1728 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001729 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001730{
1731 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001732 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001733 if (!PyArg_Parse(v, "l;int argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001734 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001735 if (prec < 0)
1736 prec = 1;
1737 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
1738 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001739 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001740}
1741
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001742static int
1743formatchar(buf, v)
1744 char *buf;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001745 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001746{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001747 if (PyString_Check(v)) {
1748 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001749 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001750 }
1751 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001752 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001753 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001754 }
1755 buf[1] = '\0';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001756 return 1;
Guido van Rossume5372401993-03-16 12:15:04 +00001757}
1758
Guido van Rossum013142a1994-08-30 08:19:36 +00001759
Guido van Rossume5372401993-03-16 12:15:04 +00001760/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
1761
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001762PyObject *
1763PyString_Format(format, args)
1764 PyObject *format;
1765 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001766{
1767 char *fmt, *res;
1768 int fmtcnt, rescnt, reslen, arglen, argidx;
Guido van Rossum993952b1996-05-21 22:44:20 +00001769 int args_owned = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001770 PyObject *result;
1771 PyObject *dict = NULL;
1772 if (format == NULL || !PyString_Check(format) || args == NULL) {
1773 PyErr_BadInternalCall();
Guido van Rossume5372401993-03-16 12:15:04 +00001774 return NULL;
1775 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001776 fmt = PyString_AsString(format);
1777 fmtcnt = PyString_Size(format);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001778 reslen = rescnt = fmtcnt + 100;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001779 result = PyString_FromStringAndSize((char *)NULL, reslen);
Guido van Rossume5372401993-03-16 12:15:04 +00001780 if (result == NULL)
1781 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001782 res = PyString_AsString(result);
1783 if (PyTuple_Check(args)) {
1784 arglen = PyTuple_Size(args);
Guido van Rossume5372401993-03-16 12:15:04 +00001785 argidx = 0;
1786 }
1787 else {
1788 arglen = -1;
1789 argidx = -2;
1790 }
Guido van Rossum013142a1994-08-30 08:19:36 +00001791 if (args->ob_type->tp_as_mapping)
1792 dict = args;
Guido van Rossume5372401993-03-16 12:15:04 +00001793 while (--fmtcnt >= 0) {
1794 if (*fmt != '%') {
1795 if (--rescnt < 0) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001796 rescnt = fmtcnt + 100;
1797 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001798 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00001799 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001800 res = PyString_AsString(result)
1801 + reslen - rescnt;
Guido van Rossum013142a1994-08-30 08:19:36 +00001802 --rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00001803 }
1804 *res++ = *fmt++;
1805 }
1806 else {
1807 /* Got a format specifier */
1808 int flags = 0;
Guido van Rossume5372401993-03-16 12:15:04 +00001809 int width = -1;
1810 int prec = -1;
1811 int size = 0;
Guido van Rossum6938a291993-11-11 14:51:57 +00001812 int c = '\0';
Guido van Rossume5372401993-03-16 12:15:04 +00001813 int fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001814 PyObject *v = NULL;
1815 PyObject *temp = NULL;
Guido van Rossume5372401993-03-16 12:15:04 +00001816 char *buf;
1817 int sign;
1818 int len;
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001819 char tmpbuf[120]; /* For format{float,int,char}() */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001820 fmt++;
Guido van Rossum013142a1994-08-30 08:19:36 +00001821 if (*fmt == '(') {
1822 char *keystart;
1823 int keylen;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001824 PyObject *key;
Guido van Rossum045e6881997-09-08 18:30:11 +00001825 int pcount = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001826
1827 if (dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001828 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001829 "format requires a mapping");
1830 goto error;
1831 }
1832 ++fmt;
1833 --fmtcnt;
1834 keystart = fmt;
Guido van Rossum045e6881997-09-08 18:30:11 +00001835 /* Skip over balanced parentheses */
1836 while (pcount > 0 && --fmtcnt >= 0) {
1837 if (*fmt == ')')
1838 --pcount;
1839 else if (*fmt == '(')
1840 ++pcount;
Guido van Rossum013142a1994-08-30 08:19:36 +00001841 fmt++;
Guido van Rossum045e6881997-09-08 18:30:11 +00001842 }
1843 keylen = fmt - keystart - 1;
1844 if (fmtcnt < 0 || pcount > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001845 PyErr_SetString(PyExc_ValueError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001846 "incomplete format key");
1847 goto error;
1848 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001849 key = PyString_FromStringAndSize(keystart,
1850 keylen);
Guido van Rossum013142a1994-08-30 08:19:36 +00001851 if (key == NULL)
1852 goto error;
Guido van Rossum993952b1996-05-21 22:44:20 +00001853 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001854 Py_DECREF(args);
Guido van Rossum993952b1996-05-21 22:44:20 +00001855 args_owned = 0;
1856 }
1857 args = PyObject_GetItem(dict, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001858 Py_DECREF(key);
Guido van Rossum013142a1994-08-30 08:19:36 +00001859 if (args == NULL) {
1860 goto error;
1861 }
Guido van Rossum993952b1996-05-21 22:44:20 +00001862 args_owned = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001863 arglen = -1;
1864 argidx = -2;
1865 }
Guido van Rossume5372401993-03-16 12:15:04 +00001866 while (--fmtcnt >= 0) {
1867 switch (c = *fmt++) {
1868 case '-': flags |= F_LJUST; continue;
1869 case '+': flags |= F_SIGN; continue;
1870 case ' ': flags |= F_BLANK; continue;
1871 case '#': flags |= F_ALT; continue;
1872 case '0': flags |= F_ZERO; continue;
1873 }
1874 break;
1875 }
1876 if (c == '*') {
1877 v = getnextarg(args, arglen, &argidx);
1878 if (v == NULL)
1879 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001880 if (!PyInt_Check(v)) {
1881 PyErr_SetString(PyExc_TypeError,
1882 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001883 goto error;
1884 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001885 width = PyInt_AsLong(v);
Guido van Rossum98c9eba1999-06-07 15:12:32 +00001886 if (width < 0) {
1887 flags |= F_LJUST;
1888 width = -width;
1889 }
Guido van Rossume5372401993-03-16 12:15:04 +00001890 if (--fmtcnt >= 0)
1891 c = *fmt++;
1892 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001893 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001894 width = c - '0';
1895 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001896 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001897 if (!isdigit(c))
1898 break;
1899 if ((width*10) / 10 != width) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001900 PyErr_SetString(
1901 PyExc_ValueError,
1902 "width too big");
Guido van Rossume5372401993-03-16 12:15:04 +00001903 goto error;
1904 }
1905 width = width*10 + (c - '0');
1906 }
1907 }
1908 if (c == '.') {
1909 prec = 0;
1910 if (--fmtcnt >= 0)
1911 c = *fmt++;
1912 if (c == '*') {
1913 v = getnextarg(args, arglen, &argidx);
1914 if (v == NULL)
1915 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001916 if (!PyInt_Check(v)) {
1917 PyErr_SetString(
1918 PyExc_TypeError,
1919 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001920 goto error;
1921 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001922 prec = PyInt_AsLong(v);
Guido van Rossume5372401993-03-16 12:15:04 +00001923 if (prec < 0)
1924 prec = 0;
1925 if (--fmtcnt >= 0)
1926 c = *fmt++;
1927 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001928 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001929 prec = c - '0';
1930 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001931 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001932 if (!isdigit(c))
1933 break;
1934 if ((prec*10) / 10 != prec) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001935 PyErr_SetString(
1936 PyExc_ValueError,
Guido van Rossume5372401993-03-16 12:15:04 +00001937 "prec too big");
1938 goto error;
1939 }
1940 prec = prec*10 + (c - '0');
1941 }
1942 }
1943 } /* prec */
1944 if (fmtcnt >= 0) {
1945 if (c == 'h' || c == 'l' || c == 'L') {
1946 size = c;
1947 if (--fmtcnt >= 0)
1948 c = *fmt++;
1949 }
1950 }
1951 if (fmtcnt < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001952 PyErr_SetString(PyExc_ValueError,
1953 "incomplete format");
Guido van Rossume5372401993-03-16 12:15:04 +00001954 goto error;
1955 }
1956 if (c != '%') {
1957 v = getnextarg(args, arglen, &argidx);
1958 if (v == NULL)
1959 goto error;
1960 }
1961 sign = 0;
1962 fill = ' ';
1963 switch (c) {
1964 case '%':
1965 buf = "%";
1966 len = 1;
1967 break;
1968 case 's':
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001969 temp = PyObject_Str(v);
Guido van Rossum013142a1994-08-30 08:19:36 +00001970 if (temp == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +00001971 goto error;
Guido van Rossum4a0144c1998-06-09 15:08:41 +00001972 if (!PyString_Check(temp)) {
1973 PyErr_SetString(PyExc_TypeError,
1974 "%s argument has non-string str()");
1975 goto error;
1976 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001977 buf = PyString_AsString(temp);
1978 len = PyString_Size(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00001979 if (prec >= 0 && len > prec)
1980 len = prec;
1981 break;
1982 case 'i':
1983 case 'd':
1984 case 'u':
1985 case 'o':
1986 case 'x':
1987 case 'X':
1988 if (c == 'i')
1989 c = 'd';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001990 buf = tmpbuf;
1991 len = formatint(buf, flags, prec, c, v);
1992 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00001993 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00001994 sign = (c == 'd');
Guido van Rossum4acdc231997-01-29 06:00:24 +00001995 if (flags&F_ZERO) {
Guido van Rossume5372401993-03-16 12:15:04 +00001996 fill = '0';
Guido van Rossum4acdc231997-01-29 06:00:24 +00001997 if ((flags&F_ALT) &&
1998 (c == 'x' || c == 'X') &&
1999 buf[0] == '0' && buf[1] == c) {
2000 *res++ = *buf++;
2001 *res++ = *buf++;
2002 rescnt -= 2;
2003 len -= 2;
2004 width -= 2;
2005 if (width < 0)
2006 width = 0;
2007 }
2008 }
Guido van Rossume5372401993-03-16 12:15:04 +00002009 break;
2010 case 'e':
2011 case 'E':
2012 case 'f':
2013 case 'g':
2014 case 'G':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002015 buf = tmpbuf;
2016 len = formatfloat(buf, flags, prec, c, v);
2017 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002018 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002019 sign = 1;
2020 if (flags&F_ZERO)
2021 fill = '0';
2022 break;
2023 case 'c':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002024 buf = tmpbuf;
2025 len = formatchar(buf, v);
2026 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002027 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002028 break;
2029 default:
Guido van Rossum045e6881997-09-08 18:30:11 +00002030 PyErr_Format(PyExc_ValueError,
2031 "unsupported format character '%c' (0x%x)",
2032 c, c);
Guido van Rossume5372401993-03-16 12:15:04 +00002033 goto error;
2034 }
2035 if (sign) {
2036 if (*buf == '-' || *buf == '+') {
2037 sign = *buf++;
2038 len--;
2039 }
2040 else if (flags & F_SIGN)
2041 sign = '+';
2042 else if (flags & F_BLANK)
2043 sign = ' ';
2044 else
2045 sign = '\0';
2046 }
2047 if (width < len)
2048 width = len;
2049 if (rescnt < width + (sign != '\0')) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00002050 reslen -= rescnt;
2051 rescnt = width + fmtcnt + 100;
2052 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002053 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002054 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002055 res = PyString_AsString(result)
2056 + reslen - rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00002057 }
2058 if (sign) {
Guido van Rossum71e57d01993-11-11 15:03:51 +00002059 if (fill != ' ')
2060 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002061 rescnt--;
2062 if (width > len)
2063 width--;
2064 }
2065 if (width > len && !(flags&F_LJUST)) {
2066 do {
2067 --rescnt;
2068 *res++ = fill;
2069 } while (--width > len);
2070 }
Guido van Rossum71e57d01993-11-11 15:03:51 +00002071 if (sign && fill == ' ')
Guido van Rossum6938a291993-11-11 14:51:57 +00002072 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002073 memcpy(res, buf, len);
2074 res += len;
2075 rescnt -= len;
2076 while (--width >= len) {
2077 --rescnt;
2078 *res++ = ' ';
2079 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002080 if (dict && (argidx < arglen) && c != '%') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002081 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00002082 "not all arguments converted");
2083 goto error;
2084 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002085 Py_XDECREF(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00002086 } /* '%' */
2087 } /* until end */
Guido van Rossumcaeaafc1995-02-27 10:13:23 +00002088 if (argidx < arglen && !dict) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002089 PyErr_SetString(PyExc_TypeError,
2090 "not all arguments converted");
Guido van Rossume5372401993-03-16 12:15:04 +00002091 goto error;
2092 }
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002093 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002094 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002095 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002096 _PyString_Resize(&result, reslen - rescnt);
Guido van Rossume5372401993-03-16 12:15:04 +00002097 return result;
2098 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002099 Py_DECREF(result);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002100 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002101 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002102 }
Guido van Rossume5372401993-03-16 12:15:04 +00002103 return NULL;
2104}
Guido van Rossum2a61e741997-01-18 07:55:05 +00002105
2106
2107#ifdef INTERN_STRINGS
2108
2109static PyObject *interned;
2110
2111void
2112PyString_InternInPlace(p)
2113 PyObject **p;
2114{
2115 register PyStringObject *s = (PyStringObject *)(*p);
2116 PyObject *t;
2117 if (s == NULL || !PyString_Check(s))
2118 Py_FatalError("PyString_InternInPlace: strings only please!");
2119 if ((t = s->ob_sinterned) != NULL) {
2120 if (t == (PyObject *)s)
2121 return;
2122 Py_INCREF(t);
2123 *p = t;
2124 Py_DECREF(s);
2125 return;
2126 }
2127 if (interned == NULL) {
2128 interned = PyDict_New();
2129 if (interned == NULL)
2130 return;
Guido van Rossum2a61e741997-01-18 07:55:05 +00002131 }
2132 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
2133 Py_INCREF(t);
2134 *p = s->ob_sinterned = t;
2135 Py_DECREF(s);
2136 return;
2137 }
2138 t = (PyObject *)s;
2139 if (PyDict_SetItem(interned, t, t) == 0) {
2140 s->ob_sinterned = t;
2141 return;
2142 }
2143 PyErr_Clear();
2144}
2145
2146
2147PyObject *
2148PyString_InternFromString(cp)
2149 const char *cp;
2150{
2151 PyObject *s = PyString_FromString(cp);
2152 if (s == NULL)
2153 return NULL;
2154 PyString_InternInPlace(&s);
2155 return s;
2156}
2157
2158#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002159
2160void
2161PyString_Fini()
2162{
2163 int i;
Guido van Rossum8cf04761997-08-02 02:57:45 +00002164 for (i = 0; i < UCHAR_MAX + 1; i++) {
2165 Py_XDECREF(characters[i]);
2166 characters[i] = NULL;
2167 }
2168#ifndef DONT_SHARE_SHORT_STRINGS
2169 Py_XDECREF(nullstring);
2170 nullstring = NULL;
2171#endif
Guido van Rossum971a7aa1997-08-05 02:15:12 +00002172#ifdef INTERN_STRINGS
2173 if (interned) {
2174 int pos, changed;
2175 PyObject *key, *value;
2176 do {
2177 changed = 0;
2178 pos = 0;
2179 while (PyDict_Next(interned, &pos, &key, &value)) {
2180 if (key->ob_refcnt == 2 && key == value) {
2181 PyDict_DelItem(interned, key);
2182 changed = 1;
2183 }
2184 }
2185 } while (changed);
2186 }
2187#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002188}