blob: ec909db49b292d9d0dce164938bc34f233c0d738 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* String object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum013142a1994-08-30 08:19:36 +00006#include <ctype.h>
7
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00008#ifdef COUNT_ALLOCS
9int null_strings, one_strings;
10#endif
11
Fred Draked5fadf72000-09-26 05:46:01 +000012#if !defined(HAVE_LIMITS_H) && !defined(UCHAR_MAX)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000013#define UCHAR_MAX 255
14#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000015
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016static PyStringObject *characters[UCHAR_MAX + 1];
Sjoerd Mullender615194a1993-11-01 13:46:50 +000017#ifndef DONT_SHARE_SHORT_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000018static PyStringObject *nullstring;
Sjoerd Mullender615194a1993-11-01 13:46:50 +000019#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000020
21/*
22 Newsizedstringobject() and newstringobject() try in certain cases
23 to share string objects. When the size of the string is zero,
24 these routines always return a pointer to the same string object;
25 when the size is one, they return a pointer to an already existing
26 object if the contents of the string is known. For
27 newstringobject() this is always the case, for
28 newsizedstringobject() this is the case when the first argument in
29 not NULL.
30 A common practice to allocate a string and then fill it in or
31 change it must be done carefully. It is only allowed to change the
32 contents of the string if the obect was gotten from
33 newsizedstringobject() with a NULL first argument, because in the
34 future these routines may try to do even more sharing of objects.
35*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000037PyString_FromStringAndSize(const char *str, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038{
Tim Peters9e897f42001-05-09 07:37:07 +000039 register PyStringObject *op;
Sjoerd Mullender615194a1993-11-01 13:46:50 +000040#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000041 if (size == 0 && (op = nullstring) != NULL) {
42#ifdef COUNT_ALLOCS
43 null_strings++;
44#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 Py_INCREF(op);
46 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000047 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 if (size == 1 && str != NULL &&
49 (op = characters[*str & UCHAR_MAX]) != NULL)
50 {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000051#ifdef COUNT_ALLOCS
52 one_strings++;
53#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054 Py_INCREF(op);
55 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000056 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +000057#endif /* DONT_SHARE_SHORT_STRINGS */
Guido van Rossumb18618d2000-05-03 23:44:39 +000058
59 /* PyObject_NewVar is inlined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 op = (PyStringObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +000061 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +000062 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000063 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 PyObject_INIT_VAR(op, &PyString_Type, size);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000065#ifdef CACHE_HASH
66 op->ob_shash = -1;
67#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +000068#ifdef INTERN_STRINGS
69 op->ob_sinterned = NULL;
70#endif
Guido van Rossum2a9096b1990-10-21 22:15:08 +000071 if (str != NULL)
72 memcpy(op->ob_sval, str, size);
73 op->ob_sval[size] = '\0';
Sjoerd Mullender615194a1993-11-01 13:46:50 +000074#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000075 if (size == 0) {
Tim Peters9e897f42001-05-09 07:37:07 +000076 PyObject *t = (PyObject *)op;
77 PyString_InternInPlace(&t);
Tim Peters4862ab72001-05-09 08:43:21 +000078 op = (PyStringObject *)t;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000079 nullstring = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000081 } else if (size == 1 && str != NULL) {
Tim Peters9e897f42001-05-09 07:37:07 +000082 PyObject *t = (PyObject *)op;
83 PyString_InternInPlace(&t);
Tim Peters4862ab72001-05-09 08:43:21 +000084 op = (PyStringObject *)t;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000085 characters[*str & UCHAR_MAX] = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000087 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +000088#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090}
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000093PyString_FromString(const char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Marc-André Lemburgf28dd832000-06-30 10:29:57 +000095 register size_t size = strlen(str);
Tim Peters9e897f42001-05-09 07:37:07 +000096 register PyStringObject *op;
Marc-André Lemburgf28dd832000-06-30 10:29:57 +000097 if (size > INT_MAX) {
98 PyErr_SetString(PyExc_OverflowError,
99 "string is too long for a Python string");
100 return NULL;
101 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000102#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000103 if (size == 0 && (op = nullstring) != NULL) {
104#ifdef COUNT_ALLOCS
105 null_strings++;
106#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 Py_INCREF(op);
108 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000109 }
110 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
111#ifdef COUNT_ALLOCS
112 one_strings++;
113#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 Py_INCREF(op);
115 return (PyObject *)op;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000116 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000117#endif /* DONT_SHARE_SHORT_STRINGS */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118
119 /* PyObject_NewVar is inlined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 op = (PyStringObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000122 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000123 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000124 PyObject_INIT_VAR(op, &PyString_Type, size);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000125#ifdef CACHE_HASH
126 op->ob_shash = -1;
127#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000128#ifdef INTERN_STRINGS
129 op->ob_sinterned = NULL;
130#endif
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000131 strcpy(op->ob_sval, str);
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000132#ifndef DONT_SHARE_SHORT_STRINGS
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000133 if (size == 0) {
Tim Peters9e897f42001-05-09 07:37:07 +0000134 PyObject *t = (PyObject *)op;
135 PyString_InternInPlace(&t);
Tim Peters4862ab72001-05-09 08:43:21 +0000136 op = (PyStringObject *)t;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000137 nullstring = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000139 } else if (size == 1) {
Tim Peters9e897f42001-05-09 07:37:07 +0000140 PyObject *t = (PyObject *)op;
141 PyString_InternInPlace(&t);
Tim Peters4862ab72001-05-09 08:43:21 +0000142 op = (PyStringObject *)t;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000143 characters[*str & UCHAR_MAX] = op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 Py_INCREF(op);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000145 }
Sjoerd Mullender615194a1993-11-01 13:46:50 +0000146#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148}
149
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000150PyObject *PyString_Decode(const char *s,
151 int size,
152 const char *encoding,
153 const char *errors)
154{
155 PyObject *buffer = NULL, *str;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000156
157 if (encoding == NULL)
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000158 encoding = PyUnicode_GetDefaultEncoding();
159
160 /* Decode via the codec registry */
161 buffer = PyBuffer_FromMemory((void *)s, size);
162 if (buffer == NULL)
163 goto onError;
164 str = PyCodec_Decode(buffer, encoding, errors);
165 if (str == NULL)
166 goto onError;
167 /* Convert Unicode to a string using the default encoding */
168 if (PyUnicode_Check(str)) {
169 PyObject *temp = str;
170 str = PyUnicode_AsEncodedString(str, NULL, NULL);
171 Py_DECREF(temp);
172 if (str == NULL)
173 goto onError;
174 }
175 if (!PyString_Check(str)) {
176 PyErr_Format(PyExc_TypeError,
Andrew M. Kuchlingbd9848d2000-07-12 02:58:28 +0000177 "decoder did not return a string object (type=%.400s)",
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000178 str->ob_type->tp_name);
179 Py_DECREF(str);
180 goto onError;
181 }
182 Py_DECREF(buffer);
183 return str;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000184
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000185 onError:
186 Py_XDECREF(buffer);
187 return NULL;
188}
189
190PyObject *PyString_Encode(const char *s,
191 int size,
192 const char *encoding,
193 const char *errors)
194{
195 PyObject *v, *str;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000196
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000197 str = PyString_FromStringAndSize(s, size);
198 if (str == NULL)
199 return NULL;
200 v = PyString_AsEncodedString(str, encoding, errors);
201 Py_DECREF(str);
202 return v;
203}
204
205PyObject *PyString_AsEncodedString(PyObject *str,
206 const char *encoding,
207 const char *errors)
208{
209 PyObject *v;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000210
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000211 if (!PyString_Check(str)) {
212 PyErr_BadArgument();
213 goto onError;
214 }
215
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000216 if (encoding == NULL)
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000217 encoding = PyUnicode_GetDefaultEncoding();
218
219 /* Encode via the codec registry */
220 v = PyCodec_Encode(str, encoding, errors);
221 if (v == NULL)
222 goto onError;
223 /* Convert Unicode to a string using the default encoding */
224 if (PyUnicode_Check(v)) {
225 PyObject *temp = v;
226 v = PyUnicode_AsEncodedString(v, NULL, NULL);
227 Py_DECREF(temp);
228 if (v == NULL)
229 goto onError;
230 }
231 if (!PyString_Check(v)) {
232 PyErr_Format(PyExc_TypeError,
233 "encoder did not return a string object (type=%.400s)",
234 v->ob_type->tp_name);
235 Py_DECREF(v);
236 goto onError;
237 }
238 return v;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000239
Marc-André Lemburg63f3d172000-07-06 11:29:01 +0000240 onError:
241 return NULL;
242}
243
Guido van Rossum234f9421993-06-17 12:35:49 +0000244static void
Fred Drakeba096332000-07-09 07:04:36 +0000245string_dealloc(PyObject *op)
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000246{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000247 PyObject_DEL(op);
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000248}
249
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000250static int
251string_getsize(register PyObject *op)
252{
253 char *s;
254 int len;
255 if (PyString_AsStringAndSize(op, &s, &len))
256 return -1;
257 return len;
258}
259
260static /*const*/ char *
261string_getbuffer(register PyObject *op)
262{
263 char *s;
264 int len;
265 if (PyString_AsStringAndSize(op, &s, &len))
266 return NULL;
267 return s;
268}
269
Guido van Rossumd7047b31995-01-02 19:07:15 +0000270int
Fred Drakeba096332000-07-09 07:04:36 +0000271PyString_Size(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272{
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000273 if (!PyString_Check(op))
274 return string_getsize(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 return ((PyStringObject *)op) -> ob_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
277
278/*const*/ char *
Fred Drakeba096332000-07-09 07:04:36 +0000279PyString_AsString(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280{
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000281 if (!PyString_Check(op))
282 return string_getbuffer(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283 return ((PyStringObject *)op) -> ob_sval;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284}
285
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000286/* Internal API needed by PyString_AsStringAndSize(): */
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000287extern
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000288PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
289 const char *errors);
290
291int
292PyString_AsStringAndSize(register PyObject *obj,
293 register char **s,
294 register int *len)
295{
296 if (s == NULL) {
297 PyErr_BadInternalCall();
298 return -1;
299 }
300
301 if (!PyString_Check(obj)) {
302 if (PyUnicode_Check(obj)) {
303 obj = _PyUnicode_AsDefaultEncodedString(obj, NULL);
304 if (obj == NULL)
305 return -1;
306 }
307 else {
308 PyErr_Format(PyExc_TypeError,
309 "expected string or Unicode object, "
310 "%.200s found", obj->ob_type->tp_name);
311 return -1;
312 }
313 }
314
315 *s = PyString_AS_STRING(obj);
316 if (len != NULL)
317 *len = PyString_GET_SIZE(obj);
318 else if ((int)strlen(*s) != PyString_GET_SIZE(obj)) {
319 PyErr_SetString(PyExc_TypeError,
320 "expected string without null bytes");
321 return -1;
322 }
323 return 0;
324}
325
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326/* Methods */
327
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000328static int
Fred Drakeba096332000-07-09 07:04:36 +0000329string_print(PyStringObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330{
331 int i;
332 char c;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000333 int quote;
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000334 /* XXX Ought to check for interrupts when writing long strings */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 if (flags & Py_PRINT_RAW) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336 fwrite(op->ob_sval, 1, (int) op->ob_size, fp);
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000337 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000339
Thomas Wouters7e474022000-07-16 12:04:32 +0000340 /* figure out which quote to use; single is preferred */
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000341 quote = '\'';
342 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
343 quote = '"';
344
345 fputc(quote, fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000346 for (i = 0; i < op->ob_size; i++) {
347 c = op->ob_sval[i];
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000348 if (c == quote || c == '\\')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 fprintf(fp, "\\%c", c);
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000350 else if (c == '\t')
351 fprintf(fp, "\\t");
352 else if (c == '\n')
353 fprintf(fp, "\\n");
354 else if (c == '\r')
355 fprintf(fp, "\\r");
356 else if (c < ' ' || c >= 0x7f)
357 fprintf(fp, "\\x%02x", c & 0xff);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358 else
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000359 fputc(c, fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000361 fputc(quote, fp);
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000362 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000363}
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000366string_repr(register PyStringObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Marc-André Lemburgf28dd832000-06-30 10:29:57 +0000368 size_t newsize = 2 + 4 * op->ob_size * sizeof(char);
369 PyObject *v;
370 if (newsize > INT_MAX) {
371 PyErr_SetString(PyExc_OverflowError,
372 "string is too large to make repr");
373 }
374 v = PyString_FromStringAndSize((char *)NULL, newsize);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000375 if (v == NULL) {
Guido van Rossumbcaa31c1991-06-07 22:58:57 +0000376 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377 }
378 else {
379 register int i;
380 register char c;
381 register char *p;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000382 int quote;
383
Thomas Wouters7e474022000-07-16 12:04:32 +0000384 /* figure out which quote to use; single is preferred */
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000385 quote = '\'';
386 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
387 quote = '"';
388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389 p = ((PyStringObject *)v)->ob_sval;
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000390 *p++ = quote;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 for (i = 0; i < op->ob_size; i++) {
392 c = op->ob_sval[i];
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000393 if (c == quote || c == '\\')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394 *p++ = '\\', *p++ = c;
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000395 else if (c == '\t')
396 *p++ = '\\', *p++ = 't';
397 else if (c == '\n')
398 *p++ = '\\', *p++ = 'n';
399 else if (c == '\r')
400 *p++ = '\\', *p++ = 'r';
401 else if (c < ' ' || c >= 0x7f) {
402 sprintf(p, "\\x%02x", c & 0xff);
403 p += 4;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404 }
405 else
406 *p++ = c;
407 }
Guido van Rossum444fc7c1993-10-26 15:25:16 +0000408 *p++ = quote;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409 *p = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 _PyString_Resize(
411 &v, (int) (p - ((PyStringObject *)v)->ob_sval));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000412 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414}
415
Guido van Rossum189f1df2001-05-01 16:51:53 +0000416static PyObject *
417string_str(PyObject *s)
418{
419 Py_INCREF(s);
420 return s;
421}
422
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423static int
Fred Drakeba096332000-07-09 07:04:36 +0000424string_length(PyStringObject *a)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000425{
426 return a->ob_size;
427}
428
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000430string_concat(register PyStringObject *a, register PyObject *bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431{
432 register unsigned int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 register PyStringObject *op;
434 if (!PyString_Check(bb)) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000435 if (PyUnicode_Check(bb))
436 return PyUnicode_Concat((PyObject *)a, bb);
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000437 PyErr_Format(PyExc_TypeError,
Fred Drakeb6a9ada2000-06-01 03:12:13 +0000438 "cannot add type \"%.200s\" to string",
439 bb->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440 return NULL;
441 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442#define b ((PyStringObject *)bb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443 /* Optimize cases with empty left or right operand */
444 if (a->ob_size == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 Py_INCREF(bb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 return bb;
447 }
448 if (b->ob_size == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449 Py_INCREF(a);
450 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000451 }
452 size = a->ob_size + b->ob_size;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000453 /* PyObject_NewVar is inlined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 op = (PyStringObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000455 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char));
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000456 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000458 PyObject_INIT_VAR(op, &PyString_Type, size);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000459#ifdef CACHE_HASH
460 op->ob_shash = -1;
461#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000462#ifdef INTERN_STRINGS
463 op->ob_sinterned = NULL;
464#endif
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000465 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
466 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
467 op->ob_sval[size] = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469#undef b
470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000473string_repeat(register PyStringObject *a, register int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
475 register int i;
Guido van Rossum2095d241997-04-09 19:41:24 +0000476 register int size;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 register PyStringObject *op;
Tim Peters8f422462000-09-09 06:13:41 +0000478 size_t nbytes;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479 if (n < 0)
480 n = 0;
Tim Peters8f422462000-09-09 06:13:41 +0000481 /* watch out for overflows: the size can overflow int,
482 * and the # of bytes needed can overflow size_t
483 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 size = a->ob_size * n;
Tim Peters8f422462000-09-09 06:13:41 +0000485 if (n && size / n != a->ob_size) {
486 PyErr_SetString(PyExc_OverflowError,
487 "repeated string is too long");
488 return NULL;
489 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 if (size == a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 Py_INCREF(a);
492 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493 }
Tim Peters8f422462000-09-09 06:13:41 +0000494 nbytes = size * sizeof(char);
495 if (nbytes / sizeof(char) != (size_t)size ||
496 nbytes + sizeof(PyStringObject) <= nbytes) {
497 PyErr_SetString(PyExc_OverflowError,
498 "repeated string is too long");
499 return NULL;
500 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 op = (PyStringObject *)
Tim Peters8f422462000-09-09 06:13:41 +0000502 PyObject_MALLOC(sizeof(PyStringObject) + nbytes);
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000503 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000505 PyObject_INIT_VAR(op, &PyString_Type, size);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000506#ifdef CACHE_HASH
507 op->ob_shash = -1;
508#endif
Guido van Rossum2a61e741997-01-18 07:55:05 +0000509#ifdef INTERN_STRINGS
510 op->ob_sinterned = NULL;
511#endif
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000512 for (i = 0; i < size; i += a->ob_size)
513 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
514 op->ob_sval[size] = '\0';
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516}
517
518/* String slice a[i:j] consists of characters a[i] ... a[j-1] */
519
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000521string_slice(register PyStringObject *a, register int i, register int j)
522 /* j -- may be negative! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523{
524 if (i < 0)
525 i = 0;
526 if (j < 0)
527 j = 0; /* Avoid signed/unsigned bug in next line */
528 if (j > a->ob_size)
529 j = a->ob_size;
530 if (i == 0 && j == a->ob_size) { /* It's the same as a */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531 Py_INCREF(a);
532 return (PyObject *)a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000533 }
534 if (j < i)
535 j = i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537}
538
Guido van Rossum9284a572000-03-07 15:53:43 +0000539static int
Fred Drakeba096332000-07-09 07:04:36 +0000540string_contains(PyObject *a, PyObject *el)
Guido van Rossum9284a572000-03-07 15:53:43 +0000541{
542 register char *s, *end;
543 register char c;
Guido van Rossum90daa872000-04-10 13:47:21 +0000544 if (PyUnicode_Check(el))
Guido van Rossum96a45ad2000-03-13 15:56:08 +0000545 return PyUnicode_Contains(a, el);
Guido van Rossum90daa872000-04-10 13:47:21 +0000546 if (!PyString_Check(el) || PyString_Size(el) != 1) {
Guido van Rossum9284a572000-03-07 15:53:43 +0000547 PyErr_SetString(PyExc_TypeError,
Andrew M. Kuchlingcb95a142000-06-09 14:04:53 +0000548 "'in <string>' requires character as left operand");
Guido van Rossum9284a572000-03-07 15:53:43 +0000549 return -1;
550 }
551 c = PyString_AsString(el)[0];
552 s = PyString_AsString(a);
553 end = s + PyString_Size(a);
554 while (s < end) {
555 if (c == *s++)
556 return 1;
557 }
558 return 0;
559}
560
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000562string_item(PyStringObject *a, register int i)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 PyObject *v;
Tim Peters5b4d4772001-05-08 22:33:50 +0000565 char *pchar;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566 if (i < 0 || i >= a->ob_size) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 PyErr_SetString(PyExc_IndexError, "string index out of range");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 return NULL;
569 }
Tim Peters5b4d4772001-05-08 22:33:50 +0000570 pchar = a->ob_sval + i;
Tim Peterscf5ad5d2001-05-09 00:24:55 +0000571 v = (PyObject *)characters[*pchar & UCHAR_MAX];
Tim Peters5b4d4772001-05-08 22:33:50 +0000572 if (v == NULL)
573 v = PyString_FromStringAndSize(pchar, 1);
Tim Petersb4bbcd72001-05-09 00:31:40 +0000574 else {
575#ifdef COUNT_ALLOCS
576 one_strings++;
577#endif
Tim Peterscf5ad5d2001-05-09 00:24:55 +0000578 Py_INCREF(v);
Tim Petersb4bbcd72001-05-09 00:31:40 +0000579 }
Guido van Rossumdaa8bb31991-04-04 10:48:33 +0000580 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581}
582
583static int
Fred Drakeba096332000-07-09 07:04:36 +0000584string_compare(PyStringObject *a, PyStringObject *b)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585{
Guido van Rossum253919f1991-02-13 23:18:39 +0000586 int len_a = a->ob_size, len_b = b->ob_size;
587 int min_len = (len_a < len_b) ? len_a : len_b;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000588 int cmp;
589 if (min_len > 0) {
Guido van Rossumfde7a751996-10-23 14:19:40 +0000590 cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000591 if (cmp == 0)
592 cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
593 if (cmp != 0)
594 return cmp;
595 }
Guido van Rossum253919f1991-02-13 23:18:39 +0000596 return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597}
598
Guido van Rossum9bfef441993-03-29 10:43:31 +0000599static long
Fred Drakeba096332000-07-09 07:04:36 +0000600string_hash(PyStringObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000601{
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000602 register int len;
603 register unsigned char *p;
604 register long x;
605
606#ifdef CACHE_HASH
607 if (a->ob_shash != -1)
608 return a->ob_shash;
Guido van Rossum36b9f791997-02-14 16:29:22 +0000609#ifdef INTERN_STRINGS
610 if (a->ob_sinterned != NULL)
611 return (a->ob_shash =
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 ((PyStringObject *)(a->ob_sinterned))->ob_shash);
Guido van Rossum36b9f791997-02-14 16:29:22 +0000613#endif
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000614#endif
615 len = a->ob_size;
616 p = (unsigned char *) a->ob_sval;
617 x = *p << 7;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000618 while (--len >= 0)
Guido van Rossumeddcb3b1996-09-11 20:22:48 +0000619 x = (1000003*x) ^ *p++;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000620 x ^= a->ob_size;
621 if (x == -1)
622 x = -2;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000623#ifdef CACHE_HASH
624 a->ob_shash = x;
625#endif
Guido van Rossum9bfef441993-03-29 10:43:31 +0000626 return x;
627}
628
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000629static int
Fred Drakeba096332000-07-09 07:04:36 +0000630string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000631{
632 if ( index != 0 ) {
Guido van Rossum045e6881997-09-08 18:30:11 +0000633 PyErr_SetString(PyExc_SystemError,
Guido van Rossum1db70701998-10-08 02:18:52 +0000634 "accessing non-existent string segment");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000635 return -1;
636 }
637 *ptr = (void *)self->ob_sval;
638 return self->ob_size;
639}
640
641static int
Fred Drakeba096332000-07-09 07:04:36 +0000642string_buffer_getwritebuf(PyStringObject *self, int index, const void **ptr)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000643{
Guido van Rossum045e6881997-09-08 18:30:11 +0000644 PyErr_SetString(PyExc_TypeError,
Guido van Rossum07d78001998-10-01 15:59:48 +0000645 "Cannot use string as modifiable buffer");
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000646 return -1;
647}
648
649static int
Fred Drakeba096332000-07-09 07:04:36 +0000650string_buffer_getsegcount(PyStringObject *self, int *lenp)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000651{
652 if ( lenp )
653 *lenp = self->ob_size;
654 return 1;
655}
656
Guido van Rossum1db70701998-10-08 02:18:52 +0000657static int
Fred Drakeba096332000-07-09 07:04:36 +0000658string_buffer_getcharbuf(PyStringObject *self, int index, const char **ptr)
Guido van Rossum1db70701998-10-08 02:18:52 +0000659{
660 if ( index != 0 ) {
661 PyErr_SetString(PyExc_SystemError,
662 "accessing non-existent string segment");
663 return -1;
664 }
665 *ptr = self->ob_sval;
666 return self->ob_size;
667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PySequenceMethods string_as_sequence = {
Guido van Rossum013142a1994-08-30 08:19:36 +0000670 (inquiry)string_length, /*sq_length*/
671 (binaryfunc)string_concat, /*sq_concat*/
672 (intargfunc)string_repeat, /*sq_repeat*/
673 (intargfunc)string_item, /*sq_item*/
674 (intintargfunc)string_slice, /*sq_slice*/
Guido van Rossumf380e661991-06-04 19:36:32 +0000675 0, /*sq_ass_item*/
676 0, /*sq_ass_slice*/
Guido van Rossum9284a572000-03-07 15:53:43 +0000677 (objobjproc)string_contains /*sq_contains*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678};
679
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000680static PyBufferProcs string_as_buffer = {
681 (getreadbufferproc)string_buffer_getreadbuf,
682 (getwritebufferproc)string_buffer_getwritebuf,
683 (getsegcountproc)string_buffer_getsegcount,
Guido van Rossum1db70701998-10-08 02:18:52 +0000684 (getcharbufferproc)string_buffer_getcharbuf,
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000685};
686
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000687
688
689#define LEFTSTRIP 0
690#define RIGHTSTRIP 1
691#define BOTHSTRIP 2
692
693
694static PyObject *
Tim Petersc2e7da92000-07-09 08:02:21 +0000695split_whitespace(const char *s, int len, int maxsplit)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000696{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000697 int i, j, err;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000698 PyObject* item;
699 PyObject *list = PyList_New(0);
700
701 if (list == NULL)
702 return NULL;
703
Guido van Rossum4c08d552000-03-10 22:55:18 +0000704 for (i = j = 0; i < len; ) {
705 while (i < len && isspace(Py_CHARMASK(s[i])))
706 i++;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000707 j = i;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000708 while (i < len && !isspace(Py_CHARMASK(s[i])))
709 i++;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000710 if (j < i) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000711 if (maxsplit-- <= 0)
712 break;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000713 item = PyString_FromStringAndSize(s+j, (int)(i-j));
714 if (item == NULL)
715 goto finally;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000716 err = PyList_Append(list, item);
717 Py_DECREF(item);
718 if (err < 0)
719 goto finally;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000720 while (i < len && isspace(Py_CHARMASK(s[i])))
721 i++;
722 j = i;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000723 }
724 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000725 if (j < len) {
726 item = PyString_FromStringAndSize(s+j, (int)(len - j));
727 if (item == NULL)
728 goto finally;
729 err = PyList_Append(list, item);
730 Py_DECREF(item);
731 if (err < 0)
732 goto finally;
733 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000734 return list;
735 finally:
736 Py_DECREF(list);
737 return NULL;
738}
739
740
741static char split__doc__[] =
742"S.split([sep [,maxsplit]]) -> list of strings\n\
743\n\
744Return a list of the words in the string S, using sep as the\n\
Guido van Rossum4c08d552000-03-10 22:55:18 +0000745delimiter string. If maxsplit is given, at most maxsplit\n\
746splits are done. If sep is not specified, any whitespace string\n\
747is a separator.";
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000748
749static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000750string_split(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000751{
752 int len = PyString_GET_SIZE(self), n, i, j, err;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000753 int maxsplit = -1;
754 const char *s = PyString_AS_STRING(self), *sub;
755 PyObject *list, *item, *subobj = Py_None;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000756
Guido van Rossum4c08d552000-03-10 22:55:18 +0000757 if (!PyArg_ParseTuple(args, "|Oi:split", &subobj, &maxsplit))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000758 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000759 if (maxsplit < 0)
760 maxsplit = INT_MAX;
761 if (subobj == Py_None)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000762 return split_whitespace(s, len, maxsplit);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000763 if (PyString_Check(subobj)) {
764 sub = PyString_AS_STRING(subobj);
765 n = PyString_GET_SIZE(subobj);
766 }
767 else if (PyUnicode_Check(subobj))
768 return PyUnicode_Split((PyObject *)self, subobj, maxsplit);
769 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
770 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000771 if (n == 0) {
772 PyErr_SetString(PyExc_ValueError, "empty separator");
773 return NULL;
774 }
775
776 list = PyList_New(0);
777 if (list == NULL)
778 return NULL;
779
780 i = j = 0;
781 while (i+n <= len) {
Fred Drake396f6e02000-06-20 15:47:54 +0000782 if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) {
Guido van Rossum4c08d552000-03-10 22:55:18 +0000783 if (maxsplit-- <= 0)
784 break;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000785 item = PyString_FromStringAndSize(s+j, (int)(i-j));
786 if (item == NULL)
787 goto fail;
788 err = PyList_Append(list, item);
789 Py_DECREF(item);
790 if (err < 0)
791 goto fail;
792 i = j = i + n;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000793 }
794 else
795 i++;
796 }
797 item = PyString_FromStringAndSize(s+j, (int)(len-j));
798 if (item == NULL)
799 goto fail;
800 err = PyList_Append(list, item);
801 Py_DECREF(item);
802 if (err < 0)
803 goto fail;
804
805 return list;
806
807 fail:
808 Py_DECREF(list);
809 return NULL;
810}
811
812
813static char join__doc__[] =
814"S.join(sequence) -> string\n\
815\n\
Guido van Rossum4c08d552000-03-10 22:55:18 +0000816Return a string which is the concatenation of the strings in the\n\
817sequence. The separator between elements is S.";
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000818
819static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000820string_join(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000821{
822 char *sep = PyString_AS_STRING(self);
Tim Peters19fe14e2001-01-19 03:03:47 +0000823 const int seplen = PyString_GET_SIZE(self);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000824 PyObject *res = NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000825 char *p;
826 int seqlen = 0;
Tim Peters19fe14e2001-01-19 03:03:47 +0000827 size_t sz = 0;
828 int i;
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000829 PyObject *orig, *seq, *item;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000830
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000831 if (!PyArg_ParseTuple(args, "O:join", &orig))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000832 return NULL;
833
Tim Peters19fe14e2001-01-19 03:03:47 +0000834 seq = PySequence_Fast(orig, "");
835 if (seq == NULL) {
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000836 if (PyErr_ExceptionMatches(PyExc_TypeError))
837 PyErr_Format(PyExc_TypeError,
838 "sequence expected, %.80s found",
839 orig->ob_type->tp_name);
840 return NULL;
841 }
Tim Peters19fe14e2001-01-19 03:03:47 +0000842
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000843 seqlen = PySequence_Size(seq);
Tim Peters19fe14e2001-01-19 03:03:47 +0000844 if (seqlen == 0) {
845 Py_DECREF(seq);
846 return PyString_FromString("");
847 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000848 if (seqlen == 1) {
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000849 item = PySequence_Fast_GET_ITEM(seq, 0);
Tim Peters19fe14e2001-01-19 03:03:47 +0000850 if (!PyString_Check(item) && !PyUnicode_Check(item)) {
851 PyErr_Format(PyExc_TypeError,
852 "sequence item 0: expected string,"
853 " %.80s found",
854 item->ob_type->tp_name);
855 Py_DECREF(seq);
856 return NULL;
857 }
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000858 Py_INCREF(item);
Barry Warsaw771d0672000-07-11 04:58:12 +0000859 Py_DECREF(seq);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000860 return item;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000861 }
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000862
Tim Peters19fe14e2001-01-19 03:03:47 +0000863 /* There are at least two things to join. Do a pre-pass to figure out
864 * the total amount of space we'll need (sz), see whether any argument
865 * is absurd, and defer to the Unicode join if appropriate.
866 */
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000867 for (i = 0; i < seqlen; i++) {
Tim Peters19fe14e2001-01-19 03:03:47 +0000868 const size_t old_sz = sz;
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000869 item = PySequence_Fast_GET_ITEM(seq, i);
870 if (!PyString_Check(item)){
871 if (PyUnicode_Check(item)) {
Tim Peters2cfe3682001-05-05 05:36:48 +0000872 /* Defer to Unicode join.
873 * CAUTION: There's no gurantee that the
874 * original sequence can be iterated over
875 * again, so we must pass seq here.
876 */
877 PyObject *result;
878 result = PyUnicode_Join((PyObject *)self, seq);
Barry Warsaw771d0672000-07-11 04:58:12 +0000879 Py_DECREF(seq);
Tim Peters2cfe3682001-05-05 05:36:48 +0000880 return result;
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000881 }
882 PyErr_Format(PyExc_TypeError,
Jeremy Hylton88887aa2000-07-11 20:55:38 +0000883 "sequence item %i: expected string,"
884 " %.80s found",
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000885 i, item->ob_type->tp_name);
Tim Peters19fe14e2001-01-19 03:03:47 +0000886 Py_DECREF(seq);
887 return NULL;
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000888 }
Tim Peters19fe14e2001-01-19 03:03:47 +0000889 sz += PyString_GET_SIZE(item);
890 if (i != 0)
891 sz += seplen;
892 if (sz < old_sz || sz > INT_MAX) {
893 PyErr_SetString(PyExc_OverflowError,
894 "join() is too long for a Python string");
895 Py_DECREF(seq);
896 return NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000897 }
Tim Peters19fe14e2001-01-19 03:03:47 +0000898 }
899
900 /* Allocate result space. */
901 res = PyString_FromStringAndSize((char*)NULL, (int)sz);
902 if (res == NULL) {
903 Py_DECREF(seq);
904 return NULL;
905 }
906
907 /* Catenate everything. */
908 p = PyString_AS_STRING(res);
909 for (i = 0; i < seqlen; ++i) {
910 size_t n;
911 item = PySequence_Fast_GET_ITEM(seq, i);
912 n = PyString_GET_SIZE(item);
913 memcpy(p, PyString_AS_STRING(item), n);
914 p += n;
915 if (i < seqlen - 1) {
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000916 memcpy(p, sep, seplen);
917 p += seplen;
Jeremy Hylton194e43e2000-07-10 21:30:28 +0000918 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000919 }
Tim Peters19fe14e2001-01-19 03:03:47 +0000920
Jeremy Hylton49048292000-07-11 03:28:17 +0000921 Py_DECREF(seq);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000922 return res;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000923}
924
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000925static long
Fred Drakeba096332000-07-09 07:04:36 +0000926string_find_internal(PyStringObject *self, PyObject *args, int dir)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000927{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000928 const char *s = PyString_AS_STRING(self), *sub;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000929 int len = PyString_GET_SIZE(self);
930 int n, i = 0, last = INT_MAX;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000931 PyObject *subobj;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000932
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000933 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex",
Guido van Rossumc6821402000-05-08 14:08:05 +0000934 &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
Guido van Rossum4c08d552000-03-10 22:55:18 +0000935 return -2;
936 if (PyString_Check(subobj)) {
937 sub = PyString_AS_STRING(subobj);
938 n = PyString_GET_SIZE(subobj);
939 }
940 else if (PyUnicode_Check(subobj))
941 return PyUnicode_Find((PyObject *)self, subobj, i, last, 1);
942 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000943 return -2;
944
945 if (last > len)
946 last = len;
947 if (last < 0)
948 last += len;
949 if (last < 0)
950 last = 0;
951 if (i < 0)
952 i += len;
953 if (i < 0)
954 i = 0;
955
Guido van Rossum4c08d552000-03-10 22:55:18 +0000956 if (dir > 0) {
957 if (n == 0 && i <= last)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000958 return (long)i;
Guido van Rossum4c08d552000-03-10 22:55:18 +0000959 last -= n;
960 for (; i <= last; ++i)
Fred Drake396f6e02000-06-20 15:47:54 +0000961 if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000962 return (long)i;
963 }
964 else {
965 int j;
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000966
Guido van Rossum4c08d552000-03-10 22:55:18 +0000967 if (n == 0 && i <= last)
968 return (long)last;
969 for (j = last-n; j >= i; --j)
Fred Drake396f6e02000-06-20 15:47:54 +0000970 if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000971 return (long)j;
972 }
Tim Petersb3d8d1f2001-04-28 05:38:26 +0000973
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000974 return -1;
975}
976
977
978static char find__doc__[] =
979"S.find(sub [,start [,end]]) -> int\n\
980\n\
981Return the lowest index in S where substring sub is found,\n\
982such that sub is contained within s[start,end]. Optional\n\
983arguments start and end are interpreted as in slice notation.\n\
984\n\
985Return -1 on failure.";
986
987static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +0000988string_find(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000989{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000990 long result = string_find_internal(self, args, +1);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000991 if (result == -2)
992 return NULL;
993 return PyInt_FromLong(result);
994}
995
996
997static char index__doc__[] =
998"S.index(sub [,start [,end]]) -> int\n\
999\n\
1000Like S.find() but raise ValueError when the substring is not found.";
1001
1002static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001003string_index(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001004{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001005 long result = string_find_internal(self, args, +1);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001006 if (result == -2)
1007 return NULL;
1008 if (result == -1) {
1009 PyErr_SetString(PyExc_ValueError,
1010 "substring not found in string.index");
1011 return NULL;
1012 }
1013 return PyInt_FromLong(result);
1014}
1015
1016
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001017static char rfind__doc__[] =
1018"S.rfind(sub [,start [,end]]) -> int\n\
1019\n\
1020Return the highest index in S where substring sub is found,\n\
1021such that sub is contained within s[start,end]. Optional\n\
1022arguments start and end are interpreted as in slice notation.\n\
1023\n\
1024Return -1 on failure.";
1025
1026static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001027string_rfind(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001028{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001029 long result = string_find_internal(self, args, -1);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001030 if (result == -2)
1031 return NULL;
1032 return PyInt_FromLong(result);
1033}
1034
1035
1036static char rindex__doc__[] =
1037"S.rindex(sub [,start [,end]]) -> int\n\
1038\n\
1039Like S.rfind() but raise ValueError when the substring is not found.";
1040
1041static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001042string_rindex(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001043{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001044 long result = string_find_internal(self, args, -1);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001045 if (result == -2)
1046 return NULL;
1047 if (result == -1) {
1048 PyErr_SetString(PyExc_ValueError,
1049 "substring not found in string.rindex");
1050 return NULL;
1051 }
1052 return PyInt_FromLong(result);
1053}
1054
1055
1056static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001057do_strip(PyStringObject *self, PyObject *args, int striptype)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001058{
1059 char *s = PyString_AS_STRING(self);
1060 int len = PyString_GET_SIZE(self), i, j;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001061
Guido van Rossum43713e52000-02-29 13:59:29 +00001062 if (!PyArg_ParseTuple(args, ":strip"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001063 return NULL;
1064
1065 i = 0;
1066 if (striptype != RIGHTSTRIP) {
1067 while (i < len && isspace(Py_CHARMASK(s[i]))) {
1068 i++;
1069 }
1070 }
1071
1072 j = len;
1073 if (striptype != LEFTSTRIP) {
1074 do {
1075 j--;
1076 } while (j >= i && isspace(Py_CHARMASK(s[j])));
1077 j++;
1078 }
1079
1080 if (i == 0 && j == len) {
1081 Py_INCREF(self);
1082 return (PyObject*)self;
1083 }
1084 else
1085 return PyString_FromStringAndSize(s+i, j-i);
1086}
1087
1088
1089static char strip__doc__[] =
1090"S.strip() -> string\n\
1091\n\
1092Return a copy of the string S with leading and trailing\n\
1093whitespace removed.";
1094
1095static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001096string_strip(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001097{
1098 return do_strip(self, args, BOTHSTRIP);
1099}
1100
1101
1102static char lstrip__doc__[] =
1103"S.lstrip() -> string\n\
1104\n\
1105Return a copy of the string S with leading whitespace removed.";
1106
1107static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001108string_lstrip(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001109{
1110 return do_strip(self, args, LEFTSTRIP);
1111}
1112
1113
1114static char rstrip__doc__[] =
1115"S.rstrip() -> string\n\
1116\n\
1117Return a copy of the string S with trailing whitespace removed.";
1118
1119static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001120string_rstrip(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001121{
1122 return do_strip(self, args, RIGHTSTRIP);
1123}
1124
1125
1126static char lower__doc__[] =
1127"S.lower() -> string\n\
1128\n\
1129Return a copy of the string S converted to lowercase.";
1130
1131static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001132string_lower(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001133{
1134 char *s = PyString_AS_STRING(self), *s_new;
1135 int i, n = PyString_GET_SIZE(self);
1136 PyObject *new;
1137
Guido van Rossum43713e52000-02-29 13:59:29 +00001138 if (!PyArg_ParseTuple(args, ":lower"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001139 return NULL;
1140 new = PyString_FromStringAndSize(NULL, n);
1141 if (new == NULL)
1142 return NULL;
1143 s_new = PyString_AsString(new);
1144 for (i = 0; i < n; i++) {
1145 int c = Py_CHARMASK(*s++);
1146 if (isupper(c)) {
1147 *s_new = tolower(c);
1148 } else
1149 *s_new = c;
1150 s_new++;
1151 }
1152 return new;
1153}
1154
1155
1156static char upper__doc__[] =
1157"S.upper() -> string\n\
1158\n\
1159Return a copy of the string S converted to uppercase.";
1160
1161static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001162string_upper(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001163{
1164 char *s = PyString_AS_STRING(self), *s_new;
1165 int i, n = PyString_GET_SIZE(self);
1166 PyObject *new;
1167
Guido van Rossum43713e52000-02-29 13:59:29 +00001168 if (!PyArg_ParseTuple(args, ":upper"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001169 return NULL;
1170 new = PyString_FromStringAndSize(NULL, n);
1171 if (new == NULL)
1172 return NULL;
1173 s_new = PyString_AsString(new);
1174 for (i = 0; i < n; i++) {
1175 int c = Py_CHARMASK(*s++);
1176 if (islower(c)) {
1177 *s_new = toupper(c);
1178 } else
1179 *s_new = c;
1180 s_new++;
1181 }
1182 return new;
1183}
1184
1185
Guido van Rossum4c08d552000-03-10 22:55:18 +00001186static char title__doc__[] =
1187"S.title() -> string\n\
1188\n\
1189Return a titlecased version of S, i.e. words start with uppercase\n\
1190characters, all remaining cased characters have lowercase.";
1191
1192static PyObject*
Fred Drake49312a52000-12-06 14:27:49 +00001193string_title(PyStringObject *self, PyObject *args)
Guido van Rossum4c08d552000-03-10 22:55:18 +00001194{
1195 char *s = PyString_AS_STRING(self), *s_new;
1196 int i, n = PyString_GET_SIZE(self);
1197 int previous_is_cased = 0;
1198 PyObject *new;
1199
1200 if (!PyArg_ParseTuple(args, ":title"))
1201 return NULL;
1202 new = PyString_FromStringAndSize(NULL, n);
1203 if (new == NULL)
1204 return NULL;
1205 s_new = PyString_AsString(new);
1206 for (i = 0; i < n; i++) {
1207 int c = Py_CHARMASK(*s++);
1208 if (islower(c)) {
1209 if (!previous_is_cased)
1210 c = toupper(c);
1211 previous_is_cased = 1;
1212 } else if (isupper(c)) {
1213 if (previous_is_cased)
1214 c = tolower(c);
1215 previous_is_cased = 1;
1216 } else
1217 previous_is_cased = 0;
1218 *s_new++ = c;
1219 }
1220 return new;
1221}
1222
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001223static char capitalize__doc__[] =
1224"S.capitalize() -> string\n\
1225\n\
1226Return a copy of the string S with only its first character\n\
1227capitalized.";
1228
1229static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001230string_capitalize(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001231{
1232 char *s = PyString_AS_STRING(self), *s_new;
1233 int i, n = PyString_GET_SIZE(self);
1234 PyObject *new;
1235
Guido van Rossum43713e52000-02-29 13:59:29 +00001236 if (!PyArg_ParseTuple(args, ":capitalize"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001237 return NULL;
1238 new = PyString_FromStringAndSize(NULL, n);
1239 if (new == NULL)
1240 return NULL;
1241 s_new = PyString_AsString(new);
1242 if (0 < n) {
1243 int c = Py_CHARMASK(*s++);
1244 if (islower(c))
1245 *s_new = toupper(c);
1246 else
1247 *s_new = c;
1248 s_new++;
1249 }
1250 for (i = 1; i < n; i++) {
1251 int c = Py_CHARMASK(*s++);
1252 if (isupper(c))
1253 *s_new = tolower(c);
1254 else
1255 *s_new = c;
1256 s_new++;
1257 }
1258 return new;
1259}
1260
1261
1262static char count__doc__[] =
1263"S.count(sub[, start[, end]]) -> int\n\
1264\n\
1265Return the number of occurrences of substring sub in string\n\
1266S[start:end]. Optional arguments start and end are\n\
1267interpreted as in slice notation.";
1268
1269static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001270string_count(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001271{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001272 const char *s = PyString_AS_STRING(self), *sub;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001273 int len = PyString_GET_SIZE(self), n;
1274 int i = 0, last = INT_MAX;
1275 int m, r;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001276 PyObject *subobj;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001277
Guido van Rossumc6821402000-05-08 14:08:05 +00001278 if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj,
1279 _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001280 return NULL;
Guido van Rossumc6821402000-05-08 14:08:05 +00001281
Guido van Rossum4c08d552000-03-10 22:55:18 +00001282 if (PyString_Check(subobj)) {
1283 sub = PyString_AS_STRING(subobj);
1284 n = PyString_GET_SIZE(subobj);
1285 }
Marc-André Lemburg3a645e42001-01-16 11:54:12 +00001286 else if (PyUnicode_Check(subobj)) {
1287 int count;
1288 count = PyUnicode_Count((PyObject *)self, subobj, i, last);
1289 if (count == -1)
1290 return NULL;
1291 else
1292 return PyInt_FromLong((long) count);
1293 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001294 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
1295 return NULL;
1296
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001297 if (last > len)
1298 last = len;
1299 if (last < 0)
1300 last += len;
1301 if (last < 0)
1302 last = 0;
1303 if (i < 0)
1304 i += len;
1305 if (i < 0)
1306 i = 0;
1307 m = last + 1 - n;
1308 if (n == 0)
1309 return PyInt_FromLong((long) (m-i));
1310
1311 r = 0;
1312 while (i < m) {
1313 if (!memcmp(s+i, sub, n)) {
1314 r++;
1315 i += n;
1316 } else {
1317 i++;
1318 }
1319 }
1320 return PyInt_FromLong((long) r);
1321}
1322
1323
1324static char swapcase__doc__[] =
1325"S.swapcase() -> string\n\
1326\n\
Guido van Rossum4c08d552000-03-10 22:55:18 +00001327Return a copy of the string S with uppercase characters\n\
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001328converted to lowercase and vice versa.";
1329
1330static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001331string_swapcase(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001332{
1333 char *s = PyString_AS_STRING(self), *s_new;
1334 int i, n = PyString_GET_SIZE(self);
1335 PyObject *new;
1336
Guido van Rossum43713e52000-02-29 13:59:29 +00001337 if (!PyArg_ParseTuple(args, ":swapcase"))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001338 return NULL;
1339 new = PyString_FromStringAndSize(NULL, n);
1340 if (new == NULL)
1341 return NULL;
1342 s_new = PyString_AsString(new);
1343 for (i = 0; i < n; i++) {
1344 int c = Py_CHARMASK(*s++);
1345 if (islower(c)) {
1346 *s_new = toupper(c);
1347 }
1348 else if (isupper(c)) {
1349 *s_new = tolower(c);
1350 }
1351 else
1352 *s_new = c;
1353 s_new++;
1354 }
1355 return new;
1356}
1357
1358
1359static char translate__doc__[] =
1360"S.translate(table [,deletechars]) -> string\n\
1361\n\
1362Return a copy of the string S, where all characters occurring\n\
1363in the optional argument deletechars are removed, and the\n\
1364remaining characters have been mapped through the given\n\
1365translation table, which must be a string of length 256.";
1366
1367static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001368string_translate(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001369{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001370 register char *input, *output;
1371 register const char *table;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001372 register int i, c, changed = 0;
1373 PyObject *input_obj = (PyObject*)self;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001374 const char *table1, *output_start, *del_table=NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001375 int inlen, tablen, dellen = 0;
1376 PyObject *result;
1377 int trans_table[256];
Guido van Rossum4c08d552000-03-10 22:55:18 +00001378 PyObject *tableobj, *delobj = NULL;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001379
Guido van Rossum4c08d552000-03-10 22:55:18 +00001380 if (!PyArg_ParseTuple(args, "O|O:translate",
1381 &tableobj, &delobj))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001382 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001383
1384 if (PyString_Check(tableobj)) {
1385 table1 = PyString_AS_STRING(tableobj);
1386 tablen = PyString_GET_SIZE(tableobj);
1387 }
1388 else if (PyUnicode_Check(tableobj)) {
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001389 /* Unicode .translate() does not support the deletechars
Guido van Rossum4c08d552000-03-10 22:55:18 +00001390 parameter; instead a mapping to None will cause characters
1391 to be deleted. */
1392 if (delobj != NULL) {
1393 PyErr_SetString(PyExc_TypeError,
1394 "deletions are implemented differently for unicode");
1395 return NULL;
1396 }
1397 return PyUnicode_Translate((PyObject *)self, tableobj, NULL);
1398 }
1399 else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001400 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001401
1402 if (delobj != NULL) {
1403 if (PyString_Check(delobj)) {
1404 del_table = PyString_AS_STRING(delobj);
1405 dellen = PyString_GET_SIZE(delobj);
1406 }
1407 else if (PyUnicode_Check(delobj)) {
1408 PyErr_SetString(PyExc_TypeError,
1409 "deletions are implemented differently for unicode");
1410 return NULL;
1411 }
1412 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
1413 return NULL;
1414
1415 if (tablen != 256) {
1416 PyErr_SetString(PyExc_ValueError,
1417 "translation table must be 256 characters long");
1418 return NULL;
1419 }
1420 }
1421 else {
1422 del_table = NULL;
1423 dellen = 0;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001424 }
1425
1426 table = table1;
1427 inlen = PyString_Size(input_obj);
1428 result = PyString_FromStringAndSize((char *)NULL, inlen);
1429 if (result == NULL)
1430 return NULL;
1431 output_start = output = PyString_AsString(result);
1432 input = PyString_AsString(input_obj);
1433
1434 if (dellen == 0) {
1435 /* If no deletions are required, use faster code */
1436 for (i = inlen; --i >= 0; ) {
1437 c = Py_CHARMASK(*input++);
1438 if (Py_CHARMASK((*output++ = table[c])) != c)
1439 changed = 1;
1440 }
1441 if (changed)
1442 return result;
1443 Py_DECREF(result);
1444 Py_INCREF(input_obj);
1445 return input_obj;
1446 }
1447
1448 for (i = 0; i < 256; i++)
1449 trans_table[i] = Py_CHARMASK(table[i]);
1450
1451 for (i = 0; i < dellen; i++)
1452 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
1453
1454 for (i = inlen; --i >= 0; ) {
1455 c = Py_CHARMASK(*input++);
1456 if (trans_table[c] != -1)
1457 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1458 continue;
1459 changed = 1;
1460 }
1461 if (!changed) {
1462 Py_DECREF(result);
1463 Py_INCREF(input_obj);
1464 return input_obj;
1465 }
1466 /* Fix the size of the resulting string */
1467 if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
1468 return NULL;
1469 return result;
1470}
1471
1472
1473/* What follows is used for implementing replace(). Perry Stoll. */
1474
1475/*
1476 mymemfind
1477
1478 strstr replacement for arbitrary blocks of memory.
1479
Barry Warsaw51ac5802000-03-20 16:36:48 +00001480 Locates the first occurrence in the memory pointed to by MEM of the
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001481 contents of memory pointed to by PAT. Returns the index into MEM if
1482 found, or -1 if not found. If len of PAT is greater than length of
1483 MEM, the function returns -1.
1484*/
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001485static int
Tim Petersc2e7da92000-07-09 08:02:21 +00001486mymemfind(const char *mem, int len, const char *pat, int pat_len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001487{
1488 register int ii;
1489
1490 /* pattern can not occur in the last pat_len-1 chars */
1491 len -= pat_len;
1492
1493 for (ii = 0; ii <= len; ii++) {
Fred Drake396f6e02000-06-20 15:47:54 +00001494 if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) {
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001495 return ii;
1496 }
1497 }
1498 return -1;
1499}
1500
1501/*
1502 mymemcnt
1503
1504 Return the number of distinct times PAT is found in MEM.
1505 meaning mem=1111 and pat==11 returns 2.
1506 mem=11111 and pat==11 also return 2.
1507 */
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001508static int
Tim Petersc2e7da92000-07-09 08:02:21 +00001509mymemcnt(const char *mem, int len, const char *pat, int pat_len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001510{
1511 register int offset = 0;
1512 int nfound = 0;
1513
1514 while (len >= 0) {
1515 offset = mymemfind(mem, len, pat, pat_len);
1516 if (offset == -1)
1517 break;
1518 mem += offset + pat_len;
1519 len -= offset + pat_len;
1520 nfound++;
1521 }
1522 return nfound;
1523}
1524
1525/*
1526 mymemreplace
1527
Thomas Wouters7e474022000-07-16 12:04:32 +00001528 Return a string in which all occurrences of PAT in memory STR are
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001529 replaced with SUB.
1530
Thomas Wouters7e474022000-07-16 12:04:32 +00001531 If length of PAT is less than length of STR or there are no occurrences
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001532 of PAT in STR, then the original string is returned. Otherwise, a new
1533 string is allocated here and returned.
1534
1535 on return, out_len is:
1536 the length of output string, or
1537 -1 if the input string is returned, or
1538 unchanged if an error occurs (no memory).
1539
1540 return value is:
1541 the new string allocated locally, or
1542 NULL if an error occurred.
1543*/
1544static char *
Tim Petersc2e7da92000-07-09 08:02:21 +00001545mymemreplace(const char *str, int len, /* input string */
1546 const char *pat, int pat_len, /* pattern string to find */
1547 const char *sub, int sub_len, /* substitution string */
1548 int count, /* number of replacements */
Tim Peters4cd44ef2001-05-10 00:05:33 +00001549 int *out_len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001550{
1551 char *out_s;
1552 char *new_s;
1553 int nfound, offset, new_len;
1554
1555 if (len == 0 || pat_len > len)
1556 goto return_same;
1557
1558 /* find length of output string */
1559 nfound = mymemcnt(str, len, pat, pat_len);
Tim Peters4cd44ef2001-05-10 00:05:33 +00001560 if (count > 0)
1561 nfound = nfound > count ? count : nfound;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001562 if (nfound == 0)
1563 goto return_same;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001564
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001565 new_len = len + nfound*(sub_len - pat_len);
Tim Peters4cd44ef2001-05-10 00:05:33 +00001566 if (new_len == 0) {
1567 /* Have to allocate something for the caller to free(). */
1568 out_s = (char *)PyMem_MALLOC(1);
1569 if (out_s = NULL)
1570 return NULL;
1571 out_s[0] = '\0';
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001572 }
Tim Peters4cd44ef2001-05-10 00:05:33 +00001573 else {
1574 assert(new_len > 0);
1575 new_s = (char *)PyMem_MALLOC(new_len);
1576 if (new_s == NULL)
1577 return NULL;
1578 out_s = new_s;
1579
1580 while (len > 0) {
1581 /* find index of next instance of pattern */
1582 offset = mymemfind(str, len, pat, pat_len);
1583 if (offset == -1)
1584 break;
1585
1586 /* copy non matching part of input string */
1587 memcpy(new_s, str, offset);
1588 str += offset + pat_len;
1589 len -= offset + pat_len;
1590
1591 /* copy substitute into the output string */
1592 new_s += offset;
1593 memcpy(new_s, sub, sub_len);
1594 new_s += sub_len;
1595
1596 /* note count==0 is effectively infinity */
1597 if (--count == 0)
1598 break;
1599 }
1600 /* copy any remaining values into output string */
1601 if (len > 0)
1602 memcpy(new_s, str, len);
1603 }
1604 *out_len = new_len;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001605 return out_s;
1606
1607 return_same:
1608 *out_len = -1;
Tim Peters4cd44ef2001-05-10 00:05:33 +00001609 return (char *)str; /* cast away const */
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001610}
1611
1612
1613static char replace__doc__[] =
1614"S.replace (old, new[, maxsplit]) -> string\n\
1615\n\
1616Return a copy of string S with all occurrences of substring\n\
1617old replaced by new. If the optional argument maxsplit is\n\
1618given, only the first maxsplit occurrences are replaced.";
1619
1620static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001621string_replace(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001622{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001623 const char *str = PyString_AS_STRING(self), *sub, *repl;
1624 char *new_s;
1625 int len = PyString_GET_SIZE(self), sub_len, repl_len, out_len;
1626 int count = -1;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001627 PyObject *new;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001628 PyObject *subobj, *replobj;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001629
Guido van Rossum4c08d552000-03-10 22:55:18 +00001630 if (!PyArg_ParseTuple(args, "OO|i:replace",
1631 &subobj, &replobj, &count))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001632 return NULL;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001633
1634 if (PyString_Check(subobj)) {
1635 sub = PyString_AS_STRING(subobj);
1636 sub_len = PyString_GET_SIZE(subobj);
1637 }
1638 else if (PyUnicode_Check(subobj))
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001639 return PyUnicode_Replace((PyObject *)self,
Guido van Rossum4c08d552000-03-10 22:55:18 +00001640 subobj, replobj, count);
1641 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
1642 return NULL;
1643
1644 if (PyString_Check(replobj)) {
1645 repl = PyString_AS_STRING(replobj);
1646 repl_len = PyString_GET_SIZE(replobj);
1647 }
1648 else if (PyUnicode_Check(replobj))
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001649 return PyUnicode_Replace((PyObject *)self,
Guido van Rossum4c08d552000-03-10 22:55:18 +00001650 subobj, replobj, count);
1651 else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len))
1652 return NULL;
1653
Guido van Rossum96a45ad2000-03-13 15:56:08 +00001654 if (sub_len <= 0) {
Barry Warsaw51ac5802000-03-20 16:36:48 +00001655 PyErr_SetString(PyExc_ValueError, "empty pattern string");
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001656 return NULL;
1657 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001658 new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001659 if (new_s == NULL) {
1660 PyErr_NoMemory();
1661 return NULL;
1662 }
1663 if (out_len == -1) {
1664 /* we're returning another reference to self */
1665 new = (PyObject*)self;
1666 Py_INCREF(new);
1667 }
1668 else {
1669 new = PyString_FromStringAndSize(new_s, out_len);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001670 PyMem_FREE(new_s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001671 }
1672 return new;
1673}
1674
1675
1676static char startswith__doc__[] =
1677"S.startswith(prefix[, start[, end]]) -> int\n\
1678\n\
1679Return 1 if S starts with the specified prefix, otherwise return 0. With\n\
1680optional start, test S beginning at that position. With optional end, stop\n\
1681comparing S at that position.";
1682
1683static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001684string_startswith(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001685{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001686 const char* str = PyString_AS_STRING(self);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001687 int len = PyString_GET_SIZE(self);
Guido van Rossum4c08d552000-03-10 22:55:18 +00001688 const char* prefix;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001689 int plen;
1690 int start = 0;
1691 int end = -1;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001692 PyObject *subobj;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001693
Guido van Rossumc6821402000-05-08 14:08:05 +00001694 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
1695 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
Guido van Rossum4c08d552000-03-10 22:55:18 +00001696 return NULL;
1697 if (PyString_Check(subobj)) {
1698 prefix = PyString_AS_STRING(subobj);
1699 plen = PyString_GET_SIZE(subobj);
1700 }
Marc-André Lemburg3a645e42001-01-16 11:54:12 +00001701 else if (PyUnicode_Check(subobj)) {
1702 int rc;
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001703 rc = PyUnicode_Tailmatch((PyObject *)self,
Marc-André Lemburg3a645e42001-01-16 11:54:12 +00001704 subobj, start, end, -1);
1705 if (rc == -1)
1706 return NULL;
1707 else
1708 return PyInt_FromLong((long) rc);
1709 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001710 else if (PyObject_AsCharBuffer(subobj, &prefix, &plen))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001711 return NULL;
1712
1713 /* adopt Java semantics for index out of range. it is legal for
1714 * offset to be == plen, but this only returns true if prefix is
1715 * the empty string.
1716 */
1717 if (start < 0 || start+plen > len)
1718 return PyInt_FromLong(0);
1719
1720 if (!memcmp(str+start, prefix, plen)) {
1721 /* did the match end after the specified end? */
1722 if (end < 0)
1723 return PyInt_FromLong(1);
1724 else if (end - start < plen)
1725 return PyInt_FromLong(0);
1726 else
1727 return PyInt_FromLong(1);
1728 }
1729 else return PyInt_FromLong(0);
1730}
1731
1732
1733static char endswith__doc__[] =
1734"S.endswith(suffix[, start[, end]]) -> int\n\
1735\n\
1736Return 1 if S ends with the specified suffix, otherwise return 0. With\n\
1737optional start, test S beginning at that position. With optional end, stop\n\
1738comparing S at that position.";
1739
1740static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00001741string_endswith(PyStringObject *self, PyObject *args)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001742{
Guido van Rossum4c08d552000-03-10 22:55:18 +00001743 const char* str = PyString_AS_STRING(self);
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001744 int len = PyString_GET_SIZE(self);
Guido van Rossum4c08d552000-03-10 22:55:18 +00001745 const char* suffix;
1746 int slen;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001747 int start = 0;
1748 int end = -1;
1749 int lower, upper;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001750 PyObject *subobj;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001751
Guido van Rossumc6821402000-05-08 14:08:05 +00001752 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
1753 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
Guido van Rossum4c08d552000-03-10 22:55:18 +00001754 return NULL;
1755 if (PyString_Check(subobj)) {
1756 suffix = PyString_AS_STRING(subobj);
1757 slen = PyString_GET_SIZE(subobj);
1758 }
Marc-André Lemburg3a645e42001-01-16 11:54:12 +00001759 else if (PyUnicode_Check(subobj)) {
1760 int rc;
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001761 rc = PyUnicode_Tailmatch((PyObject *)self,
Marc-André Lemburg3a645e42001-01-16 11:54:12 +00001762 subobj, start, end, +1);
1763 if (rc == -1)
1764 return NULL;
1765 else
1766 return PyInt_FromLong((long) rc);
1767 }
Guido van Rossum4c08d552000-03-10 22:55:18 +00001768 else if (PyObject_AsCharBuffer(subobj, &suffix, &slen))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001769 return NULL;
1770
Guido van Rossum4c08d552000-03-10 22:55:18 +00001771 if (start < 0 || start > len || slen > len)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001772 return PyInt_FromLong(0);
1773
1774 upper = (end >= 0 && end <= len) ? end : len;
Guido van Rossum4c08d552000-03-10 22:55:18 +00001775 lower = (upper - slen) > start ? (upper - slen) : start;
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001776
Guido van Rossum4c08d552000-03-10 22:55:18 +00001777 if (upper-lower >= slen && !memcmp(str+lower, suffix, slen))
Barry Warsaw226ae6c1999-10-12 19:54:53 +00001778 return PyInt_FromLong(1);
1779 else return PyInt_FromLong(0);
1780}
1781
1782
Marc-André Lemburg63f3d172000-07-06 11:29:01 +00001783static char encode__doc__[] =
1784"S.encode([encoding[,errors]]) -> string\n\
1785\n\
1786Return an encoded string version of S. Default encoding is the current\n\
1787default string encoding. errors may be given to set a different error\n\
1788handling scheme. Default is 'strict' meaning that encoding errors raise\n\
1789a ValueError. Other possible values are 'ignore' and 'replace'.";
1790
1791static PyObject *
1792string_encode(PyStringObject *self, PyObject *args)
1793{
1794 char *encoding = NULL;
1795 char *errors = NULL;
1796 if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
1797 return NULL;
1798 return PyString_AsEncodedString((PyObject *)self, encoding, errors);
1799}
1800
1801
Guido van Rossum4c08d552000-03-10 22:55:18 +00001802static char expandtabs__doc__[] =
1803"S.expandtabs([tabsize]) -> string\n\
1804\n\
1805Return a copy of S where all tab characters are expanded using spaces.\n\
1806If tabsize is not given, a tab size of 8 characters is assumed.";
1807
1808static PyObject*
1809string_expandtabs(PyStringObject *self, PyObject *args)
1810{
1811 const char *e, *p;
1812 char *q;
1813 int i, j;
1814 PyObject *u;
1815 int tabsize = 8;
1816
1817 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
1818 return NULL;
1819
Thomas Wouters7e474022000-07-16 12:04:32 +00001820 /* First pass: determine size of output string */
Guido van Rossum4c08d552000-03-10 22:55:18 +00001821 i = j = 0;
1822 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);
1823 for (p = PyString_AS_STRING(self); p < e; p++)
1824 if (*p == '\t') {
1825 if (tabsize > 0)
1826 j += tabsize - (j % tabsize);
1827 }
1828 else {
1829 j++;
1830 if (*p == '\n' || *p == '\r') {
1831 i += j;
1832 j = 0;
1833 }
1834 }
1835
1836 /* Second pass: create output string and fill it */
1837 u = PyString_FromStringAndSize(NULL, i + j);
1838 if (!u)
1839 return NULL;
1840
1841 j = 0;
1842 q = PyString_AS_STRING(u);
1843
1844 for (p = PyString_AS_STRING(self); p < e; p++)
1845 if (*p == '\t') {
1846 if (tabsize > 0) {
1847 i = tabsize - (j % tabsize);
1848 j += i;
1849 while (i--)
1850 *q++ = ' ';
1851 }
1852 }
1853 else {
1854 j++;
1855 *q++ = *p;
1856 if (*p == '\n' || *p == '\r')
1857 j = 0;
1858 }
1859
1860 return u;
1861}
1862
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001863static
1864PyObject *pad(PyStringObject *self,
1865 int left,
Guido van Rossum4c08d552000-03-10 22:55:18 +00001866 int right,
1867 char fill)
1868{
1869 PyObject *u;
1870
1871 if (left < 0)
1872 left = 0;
1873 if (right < 0)
1874 right = 0;
1875
1876 if (left == 0 && right == 0) {
1877 Py_INCREF(self);
1878 return (PyObject *)self;
1879 }
1880
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001881 u = PyString_FromStringAndSize(NULL,
Guido van Rossum4c08d552000-03-10 22:55:18 +00001882 left + PyString_GET_SIZE(self) + right);
1883 if (u) {
1884 if (left)
1885 memset(PyString_AS_STRING(u), fill, left);
Tim Petersb3d8d1f2001-04-28 05:38:26 +00001886 memcpy(PyString_AS_STRING(u) + left,
1887 PyString_AS_STRING(self),
Guido van Rossum4c08d552000-03-10 22:55:18 +00001888 PyString_GET_SIZE(self));
1889 if (right)
1890 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self),
1891 fill, right);
1892 }
1893
1894 return u;
1895}
1896
1897static char ljust__doc__[] =
1898"S.ljust(width) -> string\n\
1899\n\
1900Return S left justified in a string of length width. Padding is\n\
1901done using spaces.";
1902
1903static PyObject *
1904string_ljust(PyStringObject *self, PyObject *args)
1905{
1906 int width;
1907 if (!PyArg_ParseTuple(args, "i:ljust", &width))
1908 return NULL;
1909
1910 if (PyString_GET_SIZE(self) >= width) {
1911 Py_INCREF(self);
1912 return (PyObject*) self;
1913 }
1914
1915 return pad(self, 0, width - PyString_GET_SIZE(self), ' ');
1916}
1917
1918
1919static char rjust__doc__[] =
1920"S.rjust(width) -> string\n\
1921\n\
1922Return S right justified in a string of length width. Padding is\n\
1923done using spaces.";
1924
1925static PyObject *
1926string_rjust(PyStringObject *self, PyObject *args)
1927{
1928 int width;
1929 if (!PyArg_ParseTuple(args, "i:rjust", &width))
1930 return NULL;
1931
1932 if (PyString_GET_SIZE(self) >= width) {
1933 Py_INCREF(self);
1934 return (PyObject*) self;
1935 }
1936
1937 return pad(self, width - PyString_GET_SIZE(self), 0, ' ');
1938}
1939
1940
1941static char center__doc__[] =
1942"S.center(width) -> string\n\
1943\n\
1944Return S centered in a string of length width. Padding is done\n\
1945using spaces.";
1946
1947static PyObject *
1948string_center(PyStringObject *self, PyObject *args)
1949{
1950 int marg, left;
1951 int width;
1952
1953 if (!PyArg_ParseTuple(args, "i:center", &width))
1954 return NULL;
1955
1956 if (PyString_GET_SIZE(self) >= width) {
1957 Py_INCREF(self);
1958 return (PyObject*) self;
1959 }
1960
1961 marg = width - PyString_GET_SIZE(self);
1962 left = marg / 2 + (marg & width & 1);
1963
1964 return pad(self, left, marg - left, ' ');
1965}
1966
1967#if 0
1968static char zfill__doc__[] =
1969"S.zfill(width) -> string\n\
1970\n\
1971Pad a numeric string x with zeros on the left, to fill a field\n\
1972of the specified width. The string x is never truncated.";
1973
1974static PyObject *
1975string_zfill(PyStringObject *self, PyObject *args)
1976{
1977 int fill;
1978 PyObject *u;
1979 char *str;
1980
1981 int width;
1982 if (!PyArg_ParseTuple(args, "i:zfill", &width))
1983 return NULL;
1984
1985 if (PyString_GET_SIZE(self) >= width) {
1986 Py_INCREF(self);
1987 return (PyObject*) self;
1988 }
1989
1990 fill = width - PyString_GET_SIZE(self);
1991
1992 u = pad(self, fill, 0, '0');
1993 if (u == NULL)
1994 return NULL;
1995
1996 str = PyString_AS_STRING(u);
1997 if (str[fill] == '+' || str[fill] == '-') {
1998 /* move sign to beginning of string */
1999 str[0] = str[fill];
2000 str[fill] = '0';
2001 }
2002
2003 return u;
2004}
2005#endif
2006
2007static char isspace__doc__[] =
2008"S.isspace() -> int\n\
2009\n\
2010Return 1 if there are only whitespace characters in S,\n\
20110 otherwise.";
2012
2013static PyObject*
2014string_isspace(PyStringObject *self, PyObject *args)
2015{
Fred Drakeba096332000-07-09 07:04:36 +00002016 register const unsigned char *p
2017 = (unsigned char *) PyString_AS_STRING(self);
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002018 register const unsigned char *e;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002019
2020 if (!PyArg_NoArgs(args))
2021 return NULL;
2022
2023 /* Shortcut for single character strings */
2024 if (PyString_GET_SIZE(self) == 1 &&
2025 isspace(*p))
2026 return PyInt_FromLong(1);
2027
Marc-André Lemburg60bc8092000-06-14 09:18:32 +00002028 /* Special case for empty strings */
2029 if (PyString_GET_SIZE(self) == 0)
2030 return PyInt_FromLong(0);
2031
Guido van Rossum4c08d552000-03-10 22:55:18 +00002032 e = p + PyString_GET_SIZE(self);
2033 for (; p < e; p++) {
2034 if (!isspace(*p))
2035 return PyInt_FromLong(0);
2036 }
2037 return PyInt_FromLong(1);
2038}
2039
2040
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002041static char isalpha__doc__[] =
2042"S.isalpha() -> int\n\
2043\n\
2044Return 1 if all characters in S are alphabetic\n\
2045and there is at least one character in S, 0 otherwise.";
2046
2047static PyObject*
Fred Drake49312a52000-12-06 14:27:49 +00002048string_isalpha(PyStringObject *self, PyObject *args)
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002049{
Fred Drakeba096332000-07-09 07:04:36 +00002050 register const unsigned char *p
2051 = (unsigned char *) PyString_AS_STRING(self);
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002052 register const unsigned char *e;
2053
2054 if (!PyArg_NoArgs(args))
2055 return NULL;
2056
2057 /* Shortcut for single character strings */
2058 if (PyString_GET_SIZE(self) == 1 &&
2059 isalpha(*p))
2060 return PyInt_FromLong(1);
2061
2062 /* Special case for empty strings */
2063 if (PyString_GET_SIZE(self) == 0)
2064 return PyInt_FromLong(0);
2065
2066 e = p + PyString_GET_SIZE(self);
2067 for (; p < e; p++) {
2068 if (!isalpha(*p))
2069 return PyInt_FromLong(0);
2070 }
2071 return PyInt_FromLong(1);
2072}
2073
2074
2075static char isalnum__doc__[] =
2076"S.isalnum() -> int\n\
2077\n\
2078Return 1 if all characters in S are alphanumeric\n\
2079and there is at least one character in S, 0 otherwise.";
2080
2081static PyObject*
Fred Drake49312a52000-12-06 14:27:49 +00002082string_isalnum(PyStringObject *self, PyObject *args)
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002083{
Fred Drakeba096332000-07-09 07:04:36 +00002084 register const unsigned char *p
2085 = (unsigned char *) PyString_AS_STRING(self);
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002086 register const unsigned char *e;
2087
2088 if (!PyArg_NoArgs(args))
2089 return NULL;
2090
2091 /* Shortcut for single character strings */
2092 if (PyString_GET_SIZE(self) == 1 &&
2093 isalnum(*p))
2094 return PyInt_FromLong(1);
2095
2096 /* Special case for empty strings */
2097 if (PyString_GET_SIZE(self) == 0)
2098 return PyInt_FromLong(0);
2099
2100 e = p + PyString_GET_SIZE(self);
2101 for (; p < e; p++) {
2102 if (!isalnum(*p))
2103 return PyInt_FromLong(0);
2104 }
2105 return PyInt_FromLong(1);
2106}
2107
2108
Guido van Rossum4c08d552000-03-10 22:55:18 +00002109static char isdigit__doc__[] =
2110"S.isdigit() -> int\n\
2111\n\
2112Return 1 if there are only digit characters in S,\n\
21130 otherwise.";
2114
2115static PyObject*
2116string_isdigit(PyStringObject *self, PyObject *args)
2117{
Fred Drakeba096332000-07-09 07:04:36 +00002118 register const unsigned char *p
2119 = (unsigned char *) PyString_AS_STRING(self);
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002120 register const unsigned char *e;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002121
2122 if (!PyArg_NoArgs(args))
2123 return NULL;
2124
2125 /* Shortcut for single character strings */
2126 if (PyString_GET_SIZE(self) == 1 &&
2127 isdigit(*p))
2128 return PyInt_FromLong(1);
2129
Marc-André Lemburg60bc8092000-06-14 09:18:32 +00002130 /* Special case for empty strings */
2131 if (PyString_GET_SIZE(self) == 0)
2132 return PyInt_FromLong(0);
2133
Guido van Rossum4c08d552000-03-10 22:55:18 +00002134 e = p + PyString_GET_SIZE(self);
2135 for (; p < e; p++) {
2136 if (!isdigit(*p))
2137 return PyInt_FromLong(0);
2138 }
2139 return PyInt_FromLong(1);
2140}
2141
2142
2143static char islower__doc__[] =
2144"S.islower() -> int\n\
2145\n\
2146Return 1 if all cased characters in S are lowercase and there is\n\
2147at least one cased character in S, 0 otherwise.";
2148
2149static PyObject*
2150string_islower(PyStringObject *self, PyObject *args)
2151{
Fred Drakeba096332000-07-09 07:04:36 +00002152 register const unsigned char *p
2153 = (unsigned char *) PyString_AS_STRING(self);
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002154 register const unsigned char *e;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002155 int cased;
2156
2157 if (!PyArg_NoArgs(args))
2158 return NULL;
2159
2160 /* Shortcut for single character strings */
2161 if (PyString_GET_SIZE(self) == 1)
2162 return PyInt_FromLong(islower(*p) != 0);
2163
Marc-André Lemburg60bc8092000-06-14 09:18:32 +00002164 /* Special case for empty strings */
2165 if (PyString_GET_SIZE(self) == 0)
2166 return PyInt_FromLong(0);
2167
Guido van Rossum4c08d552000-03-10 22:55:18 +00002168 e = p + PyString_GET_SIZE(self);
2169 cased = 0;
2170 for (; p < e; p++) {
2171 if (isupper(*p))
2172 return PyInt_FromLong(0);
2173 else if (!cased && islower(*p))
2174 cased = 1;
2175 }
2176 return PyInt_FromLong(cased);
2177}
2178
2179
2180static char isupper__doc__[] =
2181"S.isupper() -> int\n\
2182\n\
2183Return 1 if all cased characters in S are uppercase and there is\n\
2184at least one cased character in S, 0 otherwise.";
2185
2186static PyObject*
2187string_isupper(PyStringObject *self, PyObject *args)
2188{
Fred Drakeba096332000-07-09 07:04:36 +00002189 register const unsigned char *p
2190 = (unsigned char *) PyString_AS_STRING(self);
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002191 register const unsigned char *e;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002192 int cased;
2193
2194 if (!PyArg_NoArgs(args))
2195 return NULL;
2196
2197 /* Shortcut for single character strings */
2198 if (PyString_GET_SIZE(self) == 1)
2199 return PyInt_FromLong(isupper(*p) != 0);
2200
Marc-André Lemburg60bc8092000-06-14 09:18:32 +00002201 /* Special case for empty strings */
2202 if (PyString_GET_SIZE(self) == 0)
2203 return PyInt_FromLong(0);
2204
Guido van Rossum4c08d552000-03-10 22:55:18 +00002205 e = p + PyString_GET_SIZE(self);
2206 cased = 0;
2207 for (; p < e; p++) {
2208 if (islower(*p))
2209 return PyInt_FromLong(0);
2210 else if (!cased && isupper(*p))
2211 cased = 1;
2212 }
2213 return PyInt_FromLong(cased);
2214}
2215
2216
2217static char istitle__doc__[] =
2218"S.istitle() -> int\n\
2219\n\
2220Return 1 if S is a titlecased string, i.e. uppercase characters\n\
2221may only follow uncased characters and lowercase characters only cased\n\
2222ones. Return 0 otherwise.";
2223
2224static PyObject*
2225string_istitle(PyStringObject *self, PyObject *args)
2226{
Fred Drakeba096332000-07-09 07:04:36 +00002227 register const unsigned char *p
2228 = (unsigned char *) PyString_AS_STRING(self);
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002229 register const unsigned char *e;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002230 int cased, previous_is_cased;
2231
2232 if (!PyArg_NoArgs(args))
2233 return NULL;
2234
2235 /* Shortcut for single character strings */
2236 if (PyString_GET_SIZE(self) == 1)
2237 return PyInt_FromLong(isupper(*p) != 0);
2238
Marc-André Lemburg60bc8092000-06-14 09:18:32 +00002239 /* Special case for empty strings */
2240 if (PyString_GET_SIZE(self) == 0)
2241 return PyInt_FromLong(0);
2242
Guido van Rossum4c08d552000-03-10 22:55:18 +00002243 e = p + PyString_GET_SIZE(self);
2244 cased = 0;
2245 previous_is_cased = 0;
2246 for (; p < e; p++) {
Guido van Rossumb8f820c2000-05-05 20:44:24 +00002247 register const unsigned char ch = *p;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002248
2249 if (isupper(ch)) {
2250 if (previous_is_cased)
2251 return PyInt_FromLong(0);
2252 previous_is_cased = 1;
2253 cased = 1;
2254 }
2255 else if (islower(ch)) {
2256 if (!previous_is_cased)
2257 return PyInt_FromLong(0);
2258 previous_is_cased = 1;
2259 cased = 1;
2260 }
2261 else
2262 previous_is_cased = 0;
2263 }
2264 return PyInt_FromLong(cased);
2265}
2266
2267
2268static char splitlines__doc__[] =
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002269"S.splitlines([keepends]]) -> list of strings\n\
Guido van Rossum4c08d552000-03-10 22:55:18 +00002270\n\
2271Return a list of the lines in S, breaking at line boundaries.\n\
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002272Line breaks are not included in the resulting list unless keepends\n\
2273is given and true.";
Guido van Rossum4c08d552000-03-10 22:55:18 +00002274
2275#define SPLIT_APPEND(data, left, right) \
2276 str = PyString_FromStringAndSize(data + left, right - left); \
2277 if (!str) \
2278 goto onError; \
2279 if (PyList_Append(list, str)) { \
2280 Py_DECREF(str); \
2281 goto onError; \
2282 } \
2283 else \
2284 Py_DECREF(str);
2285
2286static PyObject*
2287string_splitlines(PyStringObject *self, PyObject *args)
2288{
Guido van Rossum4c08d552000-03-10 22:55:18 +00002289 register int i;
2290 register int j;
2291 int len;
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002292 int keepends = 0;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002293 PyObject *list;
2294 PyObject *str;
2295 char *data;
2296
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002297 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
Guido van Rossum4c08d552000-03-10 22:55:18 +00002298 return NULL;
2299
2300 data = PyString_AS_STRING(self);
2301 len = PyString_GET_SIZE(self);
2302
Guido van Rossum4c08d552000-03-10 22:55:18 +00002303 list = PyList_New(0);
2304 if (!list)
2305 goto onError;
2306
2307 for (i = j = 0; i < len; ) {
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002308 int eol;
2309
Guido van Rossum4c08d552000-03-10 22:55:18 +00002310 /* Find a line and append it */
2311 while (i < len && data[i] != '\n' && data[i] != '\r')
2312 i++;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002313
2314 /* Skip the line break reading CRLF as one line break */
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002315 eol = i;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002316 if (i < len) {
2317 if (data[i] == '\r' && i + 1 < len &&
2318 data[i+1] == '\n')
2319 i += 2;
2320 else
2321 i++;
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002322 if (keepends)
2323 eol = i;
Guido van Rossum4c08d552000-03-10 22:55:18 +00002324 }
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002325 SPLIT_APPEND(data, j, eol);
Guido van Rossum4c08d552000-03-10 22:55:18 +00002326 j = i;
2327 }
2328 if (j < len) {
2329 SPLIT_APPEND(data, j, len);
2330 }
2331
2332 return list;
2333
2334 onError:
2335 Py_DECREF(list);
2336 return NULL;
2337}
2338
2339#undef SPLIT_APPEND
2340
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002341
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002342static PyMethodDef
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002343string_methods[] = {
Guido van Rossum4c08d552000-03-10 22:55:18 +00002344 /* Counterparts of the obsolete stropmodule functions; except
2345 string.maketrans(). */
2346 {"join", (PyCFunction)string_join, 1, join__doc__},
2347 {"split", (PyCFunction)string_split, 1, split__doc__},
2348 {"lower", (PyCFunction)string_lower, 1, lower__doc__},
2349 {"upper", (PyCFunction)string_upper, 1, upper__doc__},
2350 {"islower", (PyCFunction)string_islower, 0, islower__doc__},
2351 {"isupper", (PyCFunction)string_isupper, 0, isupper__doc__},
2352 {"isspace", (PyCFunction)string_isspace, 0, isspace__doc__},
2353 {"isdigit", (PyCFunction)string_isdigit, 0, isdigit__doc__},
2354 {"istitle", (PyCFunction)string_istitle, 0, istitle__doc__},
Marc-André Lemburg4027f8f2000-07-05 09:47:46 +00002355 {"isalpha", (PyCFunction)string_isalpha, 0, isalpha__doc__},
2356 {"isalnum", (PyCFunction)string_isalnum, 0, isalnum__doc__},
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002357 {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__},
2358 {"count", (PyCFunction)string_count, 1, count__doc__},
2359 {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__},
2360 {"find", (PyCFunction)string_find, 1, find__doc__},
2361 {"index", (PyCFunction)string_index, 1, index__doc__},
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002362 {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__},
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002363 {"replace", (PyCFunction)string_replace, 1, replace__doc__},
2364 {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__},
2365 {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__},
2366 {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__},
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002367 {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__},
2368 {"strip", (PyCFunction)string_strip, 1, strip__doc__},
2369 {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__},
Guido van Rossum4c08d552000-03-10 22:55:18 +00002370 {"translate", (PyCFunction)string_translate, 1, translate__doc__},
2371 {"title", (PyCFunction)string_title, 1, title__doc__},
2372 {"ljust", (PyCFunction)string_ljust, 1, ljust__doc__},
2373 {"rjust", (PyCFunction)string_rjust, 1, rjust__doc__},
2374 {"center", (PyCFunction)string_center, 1, center__doc__},
Marc-André Lemburg63f3d172000-07-06 11:29:01 +00002375 {"encode", (PyCFunction)string_encode, 1, encode__doc__},
Guido van Rossum4c08d552000-03-10 22:55:18 +00002376 {"expandtabs", (PyCFunction)string_expandtabs, 1, expandtabs__doc__},
2377 {"splitlines", (PyCFunction)string_splitlines, 1, splitlines__doc__},
2378#if 0
2379 {"zfill", (PyCFunction)string_zfill, 1, zfill__doc__},
2380#endif
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002381 {NULL, NULL} /* sentinel */
2382};
2383
2384static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00002385string_getattr(PyStringObject *s, char *name)
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002386{
2387 return Py_FindMethod(string_methods, (PyObject*)s, name);
2388}
2389
2390
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002391PyTypeObject PyString_Type = {
2392 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002393 0,
2394 "string",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002395 sizeof(PyStringObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002396 sizeof(char),
Guido van Rossum013142a1994-08-30 08:19:36 +00002397 (destructor)string_dealloc, /*tp_dealloc*/
2398 (printfunc)string_print, /*tp_print*/
Barry Warsaw226ae6c1999-10-12 19:54:53 +00002399 (getattrfunc)string_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002400 0, /*tp_setattr*/
Guido van Rossum013142a1994-08-30 08:19:36 +00002401 (cmpfunc)string_compare, /*tp_compare*/
2402 (reprfunc)string_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002403 0, /*tp_as_number*/
2404 &string_as_sequence, /*tp_as_sequence*/
2405 0, /*tp_as_mapping*/
Guido van Rossum013142a1994-08-30 08:19:36 +00002406 (hashfunc)string_hash, /*tp_hash*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00002407 0, /*tp_call*/
Guido van Rossum189f1df2001-05-01 16:51:53 +00002408 (reprfunc)string_str, /*tp_str*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00002409 0, /*tp_getattro*/
2410 0, /*tp_setattro*/
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00002411 &string_as_buffer, /*tp_as_buffer*/
Guido van Rossum1db70701998-10-08 02:18:52 +00002412 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum2a61e741997-01-18 07:55:05 +00002413 0, /*tp_doc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002414};
2415
2416void
Fred Drakeba096332000-07-09 07:04:36 +00002417PyString_Concat(register PyObject **pv, register PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002418{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002419 register PyObject *v;
Guido van Rossum013142a1994-08-30 08:19:36 +00002420 if (*pv == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002421 return;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002422 if (w == NULL || !PyString_Check(*pv)) {
2423 Py_DECREF(*pv);
Guido van Rossum013142a1994-08-30 08:19:36 +00002424 *pv = NULL;
2425 return;
2426 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002427 v = string_concat((PyStringObject *) *pv, w);
2428 Py_DECREF(*pv);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002429 *pv = v;
2430}
2431
Guido van Rossum013142a1994-08-30 08:19:36 +00002432void
Fred Drakeba096332000-07-09 07:04:36 +00002433PyString_ConcatAndDel(register PyObject **pv, register PyObject *w)
Guido van Rossum013142a1994-08-30 08:19:36 +00002434{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002435 PyString_Concat(pv, w);
2436 Py_XDECREF(w);
Guido van Rossum013142a1994-08-30 08:19:36 +00002437}
2438
2439
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002440/* The following function breaks the notion that strings are immutable:
2441 it changes the size of a string. We get away with this only if there
2442 is only one module referencing the object. You can also think of it
2443 as creating a new string object and destroying the old one, only
2444 more efficiently. In any case, don't use this if the string may
2445 already be known to some other part of the code... */
2446
2447int
Fred Drakeba096332000-07-09 07:04:36 +00002448_PyString_Resize(PyObject **pv, int newsize)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002449{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002450 register PyObject *v;
2451 register PyStringObject *sv;
Guido van Rossum921842f1990-11-18 17:30:23 +00002452 v = *pv;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002453 if (!PyString_Check(v) || v->ob_refcnt != 1) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002454 *pv = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002455 Py_DECREF(v);
2456 PyErr_BadInternalCall();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00002457 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002458 }
Guido van Rossum921842f1990-11-18 17:30:23 +00002459 /* XXX UNREF/NEWREF interface should be more symmetrical */
Guido van Rossum441e4ab1996-05-23 22:46:51 +00002460#ifdef Py_REF_DEBUG
Guido van Rossum6f9e4331995-03-29 16:57:48 +00002461 --_Py_RefTotal;
Guido van Rossum921842f1990-11-18 17:30:23 +00002462#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002463 _Py_ForgetReference(v);
2464 *pv = (PyObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +00002465 PyObject_REALLOC((char *)v,
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002466 sizeof(PyStringObject) + newsize * sizeof(char));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467 if (*pv == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00002468 PyObject_DEL(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002469 PyErr_NoMemory();
Guido van Rossum2a9096b1990-10-21 22:15:08 +00002470 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002471 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002472 _Py_NewReference(*pv);
2473 sv = (PyStringObject *) *pv;
Guido van Rossum921842f1990-11-18 17:30:23 +00002474 sv->ob_size = newsize;
2475 sv->ob_sval[newsize] = '\0';
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002476 return 0;
2477}
Guido van Rossume5372401993-03-16 12:15:04 +00002478
2479/* Helpers for formatstring */
2480
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002481static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00002482getnextarg(PyObject *args, int arglen, int *p_argidx)
Guido van Rossume5372401993-03-16 12:15:04 +00002483{
2484 int argidx = *p_argidx;
2485 if (argidx < arglen) {
2486 (*p_argidx)++;
2487 if (arglen < 0)
2488 return args;
2489 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002490 return PyTuple_GetItem(args, argidx);
Guido van Rossume5372401993-03-16 12:15:04 +00002491 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002492 PyErr_SetString(PyExc_TypeError,
2493 "not enough arguments for format string");
Guido van Rossume5372401993-03-16 12:15:04 +00002494 return NULL;
2495}
2496
Tim Peters38fd5b62000-09-21 05:43:11 +00002497/* Format codes
2498 * F_LJUST '-'
2499 * F_SIGN '+'
2500 * F_BLANK ' '
2501 * F_ALT '#'
2502 * F_ZERO '0'
2503 */
Guido van Rossume5372401993-03-16 12:15:04 +00002504#define F_LJUST (1<<0)
2505#define F_SIGN (1<<1)
2506#define F_BLANK (1<<2)
2507#define F_ALT (1<<3)
2508#define F_ZERO (1<<4)
2509
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002510static int
Fred Drakeba096332000-07-09 07:04:36 +00002511formatfloat(char *buf, size_t buflen, int flags,
2512 int prec, int type, PyObject *v)
Guido van Rossume5372401993-03-16 12:15:04 +00002513{
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002514 /* fmt = '%#.' + `prec` + `type`
2515 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
Guido van Rossume5372401993-03-16 12:15:04 +00002516 char fmt[20];
Guido van Rossume5372401993-03-16 12:15:04 +00002517 double x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002518 if (!PyArg_Parse(v, "d;float argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002519 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00002520 if (prec < 0)
2521 prec = 6;
Guido van Rossume5372401993-03-16 12:15:04 +00002522 if (type == 'f' && fabs(x)/1e25 >= 1e25)
2523 type = 'g';
2524 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002525 /* worst case length calc to ensure no buffer overrun:
2526 fmt = %#.<prec>g
2527 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002528 for any double rep.)
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002529 len = 1 + prec + 1 + 2 + 5 = 9 + prec
2530 If prec=0 the effective precision is 1 (the leading digit is
2531 always given), therefore increase by one to 10+prec. */
2532 if (buflen <= (size_t)10 + (size_t)prec) {
2533 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00002534 "formatted float is too long (precision too large?)");
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002535 return -1;
2536 }
Guido van Rossume5372401993-03-16 12:15:04 +00002537 sprintf(buf, fmt, x);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002538 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00002539}
2540
Tim Peters38fd5b62000-09-21 05:43:11 +00002541/* _PyString_FormatLong emulates the format codes d, u, o, x and X, and
2542 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for
2543 * Python's regular ints.
2544 * Return value: a new PyString*, or NULL if error.
2545 * . *pbuf is set to point into it,
2546 * *plen set to the # of chars following that.
2547 * Caller must decref it when done using pbuf.
2548 * The string starting at *pbuf is of the form
2549 * "-"? ("0x" | "0X")? digit+
2550 * "0x"/"0X" are present only for x and X conversions, with F_ALT
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002551 * set in flags. The case of hex digits will be correct,
Tim Peters38fd5b62000-09-21 05:43:11 +00002552 * There will be at least prec digits, zero-filled on the left if
2553 * necessary to get that many.
2554 * val object to be converted
2555 * flags bitmask of format flags; only F_ALT is looked at
2556 * prec minimum number of digits; 0-fill on left if needed
2557 * type a character in [duoxX]; u acts the same as d
2558 *
2559 * CAUTION: o, x and X conversions on regular ints can never
2560 * produce a '-' sign, but can for Python's unbounded ints.
2561 */
2562PyObject*
2563_PyString_FormatLong(PyObject *val, int flags, int prec, int type,
2564 char **pbuf, int *plen)
2565{
2566 PyObject *result = NULL;
2567 char *buf;
2568 int i;
2569 int sign; /* 1 if '-', else 0 */
2570 int len; /* number of characters */
2571 int numdigits; /* len == numnondigits + numdigits */
2572 int numnondigits = 0;
2573
2574 switch (type) {
2575 case 'd':
2576 case 'u':
2577 result = val->ob_type->tp_str(val);
2578 break;
2579 case 'o':
2580 result = val->ob_type->tp_as_number->nb_oct(val);
2581 break;
2582 case 'x':
2583 case 'X':
2584 numnondigits = 2;
2585 result = val->ob_type->tp_as_number->nb_hex(val);
2586 break;
2587 default:
2588 assert(!"'type' not in [duoxX]");
2589 }
2590 if (!result)
2591 return NULL;
2592
2593 /* To modify the string in-place, there can only be one reference. */
2594 if (result->ob_refcnt != 1) {
2595 PyErr_BadInternalCall();
2596 return NULL;
2597 }
2598 buf = PyString_AsString(result);
2599 len = PyString_Size(result);
2600 if (buf[len-1] == 'L') {
2601 --len;
2602 buf[len] = '\0';
2603 }
2604 sign = buf[0] == '-';
2605 numnondigits += sign;
2606 numdigits = len - numnondigits;
2607 assert(numdigits > 0);
2608
Tim Petersfff53252001-04-12 18:38:48 +00002609 /* Get rid of base marker unless F_ALT */
2610 if ((flags & F_ALT) == 0) {
Tim Peters38fd5b62000-09-21 05:43:11 +00002611 /* Need to skip 0x, 0X or 0. */
2612 int skipped = 0;
2613 switch (type) {
2614 case 'o':
2615 assert(buf[sign] == '0');
2616 /* If 0 is only digit, leave it alone. */
2617 if (numdigits > 1) {
2618 skipped = 1;
2619 --numdigits;
2620 }
2621 break;
2622 case 'x':
2623 case 'X':
2624 assert(buf[sign] == '0');
2625 assert(buf[sign + 1] == 'x');
2626 skipped = 2;
2627 numnondigits -= 2;
2628 break;
2629 }
2630 if (skipped) {
2631 buf += skipped;
2632 len -= skipped;
2633 if (sign)
2634 buf[0] = '-';
2635 }
2636 assert(len == numnondigits + numdigits);
2637 assert(numdigits > 0);
2638 }
2639
2640 /* Fill with leading zeroes to meet minimum width. */
2641 if (prec > numdigits) {
2642 PyObject *r1 = PyString_FromStringAndSize(NULL,
2643 numnondigits + prec);
2644 char *b1;
2645 if (!r1) {
2646 Py_DECREF(result);
2647 return NULL;
2648 }
2649 b1 = PyString_AS_STRING(r1);
2650 for (i = 0; i < numnondigits; ++i)
2651 *b1++ = *buf++;
2652 for (i = 0; i < prec - numdigits; i++)
2653 *b1++ = '0';
2654 for (i = 0; i < numdigits; i++)
2655 *b1++ = *buf++;
2656 *b1 = '\0';
2657 Py_DECREF(result);
2658 result = r1;
2659 buf = PyString_AS_STRING(result);
2660 len = numnondigits + prec;
2661 }
2662
2663 /* Fix up case for hex conversions. */
2664 switch (type) {
2665 case 'x':
2666 /* Need to convert all upper case letters to lower case. */
2667 for (i = 0; i < len; i++)
2668 if (buf[i] >= 'A' && buf[i] <= 'F')
2669 buf[i] += 'a'-'A';
2670 break;
2671 case 'X':
2672 /* Need to convert 0x to 0X (and -0x to -0X). */
2673 if (buf[sign + 1] == 'x')
2674 buf[sign + 1] = 'X';
2675 break;
2676 }
2677 *pbuf = buf;
2678 *plen = len;
2679 return result;
2680}
2681
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002682static int
Fred Drakeba096332000-07-09 07:04:36 +00002683formatint(char *buf, size_t buflen, int flags,
2684 int prec, int type, PyObject *v)
Guido van Rossume5372401993-03-16 12:15:04 +00002685{
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002686 /* fmt = '%#.' + `prec` + 'l' + `type`
Tim Peters38fd5b62000-09-21 05:43:11 +00002687 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
2688 + 1 + 1 = 24 */
2689 char fmt[64]; /* plenty big enough! */
Guido van Rossume5372401993-03-16 12:15:04 +00002690 long x;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002691 if (!PyArg_Parse(v, "l;int argument required", &x))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002692 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00002693 if (prec < 0)
2694 prec = 1;
2695 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
Tim Peters38fd5b62000-09-21 05:43:11 +00002696 /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal))
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002697 worst case buf = '0x' + [0-9]*prec, where prec >= 11 */
Tim Peters38fd5b62000-09-21 05:43:11 +00002698 if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) {
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002699 PyErr_SetString(PyExc_OverflowError,
Fred Drake661ea262000-10-24 19:57:45 +00002700 "formatted integer is too long (precision too large?)");
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002701 return -1;
2702 }
Guido van Rossume5372401993-03-16 12:15:04 +00002703 sprintf(buf, fmt, x);
Tim Petersfff53252001-04-12 18:38:48 +00002704 /* When converting 0 under %#x or %#X, C leaves off the base marker,
2705 * but we want it (for consistency with other %#x conversions, and
2706 * for consistency with Python's hex() function).
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002707 * BUG 28-Apr-2001 tim: At least two platform Cs (Metrowerks &
2708 * Compaq Tru64) violate the std by converting 0 w/ leading 0x anyway.
2709 * So add it only if the platform didn't already.
Tim Petersfff53252001-04-12 18:38:48 +00002710 */
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002711 if (x == 0 &&
2712 (flags & F_ALT) &&
2713 (type == 'x' || type == 'X') &&
2714 buf[1] != (char)type) /* this last always true under std C */
2715 {
Tim Petersfff53252001-04-12 18:38:48 +00002716 memmove(buf+2, buf, strlen(buf) + 1);
2717 buf[0] = '0';
2718 buf[1] = (char)type;
2719 }
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002720 return strlen(buf);
Guido van Rossume5372401993-03-16 12:15:04 +00002721}
2722
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002723static int
Fred Drakeba096332000-07-09 07:04:36 +00002724formatchar(char *buf, size_t buflen, PyObject *v)
Guido van Rossume5372401993-03-16 12:15:04 +00002725{
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002726 /* presume that the buffer is at least 2 characters long */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002727 if (PyString_Check(v)) {
2728 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002729 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00002730 }
2731 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002732 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002733 return -1;
Guido van Rossume5372401993-03-16 12:15:04 +00002734 }
2735 buf[1] = '\0';
Guido van Rossuma04d47b1997-01-21 16:12:09 +00002736 return 1;
Guido van Rossume5372401993-03-16 12:15:04 +00002737}
2738
Guido van Rossum013142a1994-08-30 08:19:36 +00002739
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002740/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
2741
2742 FORMATBUFLEN is the length of the buffer in which the floats, ints, &
2743 chars are formatted. XXX This is a magic number. Each formatting
2744 routine does bounds checking to ensure no overflow, but a better
2745 solution may be to malloc a buffer of appropriate size for each
2746 format. For now, the current solution is sufficient.
2747*/
2748#define FORMATBUFLEN (size_t)120
Guido van Rossume5372401993-03-16 12:15:04 +00002749
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002750PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00002751PyString_Format(PyObject *format, PyObject *args)
Guido van Rossume5372401993-03-16 12:15:04 +00002752{
2753 char *fmt, *res;
2754 int fmtcnt, rescnt, reslen, arglen, argidx;
Guido van Rossum993952b1996-05-21 22:44:20 +00002755 int args_owned = 0;
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00002756 PyObject *result, *orig_args, *v, *w;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002757 PyObject *dict = NULL;
2758 if (format == NULL || !PyString_Check(format) || args == NULL) {
2759 PyErr_BadInternalCall();
Guido van Rossume5372401993-03-16 12:15:04 +00002760 return NULL;
2761 }
Guido van Rossum90daa872000-04-10 13:47:21 +00002762 orig_args = args;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002763 fmt = PyString_AsString(format);
2764 fmtcnt = PyString_Size(format);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00002765 reslen = rescnt = fmtcnt + 100;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002766 result = PyString_FromStringAndSize((char *)NULL, reslen);
Guido van Rossume5372401993-03-16 12:15:04 +00002767 if (result == NULL)
2768 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002769 res = PyString_AsString(result);
2770 if (PyTuple_Check(args)) {
2771 arglen = PyTuple_Size(args);
Guido van Rossume5372401993-03-16 12:15:04 +00002772 argidx = 0;
2773 }
2774 else {
2775 arglen = -1;
2776 argidx = -2;
2777 }
Guido van Rossum013142a1994-08-30 08:19:36 +00002778 if (args->ob_type->tp_as_mapping)
2779 dict = args;
Guido van Rossume5372401993-03-16 12:15:04 +00002780 while (--fmtcnt >= 0) {
2781 if (*fmt != '%') {
2782 if (--rescnt < 0) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00002783 rescnt = fmtcnt + 100;
2784 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002785 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00002786 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002787 res = PyString_AsString(result)
2788 + reslen - rescnt;
Guido van Rossum013142a1994-08-30 08:19:36 +00002789 --rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00002790 }
2791 *res++ = *fmt++;
2792 }
2793 else {
2794 /* Got a format specifier */
2795 int flags = 0;
Guido van Rossume5372401993-03-16 12:15:04 +00002796 int width = -1;
2797 int prec = -1;
Guido van Rossum6938a291993-11-11 14:51:57 +00002798 int c = '\0';
Guido van Rossume5372401993-03-16 12:15:04 +00002799 int fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002800 PyObject *v = NULL;
2801 PyObject *temp = NULL;
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002802 char *pbuf;
Guido van Rossume5372401993-03-16 12:15:04 +00002803 int sign;
2804 int len;
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002805 char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */
Guido van Rossum90daa872000-04-10 13:47:21 +00002806 char *fmt_start = fmt;
Marc-André Lemburg542fe562001-05-02 14:21:53 +00002807 int argidx_start = argidx;
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002808
Guido van Rossumda9c2711996-12-05 21:58:58 +00002809 fmt++;
Guido van Rossum013142a1994-08-30 08:19:36 +00002810 if (*fmt == '(') {
2811 char *keystart;
2812 int keylen;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002813 PyObject *key;
Guido van Rossum045e6881997-09-08 18:30:11 +00002814 int pcount = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00002815
2816 if (dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002817 PyErr_SetString(PyExc_TypeError,
Tim Petersb3d8d1f2001-04-28 05:38:26 +00002818 "format requires a mapping");
Guido van Rossum013142a1994-08-30 08:19:36 +00002819 goto error;
2820 }
2821 ++fmt;
2822 --fmtcnt;
2823 keystart = fmt;
Guido van Rossum045e6881997-09-08 18:30:11 +00002824 /* Skip over balanced parentheses */
2825 while (pcount > 0 && --fmtcnt >= 0) {
2826 if (*fmt == ')')
2827 --pcount;
2828 else if (*fmt == '(')
2829 ++pcount;
Guido van Rossum013142a1994-08-30 08:19:36 +00002830 fmt++;
Guido van Rossum045e6881997-09-08 18:30:11 +00002831 }
2832 keylen = fmt - keystart - 1;
2833 if (fmtcnt < 0 || pcount > 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002834 PyErr_SetString(PyExc_ValueError,
Guido van Rossum013142a1994-08-30 08:19:36 +00002835 "incomplete format key");
2836 goto error;
2837 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002838 key = PyString_FromStringAndSize(keystart,
2839 keylen);
Guido van Rossum013142a1994-08-30 08:19:36 +00002840 if (key == NULL)
2841 goto error;
Guido van Rossum993952b1996-05-21 22:44:20 +00002842 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002843 Py_DECREF(args);
Guido van Rossum993952b1996-05-21 22:44:20 +00002844 args_owned = 0;
2845 }
2846 args = PyObject_GetItem(dict, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002847 Py_DECREF(key);
Guido van Rossum013142a1994-08-30 08:19:36 +00002848 if (args == NULL) {
2849 goto error;
2850 }
Guido van Rossum993952b1996-05-21 22:44:20 +00002851 args_owned = 1;
Guido van Rossum013142a1994-08-30 08:19:36 +00002852 arglen = -1;
2853 argidx = -2;
2854 }
Guido van Rossume5372401993-03-16 12:15:04 +00002855 while (--fmtcnt >= 0) {
2856 switch (c = *fmt++) {
2857 case '-': flags |= F_LJUST; continue;
2858 case '+': flags |= F_SIGN; continue;
2859 case ' ': flags |= F_BLANK; continue;
2860 case '#': flags |= F_ALT; continue;
2861 case '0': flags |= F_ZERO; continue;
2862 }
2863 break;
2864 }
2865 if (c == '*') {
2866 v = getnextarg(args, arglen, &argidx);
2867 if (v == NULL)
2868 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002869 if (!PyInt_Check(v)) {
2870 PyErr_SetString(PyExc_TypeError,
2871 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00002872 goto error;
2873 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002874 width = PyInt_AsLong(v);
Guido van Rossum98c9eba1999-06-07 15:12:32 +00002875 if (width < 0) {
2876 flags |= F_LJUST;
2877 width = -width;
2878 }
Guido van Rossume5372401993-03-16 12:15:04 +00002879 if (--fmtcnt >= 0)
2880 c = *fmt++;
2881 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002882 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00002883 width = c - '0';
2884 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002885 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00002886 if (!isdigit(c))
2887 break;
2888 if ((width*10) / 10 != width) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002889 PyErr_SetString(
2890 PyExc_ValueError,
2891 "width too big");
Guido van Rossume5372401993-03-16 12:15:04 +00002892 goto error;
2893 }
2894 width = width*10 + (c - '0');
2895 }
2896 }
2897 if (c == '.') {
2898 prec = 0;
2899 if (--fmtcnt >= 0)
2900 c = *fmt++;
2901 if (c == '*') {
2902 v = getnextarg(args, arglen, &argidx);
2903 if (v == NULL)
2904 goto error;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002905 if (!PyInt_Check(v)) {
2906 PyErr_SetString(
2907 PyExc_TypeError,
2908 "* wants int");
Guido van Rossume5372401993-03-16 12:15:04 +00002909 goto error;
2910 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002911 prec = PyInt_AsLong(v);
Guido van Rossume5372401993-03-16 12:15:04 +00002912 if (prec < 0)
2913 prec = 0;
2914 if (--fmtcnt >= 0)
2915 c = *fmt++;
2916 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002917 else if (c >= 0 && isdigit(c)) {
Guido van Rossume5372401993-03-16 12:15:04 +00002918 prec = c - '0';
2919 while (--fmtcnt >= 0) {
Guido van Rossum9fa2c111995-02-10 17:00:37 +00002920 c = Py_CHARMASK(*fmt++);
Guido van Rossume5372401993-03-16 12:15:04 +00002921 if (!isdigit(c))
2922 break;
2923 if ((prec*10) / 10 != prec) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002924 PyErr_SetString(
2925 PyExc_ValueError,
Guido van Rossume5372401993-03-16 12:15:04 +00002926 "prec too big");
2927 goto error;
2928 }
2929 prec = prec*10 + (c - '0');
2930 }
2931 }
2932 } /* prec */
2933 if (fmtcnt >= 0) {
2934 if (c == 'h' || c == 'l' || c == 'L') {
Guido van Rossume5372401993-03-16 12:15:04 +00002935 if (--fmtcnt >= 0)
2936 c = *fmt++;
2937 }
2938 }
2939 if (fmtcnt < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002940 PyErr_SetString(PyExc_ValueError,
2941 "incomplete format");
Guido van Rossume5372401993-03-16 12:15:04 +00002942 goto error;
2943 }
2944 if (c != '%') {
2945 v = getnextarg(args, arglen, &argidx);
2946 if (v == NULL)
2947 goto error;
2948 }
2949 sign = 0;
2950 fill = ' ';
2951 switch (c) {
2952 case '%':
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002953 pbuf = "%";
Guido van Rossume5372401993-03-16 12:15:04 +00002954 len = 1;
2955 break;
2956 case 's':
Guido van Rossum90daa872000-04-10 13:47:21 +00002957 case 'r':
2958 if (PyUnicode_Check(v)) {
2959 fmt = fmt_start;
Marc-André Lemburg542fe562001-05-02 14:21:53 +00002960 argidx = argidx_start;
Guido van Rossum90daa872000-04-10 13:47:21 +00002961 goto unicode;
2962 }
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002963 if (c == 's')
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002964 temp = PyObject_Str(v);
Guido van Rossumf0b7b042000-04-11 15:39:26 +00002965 else
2966 temp = PyObject_Repr(v);
Guido van Rossum013142a1994-08-30 08:19:36 +00002967 if (temp == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +00002968 goto error;
Guido van Rossum4a0144c1998-06-09 15:08:41 +00002969 if (!PyString_Check(temp)) {
2970 PyErr_SetString(PyExc_TypeError,
2971 "%s argument has non-string str()");
2972 goto error;
2973 }
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00002974 pbuf = PyString_AsString(temp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002975 len = PyString_Size(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00002976 if (prec >= 0 && len > prec)
2977 len = prec;
2978 break;
2979 case 'i':
2980 case 'd':
2981 case 'u':
2982 case 'o':
2983 case 'x':
2984 case 'X':
2985 if (c == 'i')
2986 c = 'd';
Tim Petersa3a3a032000-11-30 05:22:44 +00002987 if (PyLong_Check(v)) {
Tim Peters38fd5b62000-09-21 05:43:11 +00002988 temp = _PyString_FormatLong(v, flags,
2989 prec, c, &pbuf, &len);
2990 if (!temp)
2991 goto error;
2992 /* unbounded ints can always produce
2993 a sign character! */
2994 sign = 1;
Guido van Rossum4acdc231997-01-29 06:00:24 +00002995 }
Tim Peters38fd5b62000-09-21 05:43:11 +00002996 else {
2997 pbuf = formatbuf;
2998 len = formatint(pbuf, sizeof(formatbuf),
2999 flags, prec, c, v);
3000 if (len < 0)
3001 goto error;
3002 /* only d conversion is signed */
3003 sign = c == 'd';
3004 }
3005 if (flags & F_ZERO)
3006 fill = '0';
Guido van Rossume5372401993-03-16 12:15:04 +00003007 break;
3008 case 'e':
3009 case 'E':
3010 case 'f':
3011 case 'g':
3012 case 'G':
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00003013 pbuf = formatbuf;
3014 len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00003015 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00003016 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00003017 sign = 1;
Tim Peters38fd5b62000-09-21 05:43:11 +00003018 if (flags & F_ZERO)
Guido van Rossume5372401993-03-16 12:15:04 +00003019 fill = '0';
3020 break;
3021 case 'c':
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00003022 pbuf = formatbuf;
3023 len = formatchar(pbuf, sizeof(formatbuf), v);
Guido van Rossuma04d47b1997-01-21 16:12:09 +00003024 if (len < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00003025 goto error;
Guido van Rossume5372401993-03-16 12:15:04 +00003026 break;
3027 default:
Guido van Rossum045e6881997-09-08 18:30:11 +00003028 PyErr_Format(PyExc_ValueError,
Andrew M. Kuchling6ca89172000-12-15 13:07:46 +00003029 "unsupported format character '%c' (0x%x) "
3030 "at index %i",
3031 c, c, fmt - 1 - PyString_AsString(format));
Guido van Rossume5372401993-03-16 12:15:04 +00003032 goto error;
3033 }
3034 if (sign) {
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00003035 if (*pbuf == '-' || *pbuf == '+') {
3036 sign = *pbuf++;
Guido van Rossume5372401993-03-16 12:15:04 +00003037 len--;
3038 }
3039 else if (flags & F_SIGN)
3040 sign = '+';
3041 else if (flags & F_BLANK)
3042 sign = ' ';
3043 else
Tim Peters38fd5b62000-09-21 05:43:11 +00003044 sign = 0;
Guido van Rossume5372401993-03-16 12:15:04 +00003045 }
3046 if (width < len)
3047 width = len;
Tim Peters38fd5b62000-09-21 05:43:11 +00003048 if (rescnt < width + (sign != 0)) {
Guido van Rossum6ac258d1993-05-12 08:24:20 +00003049 reslen -= rescnt;
3050 rescnt = width + fmtcnt + 100;
3051 reslen += rescnt;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003052 if (_PyString_Resize(&result, reslen) < 0)
Guido van Rossume5372401993-03-16 12:15:04 +00003053 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003054 res = PyString_AsString(result)
3055 + reslen - rescnt;
Guido van Rossume5372401993-03-16 12:15:04 +00003056 }
3057 if (sign) {
Guido van Rossum71e57d01993-11-11 15:03:51 +00003058 if (fill != ' ')
3059 *res++ = sign;
Guido van Rossume5372401993-03-16 12:15:04 +00003060 rescnt--;
3061 if (width > len)
3062 width--;
3063 }
Tim Peters38fd5b62000-09-21 05:43:11 +00003064 if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
3065 assert(pbuf[0] == '0');
Tim Petersfff53252001-04-12 18:38:48 +00003066 assert(pbuf[1] == c);
3067 if (fill != ' ') {
3068 *res++ = *pbuf++;
3069 *res++ = *pbuf++;
Tim Peters38fd5b62000-09-21 05:43:11 +00003070 }
Tim Petersfff53252001-04-12 18:38:48 +00003071 rescnt -= 2;
3072 width -= 2;
3073 if (width < 0)
3074 width = 0;
3075 len -= 2;
Tim Peters38fd5b62000-09-21 05:43:11 +00003076 }
3077 if (width > len && !(flags & F_LJUST)) {
Guido van Rossume5372401993-03-16 12:15:04 +00003078 do {
3079 --rescnt;
3080 *res++ = fill;
3081 } while (--width > len);
3082 }
Tim Peters38fd5b62000-09-21 05:43:11 +00003083 if (fill == ' ') {
3084 if (sign)
3085 *res++ = sign;
3086 if ((flags & F_ALT) &&
Tim Petersfff53252001-04-12 18:38:48 +00003087 (c == 'x' || c == 'X')) {
3088 assert(pbuf[0] == '0');
3089 assert(pbuf[1] == c);
Tim Peters38fd5b62000-09-21 05:43:11 +00003090 *res++ = *pbuf++;
3091 *res++ = *pbuf++;
3092 }
3093 }
Marc-André Lemburgf28dd832000-06-30 10:29:57 +00003094 memcpy(res, pbuf, len);
Guido van Rossume5372401993-03-16 12:15:04 +00003095 res += len;
3096 rescnt -= len;
3097 while (--width >= len) {
3098 --rescnt;
3099 *res++ = ' ';
3100 }
Guido van Rossum9fa2c111995-02-10 17:00:37 +00003101 if (dict && (argidx < arglen) && c != '%') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003102 PyErr_SetString(PyExc_TypeError,
Guido van Rossum013142a1994-08-30 08:19:36 +00003103 "not all arguments converted");
3104 goto error;
3105 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003106 Py_XDECREF(temp);
Guido van Rossume5372401993-03-16 12:15:04 +00003107 } /* '%' */
3108 } /* until end */
Guido van Rossumcaeaafc1995-02-27 10:13:23 +00003109 if (argidx < arglen && !dict) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003110 PyErr_SetString(PyExc_TypeError,
3111 "not all arguments converted");
Guido van Rossume5372401993-03-16 12:15:04 +00003112 goto error;
3113 }
Guido van Rossum1109fbc1998-04-10 22:16:39 +00003114 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003115 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00003116 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003117 _PyString_Resize(&result, reslen - rescnt);
Guido van Rossume5372401993-03-16 12:15:04 +00003118 return result;
Guido van Rossum90daa872000-04-10 13:47:21 +00003119
3120 unicode:
3121 if (args_owned) {
3122 Py_DECREF(args);
3123 args_owned = 0;
3124 }
Marc-André Lemburg542fe562001-05-02 14:21:53 +00003125 /* Fiddle args right (remove the first argidx arguments) */
Guido van Rossum90daa872000-04-10 13:47:21 +00003126 if (PyTuple_Check(orig_args) && argidx > 0) {
3127 PyObject *v;
3128 int n = PyTuple_GET_SIZE(orig_args) - argidx;
3129 v = PyTuple_New(n);
3130 if (v == NULL)
3131 goto error;
3132 while (--n >= 0) {
3133 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx);
3134 Py_INCREF(w);
3135 PyTuple_SET_ITEM(v, n, w);
3136 }
3137 args = v;
3138 } else {
3139 Py_INCREF(orig_args);
3140 args = orig_args;
3141 }
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003142 args_owned = 1;
3143 /* Take what we have of the result and let the Unicode formatting
3144 function format the rest of the input. */
Guido van Rossum90daa872000-04-10 13:47:21 +00003145 rescnt = res - PyString_AS_STRING(result);
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003146 if (_PyString_Resize(&result, rescnt))
3147 goto error;
Guido van Rossum90daa872000-04-10 13:47:21 +00003148 fmtcnt = PyString_GET_SIZE(format) - \
3149 (fmt - PyString_AS_STRING(format));
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003150 format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL);
3151 if (format == NULL)
Guido van Rossum90daa872000-04-10 13:47:21 +00003152 goto error;
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003153 v = PyUnicode_Format(format, args);
Guido van Rossum90daa872000-04-10 13:47:21 +00003154 Py_DECREF(format);
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003155 if (v == NULL)
3156 goto error;
3157 /* Paste what we have (result) to what the Unicode formatting
3158 function returned (v) and return the result (or error) */
3159 w = PyUnicode_Concat(result, v);
3160 Py_DECREF(result);
3161 Py_DECREF(v);
Guido van Rossum90daa872000-04-10 13:47:21 +00003162 Py_DECREF(args);
Marc-André Lemburg53f3d4a2000-10-07 08:54:09 +00003163 return w;
Tim Petersb3d8d1f2001-04-28 05:38:26 +00003164
Guido van Rossume5372401993-03-16 12:15:04 +00003165 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003166 Py_DECREF(result);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00003167 if (args_owned) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003168 Py_DECREF(args);
Guido van Rossum1109fbc1998-04-10 22:16:39 +00003169 }
Guido van Rossume5372401993-03-16 12:15:04 +00003170 return NULL;
3171}
Guido van Rossum2a61e741997-01-18 07:55:05 +00003172
3173
3174#ifdef INTERN_STRINGS
3175
Barry Warsaw4df762f2000-08-16 23:41:01 +00003176/* This dictionary will leak at PyString_Fini() time. That's acceptable
3177 * because PyString_Fini() specifically frees interned strings that are
3178 * only referenced by this dictionary. The CVS log entry for revision 2.45
3179 * says:
3180 *
3181 * Change the Fini function to only remove otherwise unreferenced
3182 * strings from the interned table. There are references in
3183 * hard-to-find static variables all over the interpreter, and it's not
3184 * worth trying to get rid of all those; but "uninterning" isn't fair
3185 * either and may cause subtle failures later -- so we have to keep them
3186 * in the interned table.
3187 */
Guido van Rossum2a61e741997-01-18 07:55:05 +00003188static PyObject *interned;
3189
3190void
Fred Drakeba096332000-07-09 07:04:36 +00003191PyString_InternInPlace(PyObject **p)
Guido van Rossum2a61e741997-01-18 07:55:05 +00003192{
3193 register PyStringObject *s = (PyStringObject *)(*p);
3194 PyObject *t;
3195 if (s == NULL || !PyString_Check(s))
3196 Py_FatalError("PyString_InternInPlace: strings only please!");
3197 if ((t = s->ob_sinterned) != NULL) {
3198 if (t == (PyObject *)s)
3199 return;
3200 Py_INCREF(t);
3201 *p = t;
3202 Py_DECREF(s);
3203 return;
3204 }
3205 if (interned == NULL) {
3206 interned = PyDict_New();
3207 if (interned == NULL)
3208 return;
Guido van Rossum2a61e741997-01-18 07:55:05 +00003209 }
3210 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
3211 Py_INCREF(t);
3212 *p = s->ob_sinterned = t;
3213 Py_DECREF(s);
3214 return;
3215 }
3216 t = (PyObject *)s;
3217 if (PyDict_SetItem(interned, t, t) == 0) {
3218 s->ob_sinterned = t;
3219 return;
3220 }
3221 PyErr_Clear();
3222}
3223
3224
3225PyObject *
Fred Drakeba096332000-07-09 07:04:36 +00003226PyString_InternFromString(const char *cp)
Guido van Rossum2a61e741997-01-18 07:55:05 +00003227{
3228 PyObject *s = PyString_FromString(cp);
3229 if (s == NULL)
3230 return NULL;
3231 PyString_InternInPlace(&s);
3232 return s;
3233}
3234
3235#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00003236
3237void
Fred Drakeba096332000-07-09 07:04:36 +00003238PyString_Fini(void)
Guido van Rossum8cf04761997-08-02 02:57:45 +00003239{
3240 int i;
Guido van Rossum8cf04761997-08-02 02:57:45 +00003241 for (i = 0; i < UCHAR_MAX + 1; i++) {
3242 Py_XDECREF(characters[i]);
3243 characters[i] = NULL;
3244 }
3245#ifndef DONT_SHARE_SHORT_STRINGS
3246 Py_XDECREF(nullstring);
3247 nullstring = NULL;
3248#endif
Guido van Rossum971a7aa1997-08-05 02:15:12 +00003249#ifdef INTERN_STRINGS
3250 if (interned) {
3251 int pos, changed;
3252 PyObject *key, *value;
3253 do {
3254 changed = 0;
3255 pos = 0;
3256 while (PyDict_Next(interned, &pos, &key, &value)) {
3257 if (key->ob_refcnt == 2 && key == value) {
3258 PyDict_DelItem(interned, key);
3259 changed = 1;
3260 }
3261 }
3262 } while (changed);
3263 }
3264#endif
Guido van Rossum8cf04761997-08-02 02:57:45 +00003265}
Barry Warsawa903ad982001-02-23 16:40:48 +00003266
3267#ifdef INTERN_STRINGS
3268void _Py_ReleaseInternedStrings(void)
3269{
3270 if (interned) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003271 fprintf(stderr, "releasing interned strings\n");
3272 PyDict_Clear(interned);
Barry Warsawa903ad982001-02-23 16:40:48 +00003273 Py_DECREF(interned);
3274 interned = NULL;
3275 }
3276}
3277#endif /* INTERN_STRINGS */