blob: 77c08dd749c1573fd7c95be1824c089b19f41b1a [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 Rossum9284a572000-03-07 15:53:43 +0000384static int
385string_contains(a, el)
386PyObject *a, *el;
387{
388 register char *s, *end;
389 register char c;
390 if (!PyString_Check(el) || PyString_Size(el) != 1) {
391 PyErr_SetString(PyExc_TypeError,
392 "string member test needs char left operand");
393 return -1;
394 }
395 c = PyString_AsString(el)[0];
396 s = PyString_AsString(a);
397 end = s + PyString_Size(a);
398 while (s < end) {
399 if (c == *s++)
400 return 1;
401 }
402 return 0;
403}
404
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +0000406string_item(a, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyStringObject *a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408 register int i;
409{
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000410 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 PyErr_SetString(PyExc_IndexError, "string index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414 return NULL;
415 }
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000416 c = a->ob_sval[i] & UCHAR_MAX;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 v = (PyObject *) characters[c];
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000418#ifdef COUNT_ALLOCS
419 if (v != NULL)
420 one_strings++;
421#endif
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000422 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 v = PyString_FromStringAndSize((char *)NULL, 1);
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000424 if (v == NULL)
425 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426 characters[c] = (PyStringObject *) v;
427 ((PyStringObject *)v)->ob_sval[0] = c;
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000428 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 Py_INCREF(v);
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000430 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431}
432
433static int
Guido van Rossume5372401993-03-16 12:15:04 +0000434string_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 PyStringObject *a, *b;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436{
Guido van Rossum253919f1991-02-13 23:18:39 +0000437 int len_a = a->ob_size, len_b = b->ob_size;
438 int min_len = (len_a < len_b) ? len_a : len_b;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000439 int cmp;
440 if (min_len > 0) {
Guido van Rossumfde7a751996-10-23 14:19:40 +0000441 cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000442 if (cmp == 0)
443 cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
444 if (cmp != 0)
445 return cmp;
446 }
Guido van Rossum253919f1991-02-13 23:18:39 +0000447 return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448}
449
Guido van Rossum9bfef441993-03-29 10:43:31 +0000450static long
451string_hash(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 PyStringObject *a;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000453{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000454 register int len;
455 register unsigned char *p;
456 register long x;
457
458#ifdef CACHE_HASH
459 if (a->ob_shash != -1)
460 return a->ob_shash;
Guido van Rossum36b9f791997-02-14 16:29:22 +0000461#ifdef INTERN_STRINGS
462 if (a->ob_sinterned != NULL)
463 return (a->ob_shash =
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 ((PyStringObject *)(a->ob_sinterned))->ob_shash);
Guido van Rossum36b9f791997-02-14 16:29:22 +0000465#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000466#endif
467 len = a->ob_size;
468 p = (unsigned char *) a->ob_sval;
469 x = *p << 7;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000470 while (--len >= 0)
Guido van Rossumeddcb3b1996-09-11 20:22:48 +0000471 x = (1000003*x) ^ *p++;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000472 x ^= a->ob_size;
473 if (x == -1)
474 x = -2;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000475#ifdef CACHE_HASH
476 a->ob_shash = x;
477#endif
Guido van Rossum9bfef441993-03-29 10:43:31 +0000478 return x;
479}
480
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000481static int
482string_buffer_getreadbuf(self, index, ptr)
483 PyStringObject *self;
484 int index;
485 const void **ptr;
486{
487 if ( index != 0 ) {
Guido van Rossum045e6881997-09-08 18:30:11 +0000488 PyErr_SetString(PyExc_SystemError,
Guido van Rossum1db70701998-10-08 02:18:52 +0000489 "accessing non-existent string segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000490 return -1;
491 }
492 *ptr = (void *)self->ob_sval;
493 return self->ob_size;
494}
495
496static int
497string_buffer_getwritebuf(self, index, ptr)
498 PyStringObject *self;
499 int index;
500 const void **ptr;
501{
Guido van Rossum045e6881997-09-08 18:30:11 +0000502 PyErr_SetString(PyExc_TypeError,
Guido van Rossum07d78001998-10-01 15:59:48 +0000503 "Cannot use string as modifiable buffer");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000504 return -1;
505}
506
507static int
508string_buffer_getsegcount(self, lenp)
509 PyStringObject *self;
510 int *lenp;
511{
512 if ( lenp )
513 *lenp = self->ob_size;
514 return 1;
515}
516
Guido van Rossum1db70701998-10-08 02:18:52 +0000517static int
518string_buffer_getcharbuf(self, index, ptr)
519 PyStringObject *self;
520 int index;
521 const char **ptr;
522{
523 if ( index != 0 ) {
524 PyErr_SetString(PyExc_SystemError,
525 "accessing non-existent string segment");
526 return -1;
527 }
528 *ptr = self->ob_sval;
529 return self->ob_size;
530}
531
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532static PySequenceMethods string_as_sequence = {
Guido van Rossum013142a1994-08-30 08:19:36 +0000533 (inquiry)string_length, /*sq_length*/
534 (binaryfunc)string_concat, /*sq_concat*/
535 (intargfunc)string_repeat, /*sq_repeat*/
536 (intargfunc)string_item, /*sq_item*/
537 (intintargfunc)string_slice, /*sq_slice*/
Guido van Rossumf380e661991-06-04 19:36:32 +0000538 0, /*sq_ass_item*/
539 0, /*sq_ass_slice*/
Guido van Rossum9284a572000-03-07 15:53:43 +0000540 (objobjproc)string_contains /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541};
542
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000543static PyBufferProcs string_as_buffer = {
544 (getreadbufferproc)string_buffer_getreadbuf,
545 (getwritebufferproc)string_buffer_getwritebuf,
546 (getsegcountproc)string_buffer_getsegcount,
Guido van Rossum1db70701998-10-08 02:18:52 +0000547 (getcharbufferproc)string_buffer_getcharbuf,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000548};
549
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000550
551
552#define LEFTSTRIP 0
553#define RIGHTSTRIP 1
554#define BOTHSTRIP 2
555
556
557static PyObject *
558split_whitespace(s, len, maxsplit)
559 char *s;
560 int len;
561 int maxsplit;
562{
563 int i = 0, j, err;
564 int countsplit = 0;
565 PyObject* item;
566 PyObject *list = PyList_New(0);
567
568 if (list == NULL)
569 return NULL;
570
571 while (i < len) {
572 while (i < len && isspace(Py_CHARMASK(s[i]))) {
573 i = i+1;
574 }
575 j = i;
576 while (i < len && !isspace(Py_CHARMASK(s[i]))) {
577 i = i+1;
578 }
579 if (j < i) {
580 item = PyString_FromStringAndSize(s+j, (int)(i-j));
581 if (item == NULL)
582 goto finally;
583
584 err = PyList_Append(list, item);
585 Py_DECREF(item);
586 if (err < 0)
587 goto finally;
588
589 countsplit++;
590 while (i < len && isspace(Py_CHARMASK(s[i]))) {
591 i = i+1;
592 }
593 if (maxsplit && (countsplit >= maxsplit) && i < len) {
594 item = PyString_FromStringAndSize(
595 s+i, (int)(len - i));
596 if (item == NULL)
597 goto finally;
598
599 err = PyList_Append(list, item);
600 Py_DECREF(item);
601 if (err < 0)
602 goto finally;
603
604 i = len;
605 }
606 }
607 }
608 return list;
609 finally:
610 Py_DECREF(list);
611 return NULL;
612}
613
614
615static char split__doc__[] =
616"S.split([sep [,maxsplit]]) -> list of strings\n\
617\n\
618Return a list of the words in the string S, using sep as the\n\
619delimiter string. If maxsplit is nonzero, splits into at most\n\
620maxsplit words If sep is not specified, any whitespace string\n\
621is a separator. Maxsplit defaults to 0.";
622
623static PyObject *
624string_split(self, args)
625 PyStringObject *self;
626 PyObject *args;
627{
628 int len = PyString_GET_SIZE(self), n, i, j, err;
629 int splitcount, maxsplit;
630 char *s = PyString_AS_STRING(self), *sub;
631 PyObject *list, *item;
632
633 sub = NULL;
634 n = 0;
635 splitcount = 0;
636 maxsplit = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000637 if (!PyArg_ParseTuple(args, "|z#i:split", &sub, &n, &maxsplit))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000638 return NULL;
639 if (sub == NULL)
640 return split_whitespace(s, len, maxsplit);
641 if (n == 0) {
642 PyErr_SetString(PyExc_ValueError, "empty separator");
643 return NULL;
644 }
645
646 list = PyList_New(0);
647 if (list == NULL)
648 return NULL;
649
650 i = j = 0;
651 while (i+n <= len) {
652 if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
653 item = PyString_FromStringAndSize(s+j, (int)(i-j));
654 if (item == NULL)
655 goto fail;
656 err = PyList_Append(list, item);
657 Py_DECREF(item);
658 if (err < 0)
659 goto fail;
660 i = j = i + n;
661 splitcount++;
662 if (maxsplit && (splitcount >= maxsplit))
663 break;
664 }
665 else
666 i++;
667 }
668 item = PyString_FromStringAndSize(s+j, (int)(len-j));
669 if (item == NULL)
670 goto fail;
671 err = PyList_Append(list, item);
672 Py_DECREF(item);
673 if (err < 0)
674 goto fail;
675
676 return list;
677
678 fail:
679 Py_DECREF(list);
680 return NULL;
681}
682
683
684static char join__doc__[] =
685"S.join(sequence) -> string\n\
686\n\
687Return a string which is the concatenation of the string representation\n\
688of every element in the sequence. The separator between elements is S.";
689
690static PyObject *
691string_join(self, args)
692 PyStringObject *self;
693 PyObject *args;
694{
695 char *sep = PyString_AS_STRING(self);
696 int seplen = PyString_GET_SIZE(self);
697 PyObject *res = NULL;
698 int reslen = 0;
699 char *p;
700 int seqlen = 0;
701 int sz = 100;
702 int i, slen;
703 PyObject *seq;
704
Guido van Rossum43713e52000-02-29 13:59:29 +0000705 if (!PyArg_ParseTuple(args, "O:join", &seq))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000706 return NULL;
707
708 seqlen = PySequence_Length(seq);
709 if (seqlen < 0 && PyErr_Occurred())
710 return NULL;
711
712 if (seqlen == 1) {
713 /* Optimization if there's only one item */
714 PyObject *item = PySequence_GetItem(seq, 0);
715 PyObject *stritem = PyObject_Str(item);
716 Py_DECREF(item);
717 return stritem;
718 }
719 if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
720 return NULL;
721 p = PyString_AsString(res);
722
723 /* optimize for lists. all others (tuples and arbitrary sequences)
724 * just use the abstract interface.
725 */
726 if (PyList_Check(seq)) {
727 for (i = 0; i < seqlen; i++) {
728 PyObject *item = PyList_GET_ITEM(seq, i);
729 PyObject *sitem = PyObject_Str(item);
730 if (!sitem)
731 goto finally;
732 slen = PyString_GET_SIZE(sitem);
733 while (reslen + slen + seplen >= sz) {
Barry Warsawbf325832000-03-06 14:52:18 +0000734 if (_PyString_Resize(&res, sz*2)) {
735 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000736 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000737 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000738 sz *= 2;
739 p = PyString_AsString(res) + reslen;
740 }
741 if (i > 0) {
742 memcpy(p, sep, seplen);
743 p += seplen;
744 reslen += seplen;
745 }
746 memcpy(p, PyString_AS_STRING(sitem), slen);
Barry Warsawbf325832000-03-06 14:52:18 +0000747 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000748 p += slen;
749 reslen += slen;
750 }
751 }
752 else {
753 for (i = 0; i < seqlen; i++) {
754 PyObject *item = PySequence_GetItem(seq, i);
755 PyObject *sitem;
Barry Warsawbf325832000-03-06 14:52:18 +0000756
757 if (!item)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000758 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000759 sitem = PyObject_Str(item);
760 Py_DECREF(item);
761 if (!sitem)
762 goto finally;
763
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000764 slen = PyString_GET_SIZE(sitem);
765 while (reslen + slen + seplen >= sz) {
Barry Warsawbf325832000-03-06 14:52:18 +0000766 if (_PyString_Resize(&res, sz*2)) {
767 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000768 goto finally;
Barry Warsawbf325832000-03-06 14:52:18 +0000769 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000770 sz *= 2;
771 p = PyString_AsString(res) + reslen;
772 }
773 if (i > 0) {
774 memcpy(p, sep, seplen);
775 p += seplen;
776 reslen += seplen;
777 }
778 memcpy(p, PyString_AS_STRING(sitem), slen);
Barry Warsawbf325832000-03-06 14:52:18 +0000779 Py_DECREF(sitem);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000780 p += slen;
781 reslen += slen;
782 }
783 }
784 if (_PyString_Resize(&res, reslen))
785 goto finally;
786 return res;
787
788 finally:
789 Py_DECREF(res);
790 return NULL;
791}
792
793
794
795static long
796string_find_internal(self, args)
797 PyStringObject *self;
798 PyObject *args;
799{
800 char *s = PyString_AS_STRING(self), *sub;
801 int len = PyString_GET_SIZE(self);
802 int n, i = 0, last = INT_MAX;
803
Guido van Rossum43713e52000-02-29 13:59:29 +0000804 if (!PyArg_ParseTuple(args, "t#|ii:find", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000805 return -2;
806
807 if (last > len)
808 last = len;
809 if (last < 0)
810 last += len;
811 if (last < 0)
812 last = 0;
813 if (i < 0)
814 i += len;
815 if (i < 0)
816 i = 0;
817
818 if (n == 0 && i <= last)
819 return (long)i;
820
821 last -= n;
822 for (; i <= last; ++i)
823 if (s[i] == sub[0] &&
824 (n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
825 return (long)i;
826
827 return -1;
828}
829
830
831static char find__doc__[] =
832"S.find(sub [,start [,end]]) -> int\n\
833\n\
834Return the lowest index in S where substring sub is found,\n\
835such that sub is contained within s[start,end]. Optional\n\
836arguments start and end are interpreted as in slice notation.\n\
837\n\
838Return -1 on failure.";
839
840static PyObject *
841string_find(self, args)
842 PyStringObject *self;
843 PyObject *args;
844{
845 long result = string_find_internal(self, args);
846 if (result == -2)
847 return NULL;
848 return PyInt_FromLong(result);
849}
850
851
852static char index__doc__[] =
853"S.index(sub [,start [,end]]) -> int\n\
854\n\
855Like S.find() but raise ValueError when the substring is not found.";
856
857static PyObject *
858string_index(self, args)
859 PyStringObject *self;
860 PyObject *args;
861{
862 long result = string_find_internal(self, args);
863 if (result == -2)
864 return NULL;
865 if (result == -1) {
866 PyErr_SetString(PyExc_ValueError,
867 "substring not found in string.index");
868 return NULL;
869 }
870 return PyInt_FromLong(result);
871}
872
873
874static long
875string_rfind_internal(self, args)
876 PyStringObject *self;
877 PyObject *args;
878{
879 char *s = PyString_AS_STRING(self), *sub;
880 int len = PyString_GET_SIZE(self), n, j;
881 int i = 0, last = INT_MAX;
882
Guido van Rossum43713e52000-02-29 13:59:29 +0000883 if (!PyArg_ParseTuple(args, "t#|ii:rfind", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000884 return -2;
885
886 if (last > len)
887 last = len;
888 if (last < 0)
889 last += len;
890 if (last < 0)
891 last = 0;
892 if (i < 0)
893 i += len;
894 if (i < 0)
895 i = 0;
896
897 if (n == 0 && i <= last)
898 return (long)last;
899
900 for (j = last-n; j >= i; --j)
901 if (s[j] == sub[0] &&
902 (n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
903 return (long)j;
904
905 return -1;
906}
907
908
909static char rfind__doc__[] =
910"S.rfind(sub [,start [,end]]) -> int\n\
911\n\
912Return the highest index in S where substring sub is found,\n\
913such that sub is contained within s[start,end]. Optional\n\
914arguments start and end are interpreted as in slice notation.\n\
915\n\
916Return -1 on failure.";
917
918static PyObject *
919string_rfind(self, args)
920 PyStringObject *self;
921 PyObject *args;
922{
923 long result = string_rfind_internal(self, args);
924 if (result == -2)
925 return NULL;
926 return PyInt_FromLong(result);
927}
928
929
930static char rindex__doc__[] =
931"S.rindex(sub [,start [,end]]) -> int\n\
932\n\
933Like S.rfind() but raise ValueError when the substring is not found.";
934
935static PyObject *
936string_rindex(self, args)
937 PyStringObject *self;
938 PyObject *args;
939{
940 long result = string_rfind_internal(self, args);
941 if (result == -2)
942 return NULL;
943 if (result == -1) {
944 PyErr_SetString(PyExc_ValueError,
945 "substring not found in string.rindex");
946 return NULL;
947 }
948 return PyInt_FromLong(result);
949}
950
951
952static PyObject *
953do_strip(self, args, striptype)
954 PyStringObject *self;
955 PyObject *args;
956 int striptype;
957{
958 char *s = PyString_AS_STRING(self);
959 int len = PyString_GET_SIZE(self), i, j;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000960
Guido van Rossum43713e52000-02-29 13:59:29 +0000961 if (!PyArg_ParseTuple(args, ":strip"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000962 return NULL;
963
964 i = 0;
965 if (striptype != RIGHTSTRIP) {
966 while (i < len && isspace(Py_CHARMASK(s[i]))) {
967 i++;
968 }
969 }
970
971 j = len;
972 if (striptype != LEFTSTRIP) {
973 do {
974 j--;
975 } while (j >= i && isspace(Py_CHARMASK(s[j])));
976 j++;
977 }
978
979 if (i == 0 && j == len) {
980 Py_INCREF(self);
981 return (PyObject*)self;
982 }
983 else
984 return PyString_FromStringAndSize(s+i, j-i);
985}
986
987
988static char strip__doc__[] =
989"S.strip() -> string\n\
990\n\
991Return a copy of the string S with leading and trailing\n\
992whitespace removed.";
993
994static PyObject *
995string_strip(self, args)
996 PyStringObject *self;
997 PyObject *args;
998{
999 return do_strip(self, args, BOTHSTRIP);
1000}
1001
1002
1003static char lstrip__doc__[] =
1004"S.lstrip() -> string\n\
1005\n\
1006Return a copy of the string S with leading whitespace removed.";
1007
1008static PyObject *
1009string_lstrip(self, args)
1010 PyStringObject *self;
1011 PyObject *args;
1012{
1013 return do_strip(self, args, LEFTSTRIP);
1014}
1015
1016
1017static char rstrip__doc__[] =
1018"S.rstrip() -> string\n\
1019\n\
1020Return a copy of the string S with trailing whitespace removed.";
1021
1022static PyObject *
1023string_rstrip(self, args)
1024 PyStringObject *self;
1025 PyObject *args;
1026{
1027 return do_strip(self, args, RIGHTSTRIP);
1028}
1029
1030
1031static char lower__doc__[] =
1032"S.lower() -> string\n\
1033\n\
1034Return a copy of the string S converted to lowercase.";
1035
1036static PyObject *
1037string_lower(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, ":lower"))
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 (isupper(c)) {
1054 *s_new = tolower(c);
1055 } else
1056 *s_new = c;
1057 s_new++;
1058 }
1059 return new;
1060}
1061
1062
1063static char upper__doc__[] =
1064"S.upper() -> string\n\
1065\n\
1066Return a copy of the string S converted to uppercase.";
1067
1068static PyObject *
1069string_upper(self, args)
1070 PyStringObject *self;
1071 PyObject *args;
1072{
1073 char *s = PyString_AS_STRING(self), *s_new;
1074 int i, n = PyString_GET_SIZE(self);
1075 PyObject *new;
1076
Guido van Rossum43713e52000-02-29 13:59:29 +00001077 if (!PyArg_ParseTuple(args, ":upper"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001078 return NULL;
1079 new = PyString_FromStringAndSize(NULL, n);
1080 if (new == NULL)
1081 return NULL;
1082 s_new = PyString_AsString(new);
1083 for (i = 0; i < n; i++) {
1084 int c = Py_CHARMASK(*s++);
1085 if (islower(c)) {
1086 *s_new = toupper(c);
1087 } else
1088 *s_new = c;
1089 s_new++;
1090 }
1091 return new;
1092}
1093
1094
1095static char capitalize__doc__[] =
1096"S.capitalize() -> string\n\
1097\n\
1098Return a copy of the string S with only its first character\n\
1099capitalized.";
1100
1101static PyObject *
1102string_capitalize(self, args)
1103 PyStringObject *self;
1104 PyObject *args;
1105{
1106 char *s = PyString_AS_STRING(self), *s_new;
1107 int i, n = PyString_GET_SIZE(self);
1108 PyObject *new;
1109
Guido van Rossum43713e52000-02-29 13:59:29 +00001110 if (!PyArg_ParseTuple(args, ":capitalize"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001111 return NULL;
1112 new = PyString_FromStringAndSize(NULL, n);
1113 if (new == NULL)
1114 return NULL;
1115 s_new = PyString_AsString(new);
1116 if (0 < n) {
1117 int c = Py_CHARMASK(*s++);
1118 if (islower(c))
1119 *s_new = toupper(c);
1120 else
1121 *s_new = c;
1122 s_new++;
1123 }
1124 for (i = 1; i < n; i++) {
1125 int c = Py_CHARMASK(*s++);
1126 if (isupper(c))
1127 *s_new = tolower(c);
1128 else
1129 *s_new = c;
1130 s_new++;
1131 }
1132 return new;
1133}
1134
1135
1136static char count__doc__[] =
1137"S.count(sub[, start[, end]]) -> int\n\
1138\n\
1139Return the number of occurrences of substring sub in string\n\
1140S[start:end]. Optional arguments start and end are\n\
1141interpreted as in slice notation.";
1142
1143static PyObject *
1144string_count(self, args)
1145 PyStringObject *self;
1146 PyObject *args;
1147{
1148 char *s = PyString_AS_STRING(self), *sub;
1149 int len = PyString_GET_SIZE(self), n;
1150 int i = 0, last = INT_MAX;
1151 int m, r;
1152
Guido van Rossum43713e52000-02-29 13:59:29 +00001153 if (!PyArg_ParseTuple(args, "t#|ii:count", &sub, &n, &i, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001154 return NULL;
1155 if (last > len)
1156 last = len;
1157 if (last < 0)
1158 last += len;
1159 if (last < 0)
1160 last = 0;
1161 if (i < 0)
1162 i += len;
1163 if (i < 0)
1164 i = 0;
1165 m = last + 1 - n;
1166 if (n == 0)
1167 return PyInt_FromLong((long) (m-i));
1168
1169 r = 0;
1170 while (i < m) {
1171 if (!memcmp(s+i, sub, n)) {
1172 r++;
1173 i += n;
1174 } else {
1175 i++;
1176 }
1177 }
1178 return PyInt_FromLong((long) r);
1179}
1180
1181
1182static char swapcase__doc__[] =
1183"S.swapcase() -> string\n\
1184\n\
1185Return a copy of the string S with upper case characters\n\
1186converted to lowercase and vice versa.";
1187
1188static PyObject *
1189string_swapcase(self, args)
1190 PyStringObject *self;
1191 PyObject *args;
1192{
1193 char *s = PyString_AS_STRING(self), *s_new;
1194 int i, n = PyString_GET_SIZE(self);
1195 PyObject *new;
1196
Guido van Rossum43713e52000-02-29 13:59:29 +00001197 if (!PyArg_ParseTuple(args, ":swapcase"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001198 return NULL;
1199 new = PyString_FromStringAndSize(NULL, n);
1200 if (new == NULL)
1201 return NULL;
1202 s_new = PyString_AsString(new);
1203 for (i = 0; i < n; i++) {
1204 int c = Py_CHARMASK(*s++);
1205 if (islower(c)) {
1206 *s_new = toupper(c);
1207 }
1208 else if (isupper(c)) {
1209 *s_new = tolower(c);
1210 }
1211 else
1212 *s_new = c;
1213 s_new++;
1214 }
1215 return new;
1216}
1217
1218
1219static char translate__doc__[] =
1220"S.translate(table [,deletechars]) -> string\n\
1221\n\
1222Return a copy of the string S, where all characters occurring\n\
1223in the optional argument deletechars are removed, and the\n\
1224remaining characters have been mapped through the given\n\
1225translation table, which must be a string of length 256.";
1226
1227static PyObject *
1228string_translate(self, args)
1229 PyStringObject *self;
1230 PyObject *args;
1231{
1232 register char *input, *table, *output;
1233 register int i, c, changed = 0;
1234 PyObject *input_obj = (PyObject*)self;
1235 char *table1, *output_start, *del_table=NULL;
1236 int inlen, tablen, dellen = 0;
1237 PyObject *result;
1238 int trans_table[256];
1239
Guido van Rossum43713e52000-02-29 13:59:29 +00001240 if (!PyArg_ParseTuple(args, "t#|t#:translate",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001241 &table1, &tablen, &del_table, &dellen))
1242 return NULL;
1243 if (tablen != 256) {
1244 PyErr_SetString(PyExc_ValueError,
1245 "translation table must be 256 characters long");
1246 return NULL;
1247 }
1248
1249 table = table1;
1250 inlen = PyString_Size(input_obj);
1251 result = PyString_FromStringAndSize((char *)NULL, inlen);
1252 if (result == NULL)
1253 return NULL;
1254 output_start = output = PyString_AsString(result);
1255 input = PyString_AsString(input_obj);
1256
1257 if (dellen == 0) {
1258 /* If no deletions are required, use faster code */
1259 for (i = inlen; --i >= 0; ) {
1260 c = Py_CHARMASK(*input++);
1261 if (Py_CHARMASK((*output++ = table[c])) != c)
1262 changed = 1;
1263 }
1264 if (changed)
1265 return result;
1266 Py_DECREF(result);
1267 Py_INCREF(input_obj);
1268 return input_obj;
1269 }
1270
1271 for (i = 0; i < 256; i++)
1272 trans_table[i] = Py_CHARMASK(table[i]);
1273
1274 for (i = 0; i < dellen; i++)
1275 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
1276
1277 for (i = inlen; --i >= 0; ) {
1278 c = Py_CHARMASK(*input++);
1279 if (trans_table[c] != -1)
1280 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1281 continue;
1282 changed = 1;
1283 }
1284 if (!changed) {
1285 Py_DECREF(result);
1286 Py_INCREF(input_obj);
1287 return input_obj;
1288 }
1289 /* Fix the size of the resulting string */
1290 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
1291 return NULL;
1292 return result;
1293}
1294
1295
1296/* What follows is used for implementing replace(). Perry Stoll. */
1297
1298/*
1299 mymemfind
1300
1301 strstr replacement for arbitrary blocks of memory.
1302
1303 Locates the first occurance in the memory pointed to by MEM of the
1304 contents of memory pointed to by PAT. Returns the index into MEM if
1305 found, or -1 if not found. If len of PAT is greater than length of
1306 MEM, the function returns -1.
1307*/
1308static int
1309mymemfind(mem, len, pat, pat_len)
1310 char *mem;
1311 int len;
1312 char *pat;
1313 int pat_len;
1314{
1315 register int ii;
1316
1317 /* pattern can not occur in the last pat_len-1 chars */
1318 len -= pat_len;
1319
1320 for (ii = 0; ii <= len; ii++) {
1321 if (mem[ii] == pat[0] &&
1322 (pat_len == 1 ||
1323 memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1324 return ii;
1325 }
1326 }
1327 return -1;
1328}
1329
1330/*
1331 mymemcnt
1332
1333 Return the number of distinct times PAT is found in MEM.
1334 meaning mem=1111 and pat==11 returns 2.
1335 mem=11111 and pat==11 also return 2.
1336 */
1337static int
1338mymemcnt(mem, len, pat, pat_len)
1339 char *mem;
1340 int len;
1341 char *pat;
1342 int pat_len;
1343{
1344 register int offset = 0;
1345 int nfound = 0;
1346
1347 while (len >= 0) {
1348 offset = mymemfind(mem, len, pat, pat_len);
1349 if (offset == -1)
1350 break;
1351 mem += offset + pat_len;
1352 len -= offset + pat_len;
1353 nfound++;
1354 }
1355 return nfound;
1356}
1357
1358/*
1359 mymemreplace
1360
1361 Return a string in which all occurences of PAT in memory STR are
1362 replaced with SUB.
1363
1364 If length of PAT is less than length of STR or there are no occurences
1365 of PAT in STR, then the original string is returned. Otherwise, a new
1366 string is allocated here and returned.
1367
1368 on return, out_len is:
1369 the length of output string, or
1370 -1 if the input string is returned, or
1371 unchanged if an error occurs (no memory).
1372
1373 return value is:
1374 the new string allocated locally, or
1375 NULL if an error occurred.
1376*/
1377static char *
1378mymemreplace(str, len, pat, pat_len, sub, sub_len, count, out_len)
1379 char *str;
1380 int len; /* input string */
1381 char *pat;
1382 int pat_len; /* pattern string to find */
1383 char *sub;
1384 int sub_len; /* substitution string */
1385 int count; /* number of replacements, 0 == all */
1386 int *out_len;
1387
1388{
1389 char *out_s;
1390 char *new_s;
1391 int nfound, offset, new_len;
1392
1393 if (len == 0 || pat_len > len)
1394 goto return_same;
1395
1396 /* find length of output string */
1397 nfound = mymemcnt(str, len, pat, pat_len);
1398 if (count > 0)
1399 nfound = nfound > count ? count : nfound;
1400 if (nfound == 0)
1401 goto return_same;
1402 new_len = len + nfound*(sub_len - pat_len);
1403
1404 new_s = (char *)malloc(new_len);
1405 if (new_s == NULL) return NULL;
1406
1407 *out_len = new_len;
1408 out_s = new_s;
1409
1410 while (len > 0) {
1411 /* find index of next instance of pattern */
1412 offset = mymemfind(str, len, pat, pat_len);
1413 /* if not found, break out of loop */
1414 if (offset == -1) break;
1415
1416 /* copy non matching part of input string */
1417 memcpy(new_s, str, offset); /* copy part of str before pat */
1418 str += offset + pat_len; /* move str past pattern */
1419 len -= offset + pat_len; /* reduce length of str remaining */
1420
1421 /* copy substitute into the output string */
1422 new_s += offset; /* move new_s to dest for sub string */
1423 memcpy(new_s, sub, sub_len); /* copy substring into new_s */
1424 new_s += sub_len; /* offset new_s past sub string */
1425
1426 /* break when we've done count replacements */
1427 if (--count == 0) break;
1428 }
1429 /* copy any remaining values into output string */
1430 if (len > 0)
1431 memcpy(new_s, str, len);
1432 return out_s;
1433
1434 return_same:
1435 *out_len = -1;
1436 return str;
1437}
1438
1439
1440static char replace__doc__[] =
1441"S.replace (old, new[, maxsplit]) -> string\n\
1442\n\
1443Return a copy of string S with all occurrences of substring\n\
1444old replaced by new. If the optional argument maxsplit is\n\
1445given, only the first maxsplit occurrences are replaced.";
1446
1447static PyObject *
1448string_replace(self, args)
1449 PyStringObject *self;
1450 PyObject *args;
1451{
1452 char *str = PyString_AS_STRING(self), *pat,*sub,*new_s;
1453 int len = PyString_GET_SIZE(self), pat_len,sub_len,out_len;
1454 int count = 0;
1455 PyObject *new;
1456
Guido van Rossum43713e52000-02-29 13:59:29 +00001457 if (!PyArg_ParseTuple(args, "t#t#|i:replace",
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001458 &pat, &pat_len, &sub, &sub_len, &count))
1459 return NULL;
1460 if (pat_len <= 0) {
1461 PyErr_SetString(PyExc_ValueError, "empty pattern string");
1462 return NULL;
1463 }
1464 new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
1465 if (new_s == NULL) {
1466 PyErr_NoMemory();
1467 return NULL;
1468 }
1469 if (out_len == -1) {
1470 /* we're returning another reference to self */
1471 new = (PyObject*)self;
1472 Py_INCREF(new);
1473 }
1474 else {
1475 new = PyString_FromStringAndSize(new_s, out_len);
1476 free(new_s);
1477 }
1478 return new;
1479}
1480
1481
1482static char startswith__doc__[] =
1483"S.startswith(prefix[, start[, end]]) -> int\n\
1484\n\
1485Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
1486optional start, test S beginning at that position. With optional end, stop\n\
1487comparing S at that position.";
1488
1489static PyObject *
1490string_startswith(self, args)
1491 PyStringObject *self;
1492 PyObject *args;
1493{
1494 char* str = PyString_AS_STRING(self);
1495 int len = PyString_GET_SIZE(self);
1496 char* prefix;
1497 int plen;
1498 int start = 0;
1499 int end = -1;
1500
Guido van Rossum43713e52000-02-29 13:59:29 +00001501 if (!PyArg_ParseTuple(args, "t#|ii:startswith", &prefix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001502 return NULL;
1503
1504 /* adopt Java semantics for index out of range. it is legal for
1505 * offset to be == plen, but this only returns true if prefix is
1506 * the empty string.
1507 */
1508 if (start < 0 || start+plen > len)
1509 return PyInt_FromLong(0);
1510
1511 if (!memcmp(str+start, prefix, plen)) {
1512 /* did the match end after the specified end? */
1513 if (end < 0)
1514 return PyInt_FromLong(1);
1515 else if (end - start < plen)
1516 return PyInt_FromLong(0);
1517 else
1518 return PyInt_FromLong(1);
1519 }
1520 else return PyInt_FromLong(0);
1521}
1522
1523
1524static char endswith__doc__[] =
1525"S.endswith(suffix[, start[, end]]) -> int\n\
1526\n\
1527Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
1528optional start, test S beginning at that position. With optional end, stop\n\
1529comparing S at that position.";
1530
1531static PyObject *
1532string_endswith(self, args)
1533 PyStringObject *self;
1534 PyObject *args;
1535{
1536 char* str = PyString_AS_STRING(self);
1537 int len = PyString_GET_SIZE(self);
1538 char* suffix;
1539 int plen;
1540 int start = 0;
1541 int end = -1;
1542 int lower, upper;
1543
Guido van Rossum43713e52000-02-29 13:59:29 +00001544 if (!PyArg_ParseTuple(args, "t#|ii:endswith", &suffix, &plen, &start, &end))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001545 return NULL;
1546
1547 if (start < 0 || start > len || plen > len)
1548 return PyInt_FromLong(0);
1549
1550 upper = (end >= 0 && end <= len) ? end : len;
1551 lower = (upper - plen) > start ? (upper - plen) : start;
1552
1553 if (upper-lower >= plen && !memcmp(str+lower, suffix, plen))
1554 return PyInt_FromLong(1);
1555 else return PyInt_FromLong(0);
1556}
1557
1558
1559
1560static PyMethodDef
1561string_methods[] = {
1562 /* counterparts of the obsolete stropmodule functions */
1563 {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__},
1564 {"count", (PyCFunction)string_count, 1, count__doc__},
1565 {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__},
1566 {"find", (PyCFunction)string_find, 1, find__doc__},
1567 {"index", (PyCFunction)string_index, 1, index__doc__},
1568 {"join", (PyCFunction)string_join, 1, join__doc__},
1569 {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__},
1570 {"lower", (PyCFunction)string_lower, 1, lower__doc__},
1571 /* maketrans */
1572 {"replace", (PyCFunction)string_replace, 1, replace__doc__},
1573 {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__},
1574 {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__},
1575 {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__},
1576 {"split", (PyCFunction)string_split, 1, split__doc__},
1577 {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__},
1578 {"strip", (PyCFunction)string_strip, 1, strip__doc__},
1579 {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__},
1580 {"translate", (PyCFunction)string_translate, 1, strip__doc__},
1581 {"upper", (PyCFunction)string_upper, 1, upper__doc__},
1582 /* TBD */
1583/* {"ljust" (PyCFunction)string_ljust, 1, ljust__doc__}, */
1584/* {"rjust" (PyCFunction)string_rjust, 1, rjust__doc__}, */
1585/* {"center" (PyCFunction)string_center, 1, center__doc__}, */
1586/* {"zfill" (PyCFunction)string_zfill, 1, zfill__doc__}, */
1587/* {"expandtabs" (PyCFunction)string_expandtabs, 1, ljust__doc__}, */
1588/* {"capwords" (PyCFunction)string_capwords, 1, capwords__doc__}, */
1589 {NULL, NULL} /* sentinel */
1590};
1591
1592static PyObject *
1593string_getattr(s, name)
1594 PyStringObject *s;
1595 char *name;
1596{
1597 return Py_FindMethod(string_methods, (PyObject*)s, name);
1598}
1599
1600
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001601PyTypeObject PyString_Type = {
1602 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603 0,
1604 "string",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001605 sizeof(PyStringObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001606 sizeof(char),
Guido van Rossum013142a1994-08-30 08:19:36 +00001607 (destructor)string_dealloc, /*tp_dealloc*/
1608 (printfunc)string_print, /*tp_print*/
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001609 (getattrfunc)string_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610 0, /*tp_setattr*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001611 (cmpfunc)string_compare, /*tp_compare*/
1612 (reprfunc)string_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001613 0, /*tp_as_number*/
1614 &string_as_sequence, /*tp_as_sequence*/
1615 0, /*tp_as_mapping*/
Guido van Rossum013142a1994-08-30 08:19:36 +00001616 (hashfunc)string_hash, /*tp_hash*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001617 0, /*tp_call*/
1618 0, /*tp_str*/
1619 0, /*tp_getattro*/
1620 0, /*tp_setattro*/
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001621 &string_as_buffer, /*tp_as_buffer*/
Guido van Rossum1db70701998-10-08 02:18:52 +00001622 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00001623 0, /*tp_doc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001624};
1625
1626void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627PyString_Concat(pv, w)
1628 register PyObject **pv;
1629 register PyObject *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001630{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001631 register PyObject *v;
Guido van Rossum013142a1994-08-30 08:19:36 +00001632 if (*pv == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001633 return;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001634 if (w == NULL || !PyString_Check(*pv)) {
1635 Py_DECREF(*pv);
Guido van Rossum013142a1994-08-30 08:19:36 +00001636 *pv = NULL;
1637 return;
1638 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001639 v = string_concat((PyStringObject *) *pv, w);
1640 Py_DECREF(*pv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001641 *pv = v;
1642}
1643
Guido van Rossum013142a1994-08-30 08:19:36 +00001644void
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001645PyString_ConcatAndDel(pv, w)
1646 register PyObject **pv;
1647 register PyObject *w;
Guido van Rossum013142a1994-08-30 08:19:36 +00001648{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001649 PyString_Concat(pv, w);
1650 Py_XDECREF(w);
Guido van Rossum013142a1994-08-30 08:19:36 +00001651}
1652
1653
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001654/* The following function breaks the notion that strings are immutable:
1655 it changes the size of a string. We get away with this only if there
1656 is only one module referencing the object. You can also think of it
1657 as creating a new string object and destroying the old one, only
1658 more efficiently. In any case, don't use this if the string may
1659 already be known to some other part of the code... */
1660
1661int
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001662_PyString_Resize(pv, newsize)
1663 PyObject **pv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664 int newsize;
1665{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001666 register PyObject *v;
1667 register PyStringObject *sv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001668 v = *pv;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001669 if (!PyString_Check(v) || v->ob_refcnt != 1) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670 *pv = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001671 Py_DECREF(v);
1672 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001673 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001674 }
Guido van Rossum921842f1990-11-18 17:30:23 +00001675 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +00001676#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +00001677 --_Py_RefTotal;
Guido van Rossum921842f1990-11-18 17:30:23 +00001678#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679 _Py_ForgetReference(v);
1680 *pv = (PyObject *)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001681 realloc((char *)v,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001682 sizeof(PyStringObject) + newsize * sizeof(char));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001683 if (*pv == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001684 PyMem_DEL(v);
1685 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00001686 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001688 _Py_NewReference(*pv);
1689 sv = (PyStringObject *) *pv;
Guido van Rossum921842f1990-11-18 17:30:23 +00001690 sv->ob_size = newsize;
1691 sv->ob_sval[newsize] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001692 return 0;
1693}
Guido van Rossume5372401993-03-16 12:15:04 +00001694
1695/* Helpers for formatstring */
1696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001697static PyObject *
Guido van Rossume5372401993-03-16 12:15:04 +00001698getnextarg(args, arglen, p_argidx)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001699 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001700 int arglen;
1701 int *p_argidx;
1702{
1703 int argidx = *p_argidx;
1704 if (argidx < arglen) {
1705 (*p_argidx)++;
1706 if (arglen < 0)
1707 return args;
1708 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001709 return PyTuple_GetItem(args, argidx);
Guido van Rossume5372401993-03-16 12:15:04 +00001710 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001711 PyErr_SetString(PyExc_TypeError,
1712 "not enough arguments for format string");
Guido van Rossume5372401993-03-16 12:15:04 +00001713 return NULL;
1714}
1715
1716#define F_LJUST (1<<0)
1717#define F_SIGN (1<<1)
1718#define F_BLANK (1<<2)
1719#define F_ALT (1<<3)
1720#define F_ZERO (1<<4)
1721
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001722static int
1723formatfloat(buf, flags, prec, type, v)
1724 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001725 int flags;
1726 int prec;
1727 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001728 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001729{
1730 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001731 double x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001732 if (!PyArg_Parse(v, "d;float argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001733 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001734 if (prec < 0)
1735 prec = 6;
1736 if (prec > 50)
1737 prec = 50; /* Arbitrary limitation */
1738 if (type == 'f' && fabs(x)/1e25 >= 1e25)
1739 type = 'g';
1740 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
1741 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001742 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001743}
1744
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001745static int
1746formatint(buf, flags, prec, type, v)
1747 char *buf;
Guido van Rossume5372401993-03-16 12:15:04 +00001748 int flags;
1749 int prec;
1750 int type;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001751 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001752{
1753 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00001754 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755 if (!PyArg_Parse(v, "l;int argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001756 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001757 if (prec < 0)
1758 prec = 1;
1759 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
1760 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001761 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00001762}
1763
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001764static int
1765formatchar(buf, v)
1766 char *buf;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001767 PyObject *v;
Guido van Rossume5372401993-03-16 12:15:04 +00001768{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001769 if (PyString_Check(v)) {
1770 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001771 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001772 }
1773 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001774 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001775 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00001776 }
1777 buf[1] = '\0';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001778 return 1;
Guido van Rossume5372401993-03-16 12:15:04 +00001779}
1780
Guido van Rossum013142a1994-08-30 08:19:36 +00001781
Guido van Rossume5372401993-03-16 12:15:04 +00001782/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
1783
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001784PyObject *
1785PyString_Format(format, args)
1786 PyObject *format;
1787 PyObject *args;
Guido van Rossume5372401993-03-16 12:15:04 +00001788{
1789 char *fmt, *res;
1790 int fmtcnt, rescnt, reslen, arglen, argidx;
Guido van Rossum993952b1996-05-21 22:44:20 +00001791 int args_owned = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001792 PyObject *result;
1793 PyObject *dict = NULL;
1794 if (format == NULL || !PyString_Check(format) || args == NULL) {
1795 PyErr_BadInternalCall();
Guido van Rossume5372401993-03-16 12:15:04 +00001796 return NULL;
1797 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001798 fmt = PyString_AsString(format);
1799 fmtcnt = PyString_Size(format);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001800 reslen = rescnt = fmtcnt + 100;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801 result = PyString_FromStringAndSize((char *)NULL, reslen);
Guido van Rossume5372401993-03-16 12:15:04 +00001802 if (result == NULL)
1803 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001804 res = PyString_AsString(result);
1805 if (PyTuple_Check(args)) {
1806 arglen = PyTuple_Size(args);
Guido van Rossume5372401993-03-16 12:15:04 +00001807 argidx = 0;
1808 }
1809 else {
1810 arglen = -1;
1811 argidx = -2;
1812 }
Guido van Rossum013142a1994-08-30 08:19:36 +00001813 if (args->ob_type->tp_as_mapping)
1814 dict = args;
Guido van Rossume5372401993-03-16 12:15:04 +00001815 while (--fmtcnt >= 0) {
1816 if (*fmt != '%') {
1817 if (--rescnt < 0) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001818 rescnt = fmtcnt + 100;
1819 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001820 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00001821 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001822 res = PyString_AsString(result)
1823 + reslen - rescnt;
Guido van Rossum013142a1994-08-30 08:19:36 +00001824 --rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00001825 }
1826 *res++ = *fmt++;
1827 }
1828 else {
1829 /* Got a format specifier */
1830 int flags = 0;
Guido van Rossume5372401993-03-16 12:15:04 +00001831 int width = -1;
1832 int prec = -1;
1833 int size = 0;
Guido van Rossum6938a291993-11-11 14:51:57 +00001834 int c = '\0';
Guido van Rossume5372401993-03-16 12:15:04 +00001835 int fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001836 PyObject *v = NULL;
1837 PyObject *temp = NULL;
Guido van Rossume5372401993-03-16 12:15:04 +00001838 char *buf;
1839 int sign;
1840 int len;
Guido van Rossuma04d47b1997-01-21 16:12:09 +00001841 char tmpbuf[120]; /* For format{float,int,char}() */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001842 fmt++;
Guido van Rossum013142a1994-08-30 08:19:36 +00001843 if (*fmt == '(') {
1844 char *keystart;
1845 int keylen;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001846 PyObject *key;
Guido van Rossum045e6881997-09-08 18:30:11 +00001847 int pcount = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001848
1849 if (dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001850 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001851 "format requires a mapping");
1852 goto error;
1853 }
1854 ++fmt;
1855 --fmtcnt;
1856 keystart = fmt;
Guido van Rossum045e6881997-09-08 18:30:11 +00001857 /* Skip over balanced parentheses */
1858 while (pcount > 0 && --fmtcnt >= 0) {
1859 if (*fmt == ')')
1860 --pcount;
1861 else if (*fmt == '(')
1862 ++pcount;
Guido van Rossum013142a1994-08-30 08:19:36 +00001863 fmt++;
Guido van Rossum045e6881997-09-08 18:30:11 +00001864 }
1865 keylen = fmt - keystart - 1;
1866 if (fmtcnt < 0 || pcount > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001867 PyErr_SetString(PyExc_ValueError,
Guido van Rossum013142a1994-08-30 08:19:36 +00001868 "incomplete format key");
1869 goto error;
1870 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001871 key = PyString_FromStringAndSize(keystart,
1872 keylen);
Guido van Rossum013142a1994-08-30 08:19:36 +00001873 if (key == NULL)
1874 goto error;
Guido van Rossum993952b1996-05-21 22:44:20 +00001875 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001876 Py_DECREF(args);
Guido van Rossum993952b1996-05-21 22:44:20 +00001877 args_owned = 0;
1878 }
1879 args = PyObject_GetItem(dict, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001880 Py_DECREF(key);
Guido van Rossum013142a1994-08-30 08:19:36 +00001881 if (args == NULL) {
1882 goto error;
1883 }
Guido van Rossum993952b1996-05-21 22:44:20 +00001884 args_owned = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00001885 arglen = -1;
1886 argidx = -2;
1887 }
Guido van Rossume5372401993-03-16 12:15:04 +00001888 while (--fmtcnt >= 0) {
1889 switch (c = *fmt++) {
1890 case '-': flags |= F_LJUST; continue;
1891 case '+': flags |= F_SIGN; continue;
1892 case ' ': flags |= F_BLANK; continue;
1893 case '#': flags |= F_ALT; continue;
1894 case '0': flags |= F_ZERO; continue;
1895 }
1896 break;
1897 }
1898 if (c == '*') {
1899 v = getnextarg(args, arglen, &argidx);
1900 if (v == NULL)
1901 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001902 if (!PyInt_Check(v)) {
1903 PyErr_SetString(PyExc_TypeError,
1904 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001905 goto error;
1906 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001907 width = PyInt_AsLong(v);
Guido van Rossum98c9eba1999-06-07 15:12:32 +00001908 if (width < 0) {
1909 flags |= F_LJUST;
1910 width = -width;
1911 }
Guido van Rossume5372401993-03-16 12:15:04 +00001912 if (--fmtcnt >= 0)
1913 c = *fmt++;
1914 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001915 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001916 width = c - '0';
1917 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001918 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001919 if (!isdigit(c))
1920 break;
1921 if ((width*10) / 10 != width) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001922 PyErr_SetString(
1923 PyExc_ValueError,
1924 "width too big");
Guido van Rossume5372401993-03-16 12:15:04 +00001925 goto error;
1926 }
1927 width = width*10 + (c - '0');
1928 }
1929 }
1930 if (c == '.') {
1931 prec = 0;
1932 if (--fmtcnt >= 0)
1933 c = *fmt++;
1934 if (c == '*') {
1935 v = getnextarg(args, arglen, &argidx);
1936 if (v == NULL)
1937 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001938 if (!PyInt_Check(v)) {
1939 PyErr_SetString(
1940 PyExc_TypeError,
1941 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00001942 goto error;
1943 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001944 prec = PyInt_AsLong(v);
Guido van Rossume5372401993-03-16 12:15:04 +00001945 if (prec < 0)
1946 prec = 0;
1947 if (--fmtcnt >= 0)
1948 c = *fmt++;
1949 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001950 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00001951 prec = c - '0';
1952 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00001953 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00001954 if (!isdigit(c))
1955 break;
1956 if ((prec*10) / 10 != prec) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001957 PyErr_SetString(
1958 PyExc_ValueError,
Guido van Rossume5372401993-03-16 12:15:04 +00001959 "prec too big");
1960 goto error;
1961 }
1962 prec = prec*10 + (c - '0');
1963 }
1964 }
1965 } /* prec */
1966 if (fmtcnt >= 0) {
1967 if (c == 'h' || c == 'l' || c == 'L') {
1968 size = c;
1969 if (--fmtcnt >= 0)
1970 c = *fmt++;
1971 }
1972 }
1973 if (fmtcnt < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001974 PyErr_SetString(PyExc_ValueError,
1975 "incomplete format");
Guido van Rossume5372401993-03-16 12:15:04 +00001976 goto error;
1977 }
1978 if (c != '%') {
1979 v = getnextarg(args, arglen, &argidx);
1980 if (v == NULL)
1981 goto error;
1982 }
1983 sign = 0;
1984 fill = ' ';
1985 switch (c) {
1986 case '%':
1987 buf = "%";
1988 len = 1;
1989 break;
1990 case 's':
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991 temp = PyObject_Str(v);
Guido van Rossum013142a1994-08-30 08:19:36 +00001992 if (temp == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +00001993 goto error;
Guido van Rossum4a0144c1998-06-09 15:08:41 +00001994 if (!PyString_Check(temp)) {
1995 PyErr_SetString(PyExc_TypeError,
1996 "%s argument has non-string str()");
1997 goto error;
1998 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001999 buf = PyString_AsString(temp);
2000 len = PyString_Size(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00002001 if (prec >= 0 && len > prec)
2002 len = prec;
2003 break;
2004 case 'i':
2005 case 'd':
2006 case 'u':
2007 case 'o':
2008 case 'x':
2009 case 'X':
2010 if (c == 'i')
2011 c = 'd';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002012 buf = tmpbuf;
2013 len = formatint(buf, flags, prec, c, v);
2014 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002015 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002016 sign = (c == 'd');
Guido van Rossum4acdc231997-01-29 06:00:24 +00002017 if (flags&F_ZERO) {
Guido van Rossume5372401993-03-16 12:15:04 +00002018 fill = '0';
Guido van Rossum4acdc231997-01-29 06:00:24 +00002019 if ((flags&F_ALT) &&
2020 (c == 'x' || c == 'X') &&
2021 buf[0] == '0' && buf[1] == c) {
2022 *res++ = *buf++;
2023 *res++ = *buf++;
2024 rescnt -= 2;
2025 len -= 2;
2026 width -= 2;
2027 if (width < 0)
2028 width = 0;
2029 }
2030 }
Guido van Rossume5372401993-03-16 12:15:04 +00002031 break;
2032 case 'e':
2033 case 'E':
2034 case 'f':
2035 case 'g':
2036 case 'G':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002037 buf = tmpbuf;
2038 len = formatfloat(buf, flags, prec, c, v);
2039 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002040 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002041 sign = 1;
2042 if (flags&F_ZERO)
2043 fill = '0';
2044 break;
2045 case 'c':
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002046 buf = tmpbuf;
2047 len = formatchar(buf, v);
2048 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002049 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00002050 break;
2051 default:
Guido van Rossum045e6881997-09-08 18:30:11 +00002052 PyErr_Format(PyExc_ValueError,
2053 "unsupported format character '%c' (0x%x)",
2054 c, c);
Guido van Rossume5372401993-03-16 12:15:04 +00002055 goto error;
2056 }
2057 if (sign) {
2058 if (*buf == '-' || *buf == '+') {
2059 sign = *buf++;
2060 len--;
2061 }
2062 else if (flags & F_SIGN)
2063 sign = '+';
2064 else if (flags & F_BLANK)
2065 sign = ' ';
2066 else
2067 sign = '\0';
2068 }
2069 if (width < len)
2070 width = len;
2071 if (rescnt < width + (sign != '\0')) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00002072 reslen -= rescnt;
2073 rescnt = width + fmtcnt + 100;
2074 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002075 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002076 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002077 res = PyString_AsString(result)
2078 + reslen - rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00002079 }
2080 if (sign) {
Guido van Rossum71e57d01993-11-11 15:03:51 +00002081 if (fill != ' ')
2082 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002083 rescnt--;
2084 if (width > len)
2085 width--;
2086 }
2087 if (width > len && !(flags&F_LJUST)) {
2088 do {
2089 --rescnt;
2090 *res++ = fill;
2091 } while (--width > len);
2092 }
Guido van Rossum71e57d01993-11-11 15:03:51 +00002093 if (sign && fill == ' ')
Guido van Rossum6938a291993-11-11 14:51:57 +00002094 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00002095 memcpy(res, buf, len);
2096 res += len;
2097 rescnt -= len;
2098 while (--width >= len) {
2099 --rescnt;
2100 *res++ = ' ';
2101 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002102 if (dict && (argidx < arglen) && c != '%') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002103 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00002104 "not all arguments converted");
2105 goto error;
2106 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002107 Py_XDECREF(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00002108 } /* '%' */
2109 } /* until end */
Guido van Rossumcaeaafc1995-02-27 10:13:23 +00002110 if (argidx < arglen && !dict) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002111 PyErr_SetString(PyExc_TypeError,
2112 "not all arguments converted");
Guido van Rossume5372401993-03-16 12:15:04 +00002113 goto error;
2114 }
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002115 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002116 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002117 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002118 _PyString_Resize(&result, reslen - rescnt);
Guido van Rossume5372401993-03-16 12:15:04 +00002119 return result;
2120 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002121 Py_DECREF(result);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002122 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002123 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00002124 }
Guido van Rossume5372401993-03-16 12:15:04 +00002125 return NULL;
2126}
Guido van Rossum2a61e741997-01-18 07:55:05 +00002127
2128
2129#ifdef INTERN_STRINGS
2130
2131static PyObject *interned;
2132
2133void
2134PyString_InternInPlace(p)
2135 PyObject **p;
2136{
2137 register PyStringObject *s = (PyStringObject *)(*p);
2138 PyObject *t;
2139 if (s == NULL || !PyString_Check(s))
2140 Py_FatalError("PyString_InternInPlace: strings only please!");
2141 if ((t = s->ob_sinterned) != NULL) {
2142 if (t == (PyObject *)s)
2143 return;
2144 Py_INCREF(t);
2145 *p = t;
2146 Py_DECREF(s);
2147 return;
2148 }
2149 if (interned == NULL) {
2150 interned = PyDict_New();
2151 if (interned == NULL)
2152 return;
Guido van Rossum2a61e741997-01-18 07:55:05 +00002153 }
2154 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
2155 Py_INCREF(t);
2156 *p = s->ob_sinterned = t;
2157 Py_DECREF(s);
2158 return;
2159 }
2160 t = (PyObject *)s;
2161 if (PyDict_SetItem(interned, t, t) == 0) {
2162 s->ob_sinterned = t;
2163 return;
2164 }
2165 PyErr_Clear();
2166}
2167
2168
2169PyObject *
2170PyString_InternFromString(cp)
2171 const char *cp;
2172{
2173 PyObject *s = PyString_FromString(cp);
2174 if (s == NULL)
2175 return NULL;
2176 PyString_InternInPlace(&s);
2177 return s;
2178}
2179
2180#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002181
2182void
2183PyString_Fini()
2184{
2185 int i;
Guido van Rossum8cf04761997-08-02 02:57:45 +00002186 for (i = 0; i < UCHAR_MAX + 1; i++) {
2187 Py_XDECREF(characters[i]);
2188 characters[i] = NULL;
2189 }
2190#ifndef DONT_SHARE_SHORT_STRINGS
2191 Py_XDECREF(nullstring);
2192 nullstring = NULL;
2193#endif
Guido van Rossum971a7aa1997-08-05 02:15:12 +00002194#ifdef INTERN_STRINGS
2195 if (interned) {
2196 int pos, changed;
2197 PyObject *key, *value;
2198 do {
2199 changed = 0;
2200 pos = 0;
2201 while (PyDict_Next(interned, &pos, &key, &value)) {
2202 if (key->ob_refcnt == 2 && key == value) {
2203 PyDict_DelItem(interned, key);
2204 changed = 1;
2205 }
2206 }
2207 } while (changed);
2208 }
2209#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00002210}