blob: 7e62af575fe88fdaead1983d163e64be6a84bc52 [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/*
2 string_format.h -- implementation of string.format().
3
4 It uses the Objects/stringlib conventions, so that it can be
5 compiled for both unicode and string objects.
6*/
7
8
Eric Smith8fd3eba2008-02-17 19:48:00 +00009/* Defines for Python 2.6 compatability */
10#if PY_VERSION_HEX < 0x03000000
11#define PyLong_FromSsize_t _PyLong_FromSsize_t
12#endif
13
Eric Smith8c663262007-08-25 02:26:07 +000014/* Defines for more efficiently reallocating the string buffer */
15#define INITIAL_SIZE_INCREMENT 100
16#define SIZE_MULTIPLIER 2
17#define MAX_SIZE_INCREMENT 3200
18
19
20/************************************************************************/
21/*********** Global data structures and forward declarations *********/
22/************************************************************************/
23
24/*
25 A SubString consists of the characters between two string or
26 unicode pointers.
27*/
28typedef struct {
29 STRINGLIB_CHAR *ptr;
30 STRINGLIB_CHAR *end;
31} SubString;
32
33
34/* forward declaration for recursion */
35static PyObject *
36build_string(SubString *input, PyObject *args, PyObject *kwargs,
Eric Smith45c07872007-09-05 02:02:43 +000037 int recursion_depth);
Eric Smith8c663262007-08-25 02:26:07 +000038
39
40
41/************************************************************************/
42/************************** Utility functions ************************/
43/************************************************************************/
44
45/* fill in a SubString from a pointer and length */
46Py_LOCAL_INLINE(void)
47SubString_init(SubString *str, STRINGLIB_CHAR *p, Py_ssize_t len)
48{
49 str->ptr = p;
50 if (p == NULL)
51 str->end = NULL;
52 else
53 str->end = str->ptr + len;
54}
55
Eric Smith625cbf22007-08-29 03:22:59 +000056/* return a new string. if str->ptr is NULL, return None */
Eric Smith8c663262007-08-25 02:26:07 +000057Py_LOCAL_INLINE(PyObject *)
58SubString_new_object(SubString *str)
59{
Eric Smith625cbf22007-08-29 03:22:59 +000060 if (str->ptr == NULL) {
61 Py_INCREF(Py_None);
62 return Py_None;
63 }
64 return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
65}
66
67/* return a new string. if str->ptr is NULL, return None */
68Py_LOCAL_INLINE(PyObject *)
69SubString_new_object_or_empty(SubString *str)
70{
71 if (str->ptr == NULL) {
72 return STRINGLIB_NEW(NULL, 0);
73 }
Eric Smith8c663262007-08-25 02:26:07 +000074 return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
75}
76
77/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +000078/*********** Output string management functions ****************/
79/************************************************************************/
80
81typedef struct {
82 STRINGLIB_CHAR *ptr;
83 STRINGLIB_CHAR *end;
84 PyObject *obj;
85 Py_ssize_t size_increment;
86} OutputString;
87
88/* initialize an OutputString object, reserving size characters */
89static int
90output_initialize(OutputString *output, Py_ssize_t size)
91{
92 output->obj = STRINGLIB_NEW(NULL, size);
93 if (output->obj == NULL)
94 return 0;
95
96 output->ptr = STRINGLIB_STR(output->obj);
97 output->end = STRINGLIB_LEN(output->obj) + output->ptr;
98 output->size_increment = INITIAL_SIZE_INCREMENT;
99
100 return 1;
101}
102
103/*
104 output_extend reallocates the output string buffer.
105 It returns a status: 0 for a failed reallocation,
106 1 for success.
107*/
108
109static int
110output_extend(OutputString *output, Py_ssize_t count)
111{
112 STRINGLIB_CHAR *startptr = STRINGLIB_STR(output->obj);
113 Py_ssize_t curlen = output->ptr - startptr;
114 Py_ssize_t maxlen = curlen + count + output->size_increment;
115
116 if (STRINGLIB_RESIZE(&output->obj, maxlen) < 0)
117 return 0;
118 startptr = STRINGLIB_STR(output->obj);
119 output->ptr = startptr + curlen;
120 output->end = startptr + maxlen;
121 if (output->size_increment < MAX_SIZE_INCREMENT)
122 output->size_increment *= SIZE_MULTIPLIER;
123 return 1;
124}
125
126/*
127 output_data dumps characters into our output string
128 buffer.
129
130 In some cases, it has to reallocate the string.
131
132 It returns a status: 0 for a failed reallocation,
133 1 for success.
134*/
135static int
136output_data(OutputString *output, const STRINGLIB_CHAR *s, Py_ssize_t count)
137{
138 if ((count > output->end - output->ptr) && !output_extend(output, count))
139 return 0;
140 memcpy(output->ptr, s, count * sizeof(STRINGLIB_CHAR));
141 output->ptr += count;
142 return 1;
143}
144
145/************************************************************************/
146/*********** Format string parsing -- integers and identifiers *********/
147/************************************************************************/
148
Eric Smith7ade6482007-08-26 22:27:13 +0000149static Py_ssize_t
150get_integer(const SubString *str)
Eric Smith8c663262007-08-25 02:26:07 +0000151{
Eric Smith7ade6482007-08-26 22:27:13 +0000152 Py_ssize_t accumulator = 0;
153 Py_ssize_t digitval;
154 Py_ssize_t oldaccumulator;
155 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000156
Eric Smith7ade6482007-08-26 22:27:13 +0000157 /* empty string is an error */
158 if (str->ptr >= str->end)
159 return -1;
Eric Smith8c663262007-08-25 02:26:07 +0000160
Eric Smith7ade6482007-08-26 22:27:13 +0000161 for (p = str->ptr; p < str->end; p++) {
162 digitval = STRINGLIB_TODECIMAL(*p);
Eric Smith8c663262007-08-25 02:26:07 +0000163 if (digitval < 0)
Eric Smith7ade6482007-08-26 22:27:13 +0000164 return -1;
Eric Smith8c663262007-08-25 02:26:07 +0000165 /*
166 This trick was copied from old Unicode format code. It's cute,
167 but would really suck on an old machine with a slow divide
168 implementation. Fortunately, in the normal case we do not
169 expect too many digits.
170 */
171 oldaccumulator = accumulator;
172 accumulator *= 10;
173 if ((accumulator+10)/10 != oldaccumulator+1) {
174 PyErr_Format(PyExc_ValueError,
175 "Too many decimal digits in format string");
176 return -1;
177 }
178 accumulator += digitval;
179 }
Eric Smith7ade6482007-08-26 22:27:13 +0000180 return accumulator;
Eric Smith8c663262007-08-25 02:26:07 +0000181}
182
183/************************************************************************/
184/******** Functions to get field objects and specification strings ******/
185/************************************************************************/
186
Eric Smith7ade6482007-08-26 22:27:13 +0000187/* do the equivalent of obj.name */
Eric Smith8c663262007-08-25 02:26:07 +0000188static PyObject *
Eric Smith7ade6482007-08-26 22:27:13 +0000189getattr(PyObject *obj, SubString *name)
Eric Smith8c663262007-08-25 02:26:07 +0000190{
Eric Smith7ade6482007-08-26 22:27:13 +0000191 PyObject *newobj;
Eric Smith7a6dd292007-08-27 23:30:47 +0000192 PyObject *str = SubString_new_object(name);
Eric Smith7ade6482007-08-26 22:27:13 +0000193 if (str == NULL)
194 return NULL;
195 newobj = PyObject_GetAttr(obj, str);
196 Py_DECREF(str);
197 return newobj;
Eric Smith8c663262007-08-25 02:26:07 +0000198}
199
Eric Smith7ade6482007-08-26 22:27:13 +0000200/* do the equivalent of obj[idx], where obj is a sequence */
201static PyObject *
202getitem_sequence(PyObject *obj, Py_ssize_t idx)
203{
204 return PySequence_GetItem(obj, idx);
205}
206
207/* do the equivalent of obj[idx], where obj is not a sequence */
208static PyObject *
209getitem_idx(PyObject *obj, Py_ssize_t idx)
210{
211 PyObject *newobj;
Christian Heimes217cfd12007-12-02 14:31:20 +0000212 PyObject *idx_obj = PyLong_FromSsize_t(idx);
Eric Smith7ade6482007-08-26 22:27:13 +0000213 if (idx_obj == NULL)
214 return NULL;
215 newobj = PyObject_GetItem(obj, idx_obj);
216 Py_DECREF(idx_obj);
217 return newobj;
218}
219
220/* do the equivalent of obj[name] */
221static PyObject *
222getitem_str(PyObject *obj, SubString *name)
223{
224 PyObject *newobj;
Eric Smith7a6dd292007-08-27 23:30:47 +0000225 PyObject *str = SubString_new_object(name);
Eric Smith7ade6482007-08-26 22:27:13 +0000226 if (str == NULL)
227 return NULL;
228 newobj = PyObject_GetItem(obj, str);
229 Py_DECREF(str);
230 return newobj;
231}
232
233typedef struct {
234 /* the entire string we're parsing. we assume that someone else
235 is managing its lifetime, and that it will exist for the
236 lifetime of the iterator. can be empty */
237 SubString str;
238
239 /* pointer to where we are inside field_name */
240 STRINGLIB_CHAR *ptr;
241} FieldNameIterator;
242
243
244static int
245FieldNameIterator_init(FieldNameIterator *self, STRINGLIB_CHAR *ptr,
246 Py_ssize_t len)
247{
248 SubString_init(&self->str, ptr, len);
249 self->ptr = self->str.ptr;
250 return 1;
251}
252
253static int
254_FieldNameIterator_attr(FieldNameIterator *self, SubString *name)
255{
256 STRINGLIB_CHAR c;
257
258 name->ptr = self->ptr;
259
260 /* return everything until '.' or '[' */
261 while (self->ptr < self->str.end) {
262 switch (c = *self->ptr++) {
263 case '[':
264 case '.':
265 /* backup so that we this character will be seen next time */
266 self->ptr--;
267 break;
268 default:
269 continue;
270 }
271 break;
272 }
273 /* end of string is okay */
274 name->end = self->ptr;
275 return 1;
276}
277
278static int
279_FieldNameIterator_item(FieldNameIterator *self, SubString *name)
280{
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000281 int bracket_seen = 0;
Eric Smith7ade6482007-08-26 22:27:13 +0000282 STRINGLIB_CHAR c;
283
284 name->ptr = self->ptr;
285
286 /* return everything until ']' */
287 while (self->ptr < self->str.end) {
288 switch (c = *self->ptr++) {
289 case ']':
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000290 bracket_seen = 1;
Eric Smith7ade6482007-08-26 22:27:13 +0000291 break;
292 default:
293 continue;
294 }
295 break;
296 }
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000297 /* make sure we ended with a ']' */
298 if (!bracket_seen) {
299 PyErr_SetString(PyExc_ValueError, "Missing ']' in format string");
300 return 0;
301 }
302
Eric Smith7ade6482007-08-26 22:27:13 +0000303 /* end of string is okay */
304 /* don't include the ']' */
305 name->end = self->ptr-1;
306 return 1;
307}
308
309/* returns 0 on error, 1 on non-error termination, and 2 if it returns a value */
310static int
311FieldNameIterator_next(FieldNameIterator *self, int *is_attribute,
312 Py_ssize_t *name_idx, SubString *name)
313{
314 /* check at end of input */
315 if (self->ptr >= self->str.end)
316 return 1;
317
318 switch (*self->ptr++) {
319 case '.':
320 *is_attribute = 1;
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000321 if (_FieldNameIterator_attr(self, name) == 0)
Eric Smith7ade6482007-08-26 22:27:13 +0000322 return 0;
Eric Smith7ade6482007-08-26 22:27:13 +0000323 *name_idx = -1;
324 break;
325 case '[':
326 *is_attribute = 0;
Eric Smith4cb4e4e2007-09-03 08:40:29 +0000327 if (_FieldNameIterator_item(self, name) == 0)
Eric Smith7ade6482007-08-26 22:27:13 +0000328 return 0;
Eric Smith7ade6482007-08-26 22:27:13 +0000329 *name_idx = get_integer(name);
330 break;
331 default:
332 /* interal error, can't get here */
333 assert(0);
334 return 0;
335 }
336
337 /* empty string is an error */
338 if (name->ptr == name->end) {
339 PyErr_SetString(PyExc_ValueError, "Empty attribute in format string");
340 return 0;
341 }
342
343 return 2;
344}
345
346
347/* input: field_name
348 output: 'first' points to the part before the first '[' or '.'
349 'first_idx' is -1 if 'first' is not an integer, otherwise
350 it's the value of first converted to an integer
351 'rest' is an iterator to return the rest
352*/
353static int
354field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
355 Py_ssize_t *first_idx, FieldNameIterator *rest)
356{
357 STRINGLIB_CHAR c;
358 STRINGLIB_CHAR *p = ptr;
359 STRINGLIB_CHAR *end = ptr + len;
360
361 /* find the part up until the first '.' or '[' */
362 while (p < end) {
363 switch (c = *p++) {
364 case '[':
365 case '.':
366 /* backup so that we this character is available to the
367 "rest" iterator */
368 p--;
369 break;
370 default:
371 continue;
372 }
373 break;
374 }
375
376 /* set up the return values */
377 SubString_init(first, ptr, p - ptr);
378 FieldNameIterator_init(rest, p, end - p);
379
380 /* see if "first" is an integer, in which case it's used as an index */
381 *first_idx = get_integer(first);
382
383 /* zero length string is an error */
384 if (first->ptr >= first->end) {
385 PyErr_SetString(PyExc_ValueError, "empty field name");
386 goto error;
387 }
388
389 return 1;
390error:
391 return 0;
392}
393
394
Eric Smith8c663262007-08-25 02:26:07 +0000395/*
396 get_field_object returns the object inside {}, before the
397 format_spec. It handles getindex and getattr lookups and consumes
398 the entire input string.
399*/
400static PyObject *
401get_field_object(SubString *input, PyObject *args, PyObject *kwargs)
402{
Eric Smith7ade6482007-08-26 22:27:13 +0000403 PyObject *obj = NULL;
404 int ok;
405 int is_attribute;
406 SubString name;
407 SubString first;
Eric Smith8c663262007-08-25 02:26:07 +0000408 Py_ssize_t index;
Eric Smith7ade6482007-08-26 22:27:13 +0000409 FieldNameIterator rest;
Eric Smith8c663262007-08-25 02:26:07 +0000410
Eric Smith7ade6482007-08-26 22:27:13 +0000411 if (!field_name_split(input->ptr, input->end - input->ptr, &first,
412 &index, &rest)) {
413 goto error;
414 }
Eric Smith8c663262007-08-25 02:26:07 +0000415
Eric Smith7ade6482007-08-26 22:27:13 +0000416 if (index == -1) {
417 /* look up in kwargs */
Eric Smith7a6dd292007-08-27 23:30:47 +0000418 PyObject *key = SubString_new_object(&first);
Eric Smith7ade6482007-08-26 22:27:13 +0000419 if (key == NULL)
420 goto error;
421 if ((kwargs == NULL) || (obj = PyDict_GetItem(kwargs, key)) == NULL) {
Eric Smith11529192007-09-04 23:04:22 +0000422 PyErr_SetObject(PyExc_KeyError, key);
Eric Smith7ade6482007-08-26 22:27:13 +0000423 Py_DECREF(key);
424 goto error;
Eric Smith8c663262007-08-25 02:26:07 +0000425 }
Neal Norwitz8a4eb292007-08-27 07:24:17 +0000426 Py_DECREF(key);
Neal Norwitz247b5152007-08-27 03:22:50 +0000427 Py_INCREF(obj);
Eric Smith0cb431c2007-08-28 01:07:27 +0000428 }
429 else {
Eric Smith7ade6482007-08-26 22:27:13 +0000430 /* look up in args */
431 obj = PySequence_GetItem(args, index);
Eric Smith11529192007-09-04 23:04:22 +0000432 if (obj == NULL)
Eric Smith7ade6482007-08-26 22:27:13 +0000433 goto error;
Eric Smith8c663262007-08-25 02:26:07 +0000434 }
Eric Smith7ade6482007-08-26 22:27:13 +0000435
436 /* iterate over the rest of the field_name */
437 while ((ok = FieldNameIterator_next(&rest, &is_attribute, &index,
438 &name)) == 2) {
439 PyObject *tmp;
440
441 if (is_attribute)
442 /* getattr lookup "." */
443 tmp = getattr(obj, &name);
444 else
445 /* getitem lookup "[]" */
446 if (index == -1)
447 tmp = getitem_str(obj, &name);
448 else
449 if (PySequence_Check(obj))
450 tmp = getitem_sequence(obj, index);
451 else
452 /* not a sequence */
453 tmp = getitem_idx(obj, index);
454 if (tmp == NULL)
455 goto error;
456
457 /* assign to obj */
458 Py_DECREF(obj);
459 obj = tmp;
Eric Smith8c663262007-08-25 02:26:07 +0000460 }
Eric Smith7ade6482007-08-26 22:27:13 +0000461 /* end of iterator, this is the non-error case */
462 if (ok == 1)
463 return obj;
464error:
465 Py_XDECREF(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000466 return NULL;
467}
468
469/************************************************************************/
470/***************** Field rendering functions **************************/
471/************************************************************************/
472
473/*
474 render_field() is the main function in this section. It takes the
475 field object and field specification string generated by
476 get_field_and_spec, and renders the field into the output string.
477
Eric Smith8c663262007-08-25 02:26:07 +0000478 render_field calls fieldobj.__format__(format_spec) method, and
479 appends to the output.
480*/
481static int
482render_field(PyObject *fieldobj, SubString *format_spec, OutputString *output)
483{
484 int ok = 0;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000485 PyObject *result = NULL;
Eric Smith1d138f12008-05-31 01:40:08 +0000486 PyObject *format_spec_object = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000487
Eric Smith1d138f12008-05-31 01:40:08 +0000488 STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
489 format_spec->ptr : NULL;
490 Py_ssize_t format_spec_len = format_spec->ptr ?
491 format_spec->end - format_spec->ptr : 0;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000492
Eric Smith1d138f12008-05-31 01:40:08 +0000493 /* If we know the type exactly, skip the lookup of __format__ and just
494 call the formatter directly. */
495 if (PyUnicode_CheckExact(fieldobj))
496 result = _PyUnicode_FormatAdvanced(fieldobj, format_spec_start,
497 format_spec_len);
498 else if (PyLong_CheckExact(fieldobj))
499 result = _PyLong_FormatAdvanced(fieldobj, format_spec_start,
500 format_spec_len);
501 else if (PyFloat_CheckExact(fieldobj))
502 result = _PyFloat_FormatAdvanced(fieldobj, format_spec_start,
503 format_spec_len);
504 else {
505 /* We need to create an object out of the pointers we have, because
506 __format__ takes a string/unicode object for format_spec. */
507 format_spec_object = STRINGLIB_NEW(format_spec_start,
508 format_spec_len);
509 if (format_spec_object == NULL)
510 goto done;
511
512 result = PyObject_Format(fieldobj, format_spec_object);
513 }
Eric Smith8c663262007-08-25 02:26:07 +0000514 if (result == NULL)
515 goto done;
516
Eric Smith8a0217c2008-02-18 18:07:47 +0000517#if PY_VERSION_HEX >= 0x03000000
Eric Smithecbac8f2008-02-24 21:44:34 +0000518 assert(PyUnicode_Check(result));
Eric Smith8a0217c2008-02-18 18:07:47 +0000519#else
Christian Heimes72b710a2008-05-26 13:28:38 +0000520 assert(PyBytes_Check(result) || PyUnicode_Check(result));
Eric Smith8a0217c2008-02-18 18:07:47 +0000521
522 /* Convert result to our type. We could be str, and result could
523 be unicode */
524 {
525 PyObject *tmp = STRINGLIB_TOSTR(result);
526 if (tmp == NULL)
527 goto done;
528 Py_DECREF(result);
529 result = tmp;
530 }
531#endif
532
Eric Smith8c663262007-08-25 02:26:07 +0000533 ok = output_data(output,
534 STRINGLIB_STR(result), STRINGLIB_LEN(result));
535done:
Eric Smith1d138f12008-05-31 01:40:08 +0000536 Py_XDECREF(format_spec_object);
Eric Smith8c663262007-08-25 02:26:07 +0000537 Py_XDECREF(result);
538 return ok;
539}
540
541static int
542parse_field(SubString *str, SubString *field_name, SubString *format_spec,
543 STRINGLIB_CHAR *conversion)
544{
545 STRINGLIB_CHAR c = 0;
546
547 /* initialize these, as they may be empty */
548 *conversion = '\0';
549 SubString_init(format_spec, NULL, 0);
550
551 /* search for the field name. it's terminated by the end of the
552 string, or a ':' or '!' */
553 field_name->ptr = str->ptr;
554 while (str->ptr < str->end) {
555 switch (c = *(str->ptr++)) {
556 case ':':
557 case '!':
558 break;
559 default:
560 continue;
561 }
562 break;
563 }
564
565 if (c == '!' || c == ':') {
566 /* we have a format specifier and/or a conversion */
567 /* don't include the last character */
568 field_name->end = str->ptr-1;
569
570 /* the format specifier is the rest of the string */
571 format_spec->ptr = str->ptr;
572 format_spec->end = str->end;
573
574 /* see if there's a conversion specifier */
575 if (c == '!') {
576 /* there must be another character present */
577 if (format_spec->ptr >= format_spec->end) {
578 PyErr_SetString(PyExc_ValueError,
579 "end of format while looking for conversion "
580 "specifier");
581 return 0;
582 }
583 *conversion = *(format_spec->ptr++);
584
585 /* if there is another character, it must be a colon */
586 if (format_spec->ptr < format_spec->end) {
587 c = *(format_spec->ptr++);
588 if (c != ':') {
589 PyErr_SetString(PyExc_ValueError,
590 "expected ':' after format specifier");
591 return 0;
592 }
593 }
594 }
595
596 return 1;
597
Eric Smith0cb431c2007-08-28 01:07:27 +0000598 }
599 else {
Eric Smith8c663262007-08-25 02:26:07 +0000600 /* end of string, there's no format_spec or conversion */
601 field_name->end = str->ptr;
602 return 1;
603 }
604}
605
606/************************************************************************/
607/******* Output string allocation and escape-to-markup processing ******/
608/************************************************************************/
609
610/* MarkupIterator breaks the string into pieces of either literal
611 text, or things inside {} that need to be marked up. it is
612 designed to make it easy to wrap a Python iterator around it, for
613 use with the Formatter class */
614
615typedef struct {
616 SubString str;
Eric Smith8c663262007-08-25 02:26:07 +0000617} MarkupIterator;
618
619static int
620MarkupIterator_init(MarkupIterator *self, STRINGLIB_CHAR *ptr, Py_ssize_t len)
621{
622 SubString_init(&self->str, ptr, len);
Eric Smith8c663262007-08-25 02:26:07 +0000623 return 1;
624}
625
626/* returns 0 on error, 1 on non-error termination, and 2 if it got a
627 string (or something to be expanded) */
628static int
Eric Smith625cbf22007-08-29 03:22:59 +0000629MarkupIterator_next(MarkupIterator *self, SubString *literal,
Eric Smith8c663262007-08-25 02:26:07 +0000630 SubString *field_name, SubString *format_spec,
631 STRINGLIB_CHAR *conversion,
632 int *format_spec_needs_expanding)
633{
634 int at_end;
635 STRINGLIB_CHAR c = 0;
636 STRINGLIB_CHAR *start;
637 int count;
638 Py_ssize_t len;
Eric Smith625cbf22007-08-29 03:22:59 +0000639 int markup_follows = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000640
Eric Smith625cbf22007-08-29 03:22:59 +0000641 /* initialize all of the output variables */
642 SubString_init(literal, NULL, 0);
643 SubString_init(field_name, NULL, 0);
644 SubString_init(format_spec, NULL, 0);
645 *conversion = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000646 *format_spec_needs_expanding = 0;
647
Eric Smith625cbf22007-08-29 03:22:59 +0000648 /* No more input, end of iterator. This is the normal exit
649 path. */
Eric Smith8c663262007-08-25 02:26:07 +0000650 if (self->str.ptr >= self->str.end)
651 return 1;
652
Eric Smith8c663262007-08-25 02:26:07 +0000653 start = self->str.ptr;
654
Eric Smith625cbf22007-08-29 03:22:59 +0000655 /* First read any literal text. Read until the end of string, an
656 escaped '{' or '}', or an unescaped '{'. In order to never
657 allocate memory and so I can just pass pointers around, if
658 there's an escaped '{' or '}' then we'll return the literal
659 including the brace, but no format object. The next time
660 through, we'll return the rest of the literal, skipping past
661 the second consecutive brace. */
662 while (self->str.ptr < self->str.end) {
663 switch (c = *(self->str.ptr++)) {
664 case '{':
665 case '}':
666 markup_follows = 1;
667 break;
668 default:
669 continue;
Eric Smith8c663262007-08-25 02:26:07 +0000670 }
Eric Smith625cbf22007-08-29 03:22:59 +0000671 break;
Eric Smith0cb431c2007-08-28 01:07:27 +0000672 }
Eric Smith625cbf22007-08-29 03:22:59 +0000673
674 at_end = self->str.ptr >= self->str.end;
675 len = self->str.ptr - start;
676
677 if ((c == '}') && (at_end || (c != *self->str.ptr))) {
678 PyErr_SetString(PyExc_ValueError, "Single '}' encountered "
679 "in format string");
680 return 0;
681 }
682 if (at_end && c == '{') {
683 PyErr_SetString(PyExc_ValueError, "Single '{' encountered "
684 "in format string");
685 return 0;
686 }
687 if (!at_end) {
688 if (c == *self->str.ptr) {
689 /* escaped } or {, skip it in the input. there is no
690 markup object following us, just this literal text */
691 self->str.ptr++;
692 markup_follows = 0;
693 }
694 else
695 len--;
696 }
697
698 /* record the literal text */
699 literal->ptr = start;
700 literal->end = start + len;
701
702 if (!markup_follows)
703 return 2;
704
705 /* this is markup, find the end of the string by counting nested
706 braces. note that this prohibits escaped braces, so that
707 format_specs cannot have braces in them. */
708 count = 1;
709
710 start = self->str.ptr;
711
712 /* we know we can't have a zero length string, so don't worry
713 about that case */
714 while (self->str.ptr < self->str.end) {
715 switch (c = *(self->str.ptr++)) {
716 case '{':
717 /* the format spec needs to be recursively expanded.
718 this is an optimization, and not strictly needed */
719 *format_spec_needs_expanding = 1;
720 count++;
721 break;
722 case '}':
723 count--;
724 if (count <= 0) {
725 /* we're done. parse and get out */
726 SubString s;
727
728 SubString_init(&s, start, self->str.ptr - 1 - start);
729 if (parse_field(&s, field_name, format_spec, conversion) == 0)
730 return 0;
731
732 /* a zero length field_name is an error */
733 if (field_name->ptr == field_name->end) {
734 PyErr_SetString(PyExc_ValueError, "zero length field name "
735 "in format");
736 return 0;
737 }
738
739 /* success */
740 return 2;
Eric Smith8c663262007-08-25 02:26:07 +0000741 }
742 break;
743 }
Eric Smith8c663262007-08-25 02:26:07 +0000744 }
Eric Smith625cbf22007-08-29 03:22:59 +0000745
746 /* end of string while searching for matching '}' */
747 PyErr_SetString(PyExc_ValueError, "unmatched '{' in format");
748 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000749}
750
751
752/* do the !r or !s conversion on obj */
753static PyObject *
754do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
755{
756 /* XXX in pre-3.0, do we need to convert this to unicode, since it
757 might have returned a string? */
758 switch (conversion) {
759 case 'r':
760 return PyObject_Repr(obj);
761 case 's':
Eric Smith8fd3eba2008-02-17 19:48:00 +0000762 return STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000763 default:
Martin v. Löwis5a6f4582008-04-07 03:22:07 +0000764 if (conversion > 32 && conversion < 127) {
765 /* It's the ASCII subrange; casting to char is safe
766 (assuming the execution character set is an ASCII
767 superset). */
768 PyErr_Format(PyExc_ValueError,
769 "Unknown conversion specifier %c",
770 (char)conversion);
771 } else
772 PyErr_Format(PyExc_ValueError,
773 "Unknown conversion specifier \\x%x",
774 (unsigned int)conversion);
Eric Smith8c663262007-08-25 02:26:07 +0000775 return NULL;
776 }
777}
778
779/* given:
780
781 {field_name!conversion:format_spec}
782
783 compute the result and write it to output.
784 format_spec_needs_expanding is an optimization. if it's false,
785 just output the string directly, otherwise recursively expand the
786 format_spec string. */
787
788static int
789output_markup(SubString *field_name, SubString *format_spec,
790 int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
791 OutputString *output, PyObject *args, PyObject *kwargs,
Eric Smith45c07872007-09-05 02:02:43 +0000792 int recursion_depth)
Eric Smith8c663262007-08-25 02:26:07 +0000793{
794 PyObject *tmp = NULL;
795 PyObject *fieldobj = NULL;
796 SubString expanded_format_spec;
797 SubString *actual_format_spec;
798 int result = 0;
799
800 /* convert field_name to an object */
801 fieldobj = get_field_object(field_name, args, kwargs);
802 if (fieldobj == NULL)
803 goto done;
804
805 if (conversion != '\0') {
806 tmp = do_conversion(fieldobj, conversion);
807 if (tmp == NULL)
808 goto done;
809
810 /* do the assignment, transferring ownership: fieldobj = tmp */
811 Py_DECREF(fieldobj);
812 fieldobj = tmp;
813 tmp = NULL;
814 }
815
816 /* if needed, recurively compute the format_spec */
817 if (format_spec_needs_expanding) {
Eric Smith45c07872007-09-05 02:02:43 +0000818 tmp = build_string(format_spec, args, kwargs, recursion_depth-1);
Eric Smith8c663262007-08-25 02:26:07 +0000819 if (tmp == NULL)
820 goto done;
821
822 /* note that in the case we're expanding the format string,
823 tmp must be kept around until after the call to
824 render_field. */
825 SubString_init(&expanded_format_spec,
826 STRINGLIB_STR(tmp), STRINGLIB_LEN(tmp));
827 actual_format_spec = &expanded_format_spec;
Eric Smith0cb431c2007-08-28 01:07:27 +0000828 }
829 else
Eric Smith8c663262007-08-25 02:26:07 +0000830 actual_format_spec = format_spec;
831
832 if (render_field(fieldobj, actual_format_spec, output) == 0)
833 goto done;
834
835 result = 1;
836
837done:
838 Py_XDECREF(fieldobj);
839 Py_XDECREF(tmp);
840
841 return result;
842}
843
844/*
Eric Smith8fd3eba2008-02-17 19:48:00 +0000845 do_markup is the top-level loop for the format() method. It
Eric Smith8c663262007-08-25 02:26:07 +0000846 searches through the format string for escapes to markup codes, and
847 calls other functions to move non-markup text to the output,
848 and to perform the markup to the output.
849*/
850static int
851do_markup(SubString *input, PyObject *args, PyObject *kwargs,
Eric Smith45c07872007-09-05 02:02:43 +0000852 OutputString *output, int recursion_depth)
Eric Smith8c663262007-08-25 02:26:07 +0000853{
854 MarkupIterator iter;
Eric Smith8c663262007-08-25 02:26:07 +0000855 int format_spec_needs_expanding;
856 int result;
Eric Smith625cbf22007-08-29 03:22:59 +0000857 SubString literal;
Eric Smith8c663262007-08-25 02:26:07 +0000858 SubString field_name;
859 SubString format_spec;
860 STRINGLIB_CHAR conversion;
861
862 MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
Eric Smith625cbf22007-08-29 03:22:59 +0000863 while ((result = MarkupIterator_next(&iter, &literal, &field_name,
Eric Smith8c663262007-08-25 02:26:07 +0000864 &format_spec, &conversion,
865 &format_spec_needs_expanding)) == 2) {
Eric Smith625cbf22007-08-29 03:22:59 +0000866 if (!output_data(output, literal.ptr, literal.end - literal.ptr))
867 return 0;
868 if (field_name.ptr != field_name.end)
Eric Smith8c663262007-08-25 02:26:07 +0000869 if (!output_markup(&field_name, &format_spec,
870 format_spec_needs_expanding, conversion, output,
Eric Smith45c07872007-09-05 02:02:43 +0000871 args, kwargs, recursion_depth))
Eric Smith8c663262007-08-25 02:26:07 +0000872 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000873 }
874 return result;
875}
876
877
878/*
879 build_string allocates the output string and then
880 calls do_markup to do the heavy lifting.
881*/
882static PyObject *
883build_string(SubString *input, PyObject *args, PyObject *kwargs,
Eric Smith45c07872007-09-05 02:02:43 +0000884 int recursion_depth)
Eric Smith8c663262007-08-25 02:26:07 +0000885{
886 OutputString output;
887 PyObject *result = NULL;
888 Py_ssize_t count;
889
890 output.obj = NULL; /* needed so cleanup code always works */
891
892 /* check the recursion level */
Eric Smith45c07872007-09-05 02:02:43 +0000893 if (recursion_depth <= 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000894 PyErr_SetString(PyExc_ValueError,
895 "Max string recursion exceeded");
896 goto done;
897 }
898
899 /* initial size is the length of the format string, plus the size
900 increment. seems like a reasonable default */
901 if (!output_initialize(&output,
902 input->end - input->ptr +
903 INITIAL_SIZE_INCREMENT))
904 goto done;
905
Eric Smith45c07872007-09-05 02:02:43 +0000906 if (!do_markup(input, args, kwargs, &output, recursion_depth)) {
Eric Smith8c663262007-08-25 02:26:07 +0000907 goto done;
908 }
909
910 count = output.ptr - STRINGLIB_STR(output.obj);
911 if (STRINGLIB_RESIZE(&output.obj, count) < 0) {
912 goto done;
913 }
914
915 /* transfer ownership to result */
916 result = output.obj;
917 output.obj = NULL;
918
919done:
Eric Smith8c663262007-08-25 02:26:07 +0000920 Py_XDECREF(output.obj);
921 return result;
922}
923
924/************************************************************************/
925/*********** main routine ***********************************************/
926/************************************************************************/
927
928/* this is the main entry point */
929static PyObject *
930do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
931{
932 SubString input;
933
934 /* PEP 3101 says only 2 levels, so that
935 "{0:{1}}".format('abc', 's') # works
936 "{0:{1:{2}}}".format('abc', 's', '') # fails
937 */
Eric Smith45c07872007-09-05 02:02:43 +0000938 int recursion_depth = 2;
Eric Smith8c663262007-08-25 02:26:07 +0000939
940 SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
Eric Smith45c07872007-09-05 02:02:43 +0000941 return build_string(&input, args, kwargs, recursion_depth);
Eric Smith8c663262007-08-25 02:26:07 +0000942}
Eric Smithf6db4092007-08-27 23:52:26 +0000943
944
945
946/************************************************************************/
947/*********** formatteriterator ******************************************/
948/************************************************************************/
949
950/* This is used to implement string.Formatter.vparse(). It exists so
951 Formatter can share code with the built in unicode.format() method.
952 It's really just a wrapper around MarkupIterator that is callable
953 from Python. */
954
955typedef struct {
956 PyObject_HEAD
957
Eric Smith8fd3eba2008-02-17 19:48:00 +0000958 STRINGLIB_OBJECT *str;
Eric Smithf6db4092007-08-27 23:52:26 +0000959
960 MarkupIterator it_markup;
961} formatteriterobject;
962
963static void
964formatteriter_dealloc(formatteriterobject *it)
965{
966 Py_XDECREF(it->str);
967 PyObject_FREE(it);
968}
969
970/* returns a tuple:
Eric Smith625cbf22007-08-29 03:22:59 +0000971 (literal, field_name, format_spec, conversion)
972
973 literal is any literal text to output. might be zero length
974 field_name is the string before the ':'. might be None
975 format_spec is the string after the ':'. mibht be None
976 conversion is either None, or the string after the '!'
Eric Smithf6db4092007-08-27 23:52:26 +0000977*/
978static PyObject *
979formatteriter_next(formatteriterobject *it)
980{
981 SubString literal;
982 SubString field_name;
983 SubString format_spec;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000984 STRINGLIB_CHAR conversion;
Eric Smithf6db4092007-08-27 23:52:26 +0000985 int format_spec_needs_expanding;
Eric Smith625cbf22007-08-29 03:22:59 +0000986 int result = MarkupIterator_next(&it->it_markup, &literal, &field_name,
987 &format_spec, &conversion,
Eric Smithf6db4092007-08-27 23:52:26 +0000988 &format_spec_needs_expanding);
989
990 /* all of the SubString objects point into it->str, so no
991 memory management needs to be done on them */
992 assert(0 <= result && result <= 2);
Eric Smith0cb431c2007-08-28 01:07:27 +0000993 if (result == 0 || result == 1)
Eric Smithf6db4092007-08-27 23:52:26 +0000994 /* if 0, error has already been set, if 1, iterator is empty */
995 return NULL;
Eric Smith0cb431c2007-08-28 01:07:27 +0000996 else {
Eric Smithf6db4092007-08-27 23:52:26 +0000997 PyObject *literal_str = NULL;
998 PyObject *field_name_str = NULL;
999 PyObject *format_spec_str = NULL;
1000 PyObject *conversion_str = NULL;
1001 PyObject *tuple = NULL;
Eric Smith625cbf22007-08-29 03:22:59 +00001002 int has_field = field_name.ptr != field_name.end;
Eric Smithf6db4092007-08-27 23:52:26 +00001003
Eric Smith625cbf22007-08-29 03:22:59 +00001004 literal_str = SubString_new_object(&literal);
1005 if (literal_str == NULL)
1006 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001007
Eric Smith625cbf22007-08-29 03:22:59 +00001008 field_name_str = SubString_new_object(&field_name);
1009 if (field_name_str == NULL)
1010 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001011
Eric Smith625cbf22007-08-29 03:22:59 +00001012 /* if field_name is non-zero length, return a string for
1013 format_spec (even if zero length), else return None */
1014 format_spec_str = (has_field ?
1015 SubString_new_object_or_empty :
1016 SubString_new_object)(&format_spec);
1017 if (format_spec_str == NULL)
1018 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001019
Eric Smith625cbf22007-08-29 03:22:59 +00001020 /* if the conversion is not specified, return a None,
1021 otherwise create a one length string with the conversion
1022 character */
1023 if (conversion == '\0') {
Eric Smithf6db4092007-08-27 23:52:26 +00001024 conversion_str = Py_None;
Eric Smithf6db4092007-08-27 23:52:26 +00001025 Py_INCREF(conversion_str);
1026 }
Eric Smith625cbf22007-08-29 03:22:59 +00001027 else
Eric Smith8fd3eba2008-02-17 19:48:00 +00001028 conversion_str = STRINGLIB_NEW(&conversion, 1);
Eric Smith625cbf22007-08-29 03:22:59 +00001029 if (conversion_str == NULL)
1030 goto done;
1031
Eric Smith9e7c8da2007-08-28 11:15:20 +00001032 tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str,
Eric Smithf6db4092007-08-27 23:52:26 +00001033 conversion_str);
Eric Smith625cbf22007-08-29 03:22:59 +00001034 done:
Eric Smithf6db4092007-08-27 23:52:26 +00001035 Py_XDECREF(literal_str);
1036 Py_XDECREF(field_name_str);
1037 Py_XDECREF(format_spec_str);
1038 Py_XDECREF(conversion_str);
1039 return tuple;
1040 }
1041}
1042
1043static PyMethodDef formatteriter_methods[] = {
1044 {NULL, NULL} /* sentinel */
1045};
1046
Eric Smith8fd3eba2008-02-17 19:48:00 +00001047static PyTypeObject PyFormatterIter_Type = {
Eric Smithf6db4092007-08-27 23:52:26 +00001048 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1049 "formatteriterator", /* tp_name */
1050 sizeof(formatteriterobject), /* tp_basicsize */
1051 0, /* tp_itemsize */
1052 /* methods */
1053 (destructor)formatteriter_dealloc, /* tp_dealloc */
1054 0, /* tp_print */
1055 0, /* tp_getattr */
1056 0, /* tp_setattr */
1057 0, /* tp_compare */
1058 0, /* tp_repr */
1059 0, /* tp_as_number */
1060 0, /* tp_as_sequence */
1061 0, /* tp_as_mapping */
1062 0, /* tp_hash */
1063 0, /* tp_call */
1064 0, /* tp_str */
1065 PyObject_GenericGetAttr, /* tp_getattro */
1066 0, /* tp_setattro */
1067 0, /* tp_as_buffer */
1068 Py_TPFLAGS_DEFAULT, /* tp_flags */
1069 0, /* tp_doc */
1070 0, /* tp_traverse */
1071 0, /* tp_clear */
1072 0, /* tp_richcompare */
1073 0, /* tp_weaklistoffset */
1074 PyObject_SelfIter, /* tp_iter */
1075 (iternextfunc)formatteriter_next, /* tp_iternext */
1076 formatteriter_methods, /* tp_methods */
1077 0,
1078};
1079
1080/* unicode_formatter_parser is used to implement
1081 string.Formatter.vformat. it parses a string and returns tuples
1082 describing the parsed elements. It's a wrapper around
1083 stringlib/string_format.h's MarkupIterator */
1084static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +00001085formatter_parser(STRINGLIB_OBJECT *self)
Eric Smithf6db4092007-08-27 23:52:26 +00001086{
1087 formatteriterobject *it;
1088
1089 it = PyObject_New(formatteriterobject, &PyFormatterIter_Type);
1090 if (it == NULL)
1091 return NULL;
1092
1093 /* take ownership, give the object to the iterator */
1094 Py_INCREF(self);
1095 it->str = self;
1096
1097 /* initialize the contained MarkupIterator */
1098 MarkupIterator_init(&it->it_markup,
Eric Smith8fd3eba2008-02-17 19:48:00 +00001099 STRINGLIB_STR(self),
1100 STRINGLIB_LEN(self));
Eric Smithf6db4092007-08-27 23:52:26 +00001101
1102 return (PyObject *)it;
1103}
1104
1105
1106/************************************************************************/
1107/*********** fieldnameiterator ******************************************/
1108/************************************************************************/
1109
1110
1111/* This is used to implement string.Formatter.vparse(). It parses the
1112 field name into attribute and item values. It's a Python-callable
1113 wrapper around FieldNameIterator */
1114
1115typedef struct {
1116 PyObject_HEAD
1117
Eric Smith8fd3eba2008-02-17 19:48:00 +00001118 STRINGLIB_OBJECT *str;
Eric Smithf6db4092007-08-27 23:52:26 +00001119
1120 FieldNameIterator it_field;
1121} fieldnameiterobject;
1122
1123static void
1124fieldnameiter_dealloc(fieldnameiterobject *it)
1125{
1126 Py_XDECREF(it->str);
1127 PyObject_FREE(it);
1128}
1129
1130/* returns a tuple:
1131 (is_attr, value)
1132 is_attr is true if we used attribute syntax (e.g., '.foo')
1133 false if we used index syntax (e.g., '[foo]')
1134 value is an integer or string
1135*/
1136static PyObject *
1137fieldnameiter_next(fieldnameiterobject *it)
1138{
1139 int result;
1140 int is_attr;
1141 Py_ssize_t idx;
1142 SubString name;
1143
1144 result = FieldNameIterator_next(&it->it_field, &is_attr,
1145 &idx, &name);
Eric Smith0cb431c2007-08-28 01:07:27 +00001146 if (result == 0 || result == 1)
Eric Smithf6db4092007-08-27 23:52:26 +00001147 /* if 0, error has already been set, if 1, iterator is empty */
1148 return NULL;
Eric Smith0cb431c2007-08-28 01:07:27 +00001149 else {
Eric Smithf6db4092007-08-27 23:52:26 +00001150 PyObject* result = NULL;
1151 PyObject* is_attr_obj = NULL;
1152 PyObject* obj = NULL;
1153
1154 is_attr_obj = PyBool_FromLong(is_attr);
1155 if (is_attr_obj == NULL)
Eric Smith625cbf22007-08-29 03:22:59 +00001156 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001157
1158 /* either an integer or a string */
1159 if (idx != -1)
Christian Heimes217cfd12007-12-02 14:31:20 +00001160 obj = PyLong_FromSsize_t(idx);
Eric Smithf6db4092007-08-27 23:52:26 +00001161 else
1162 obj = SubString_new_object(&name);
1163 if (obj == NULL)
Eric Smith625cbf22007-08-29 03:22:59 +00001164 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001165
1166 /* return a tuple of values */
1167 result = PyTuple_Pack(2, is_attr_obj, obj);
Eric Smithf6db4092007-08-27 23:52:26 +00001168
Eric Smith625cbf22007-08-29 03:22:59 +00001169 done:
Eric Smithf6db4092007-08-27 23:52:26 +00001170 Py_XDECREF(is_attr_obj);
1171 Py_XDECREF(obj);
Eric Smith625cbf22007-08-29 03:22:59 +00001172 return result;
Eric Smithf6db4092007-08-27 23:52:26 +00001173 }
Eric Smithf6db4092007-08-27 23:52:26 +00001174}
1175
1176static PyMethodDef fieldnameiter_methods[] = {
1177 {NULL, NULL} /* sentinel */
1178};
1179
1180static PyTypeObject PyFieldNameIter_Type = {
1181 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1182 "fieldnameiterator", /* tp_name */
1183 sizeof(fieldnameiterobject), /* tp_basicsize */
1184 0, /* tp_itemsize */
1185 /* methods */
1186 (destructor)fieldnameiter_dealloc, /* tp_dealloc */
1187 0, /* tp_print */
1188 0, /* tp_getattr */
1189 0, /* tp_setattr */
1190 0, /* tp_compare */
1191 0, /* tp_repr */
1192 0, /* tp_as_number */
1193 0, /* tp_as_sequence */
1194 0, /* tp_as_mapping */
1195 0, /* tp_hash */
1196 0, /* tp_call */
1197 0, /* tp_str */
1198 PyObject_GenericGetAttr, /* tp_getattro */
1199 0, /* tp_setattro */
1200 0, /* tp_as_buffer */
1201 Py_TPFLAGS_DEFAULT, /* tp_flags */
1202 0, /* tp_doc */
1203 0, /* tp_traverse */
1204 0, /* tp_clear */
1205 0, /* tp_richcompare */
1206 0, /* tp_weaklistoffset */
1207 PyObject_SelfIter, /* tp_iter */
1208 (iternextfunc)fieldnameiter_next, /* tp_iternext */
1209 fieldnameiter_methods, /* tp_methods */
1210 0};
1211
1212/* unicode_formatter_field_name_split is used to implement
1213 string.Formatter.vformat. it takes an PEP 3101 "field name", and
1214 returns a tuple of (first, rest): "first", the part before the
1215 first '.' or '['; and "rest", an iterator for the rest of the field
1216 name. it's a wrapper around stringlib/string_format.h's
1217 field_name_split. The iterator it returns is a
1218 FieldNameIterator */
1219static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +00001220formatter_field_name_split(STRINGLIB_OBJECT *self)
Eric Smithf6db4092007-08-27 23:52:26 +00001221{
1222 SubString first;
1223 Py_ssize_t first_idx;
1224 fieldnameiterobject *it;
1225
1226 PyObject *first_obj = NULL;
1227 PyObject *result = NULL;
1228
1229 it = PyObject_New(fieldnameiterobject, &PyFieldNameIter_Type);
1230 if (it == NULL)
1231 return NULL;
1232
1233 /* take ownership, give the object to the iterator. this is
1234 just to keep the field_name alive */
1235 Py_INCREF(self);
1236 it->str = self;
1237
1238 if (!field_name_split(STRINGLIB_STR(self),
1239 STRINGLIB_LEN(self),
1240 &first, &first_idx, &it->it_field))
Eric Smith625cbf22007-08-29 03:22:59 +00001241 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001242
Eric Smith0cb431c2007-08-28 01:07:27 +00001243 /* first becomes an integer, if possible; else a string */
Eric Smithf6db4092007-08-27 23:52:26 +00001244 if (first_idx != -1)
Christian Heimes217cfd12007-12-02 14:31:20 +00001245 first_obj = PyLong_FromSsize_t(first_idx);
Eric Smithf6db4092007-08-27 23:52:26 +00001246 else
1247 /* convert "first" into a string object */
1248 first_obj = SubString_new_object(&first);
1249 if (first_obj == NULL)
Eric Smith625cbf22007-08-29 03:22:59 +00001250 goto done;
Eric Smithf6db4092007-08-27 23:52:26 +00001251
1252 /* return a tuple of values */
1253 result = PyTuple_Pack(2, first_obj, it);
1254
Eric Smith625cbf22007-08-29 03:22:59 +00001255done:
Eric Smithf6db4092007-08-27 23:52:26 +00001256 Py_XDECREF(it);
1257 Py_XDECREF(first_obj);
1258 return result;
1259}