+++ /dev/null
-/* implements the string, long, and float formatters. that is,\r
- string.__format__, etc. */\r
-\r
-#include <locale.h>\r
-\r
-/* Before including this, you must include either:\r
- stringlib/unicodedefs.h\r
- stringlib/stringdefs.h\r
-\r
- Also, you should define the names:\r
- FORMAT_STRING\r
- FORMAT_LONG\r
- FORMAT_FLOAT\r
- FORMAT_COMPLEX\r
- to be whatever you want the public names of these functions to\r
- be. These are the only non-static functions defined here.\r
-*/\r
-\r
-/* Raises an exception about an unknown presentation type for this\r
- * type. */\r
-\r
-static void\r
-unknown_presentation_type(STRINGLIB_CHAR presentation_type,\r
- const char* type_name)\r
-{\r
-#if STRINGLIB_IS_UNICODE\r
- /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,\r
- hence the two cases. If it is char, gcc complains that the\r
- condition below is always true, hence the ifdef. */\r
- if (presentation_type > 32 && presentation_type < 128)\r
-#endif\r
- PyErr_Format(PyExc_ValueError,\r
- "Unknown format code '%c' "\r
- "for object of type '%.200s'",\r
- (char)presentation_type,\r
- type_name);\r
-#if STRINGLIB_IS_UNICODE\r
- else\r
- PyErr_Format(PyExc_ValueError,\r
- "Unknown format code '\\x%x' "\r
- "for object of type '%.200s'",\r
- (unsigned int)presentation_type,\r
- type_name);\r
-#endif\r
-}\r
-\r
-static void\r
-invalid_comma_type(STRINGLIB_CHAR presentation_type)\r
-{\r
-#if STRINGLIB_IS_UNICODE\r
- /* See comment in unknown_presentation_type */\r
- if (presentation_type > 32 && presentation_type < 128)\r
-#endif\r
- PyErr_Format(PyExc_ValueError,\r
- "Cannot specify ',' with '%c'.",\r
- (char)presentation_type);\r
-#if STRINGLIB_IS_UNICODE\r
- else\r
- PyErr_Format(PyExc_ValueError,\r
- "Cannot specify ',' with '\\x%x'.",\r
- (unsigned int)presentation_type);\r
-#endif\r
-}\r
-\r
-/*\r
- get_integer consumes 0 or more decimal digit characters from an\r
- input string, updates *result with the corresponding positive\r
- integer, and returns the number of digits consumed.\r
-\r
- returns -1 on error.\r
-*/\r
-static int\r
-get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,\r
- Py_ssize_t *result)\r
-{\r
- Py_ssize_t accumulator, digitval, oldaccumulator;\r
- int numdigits;\r
- accumulator = numdigits = 0;\r
- for (;;(*ptr)++, numdigits++) {\r
- if (*ptr >= end)\r
- break;\r
- digitval = STRINGLIB_TODECIMAL(**ptr);\r
- if (digitval < 0)\r
- break;\r
- /*\r
- This trick was copied from old Unicode format code. It's cute,\r
- but would really suck on an old machine with a slow divide\r
- implementation. Fortunately, in the normal case we do not\r
- expect too many digits.\r
- */\r
- oldaccumulator = accumulator;\r
- accumulator *= 10;\r
- if ((accumulator+10)/10 != oldaccumulator+1) {\r
- PyErr_Format(PyExc_ValueError,\r
- "Too many decimal digits in format string");\r
- return -1;\r
- }\r
- accumulator += digitval;\r
- }\r
- *result = accumulator;\r
- return numdigits;\r
-}\r
-\r
-/************************************************************************/\r
-/*********** standard format specifier parsing **************************/\r
-/************************************************************************/\r
-\r
-/* returns true if this character is a specifier alignment token */\r
-Py_LOCAL_INLINE(int)\r
-is_alignment_token(STRINGLIB_CHAR c)\r
-{\r
- switch (c) {\r
- case '<': case '>': case '=': case '^':\r
- return 1;\r
- default:\r
- return 0;\r
- }\r
-}\r
-\r
-/* returns true if this character is a sign element */\r
-Py_LOCAL_INLINE(int)\r
-is_sign_element(STRINGLIB_CHAR c)\r
-{\r
- switch (c) {\r
- case ' ': case '+': case '-':\r
- return 1;\r
- default:\r
- return 0;\r
- }\r
-}\r
-\r
-\r
-typedef struct {\r
- STRINGLIB_CHAR fill_char;\r
- STRINGLIB_CHAR align;\r
- int alternate;\r
- STRINGLIB_CHAR sign;\r
- Py_ssize_t width;\r
- int thousands_separators;\r
- Py_ssize_t precision;\r
- STRINGLIB_CHAR type;\r
-} InternalFormatSpec;\r
-\r
-\r
-#if 0\r
-/* Occassionally useful for debugging. Should normally be commented out. */\r
-static void\r
-DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)\r
-{\r
- printf("internal format spec: fill_char %d\n", format->fill_char);\r
- printf("internal format spec: align %d\n", format->align);\r
- printf("internal format spec: alternate %d\n", format->alternate);\r
- printf("internal format spec: sign %d\n", format->sign);\r
- printf("internal format spec: width %zd\n", format->width);\r
- printf("internal format spec: thousands_separators %d\n",\r
- format->thousands_separators);\r
- printf("internal format spec: precision %zd\n", format->precision);\r
- printf("internal format spec: type %c\n", format->type);\r
- printf("\n");\r
-}\r
-#endif\r
-\r
-\r
-/*\r
- ptr points to the start of the format_spec, end points just past its end.\r
- fills in format with the parsed information.\r
- returns 1 on success, 0 on failure.\r
- if failure, sets the exception\r
-*/\r
-static int\r
-parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len,\r
- InternalFormatSpec *format,\r
- char default_type,\r
- char default_align)\r
-{\r
- STRINGLIB_CHAR *ptr = format_spec;\r
- STRINGLIB_CHAR *end = format_spec + format_spec_len;\r
-\r
- /* end-ptr is used throughout this code to specify the length of\r
- the input string */\r
-\r
- Py_ssize_t consumed;\r
- int align_specified = 0;\r
-\r
- format->fill_char = '\0';\r
- format->align = default_align;\r
- format->alternate = 0;\r
- format->sign = '\0';\r
- format->width = -1;\r
- format->thousands_separators = 0;\r
- format->precision = -1;\r
- format->type = default_type;\r
-\r
- /* If the second char is an alignment token,\r
- then parse the fill char */\r
- if (end-ptr >= 2 && is_alignment_token(ptr[1])) {\r
- format->align = ptr[1];\r
- format->fill_char = ptr[0];\r
- align_specified = 1;\r
- ptr += 2;\r
- }\r
- else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {\r
- format->align = ptr[0];\r
- align_specified = 1;\r
- ++ptr;\r
- }\r
-\r
- /* Parse the various sign options */\r
- if (end-ptr >= 1 && is_sign_element(ptr[0])) {\r
- format->sign = ptr[0];\r
- ++ptr;\r
- }\r
-\r
- /* If the next character is #, we're in alternate mode. This only\r
- applies to integers. */\r
- if (end-ptr >= 1 && ptr[0] == '#') {\r
- format->alternate = 1;\r
- ++ptr;\r
- }\r
-\r
- /* The special case for 0-padding (backwards compat) */\r
- if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {\r
- format->fill_char = '0';\r
- if (!align_specified) {\r
- format->align = '=';\r
- }\r
- ++ptr;\r
- }\r
-\r
- consumed = get_integer(&ptr, end, &format->width);\r
- if (consumed == -1)\r
- /* Overflow error. Exception already set. */\r
- return 0;\r
-\r
- /* If consumed is 0, we didn't consume any characters for the\r
- width. In that case, reset the width to -1, because\r
- get_integer() will have set it to zero. -1 is how we record\r
- that the width wasn't specified. */\r
- if (consumed == 0)\r
- format->width = -1;\r
-\r
- /* Comma signifies add thousands separators */\r
- if (end-ptr && ptr[0] == ',') {\r
- format->thousands_separators = 1;\r
- ++ptr;\r
- }\r
-\r
- /* Parse field precision */\r
- if (end-ptr && ptr[0] == '.') {\r
- ++ptr;\r
-\r
- consumed = get_integer(&ptr, end, &format->precision);\r
- if (consumed == -1)\r
- /* Overflow error. Exception already set. */\r
- return 0;\r
-\r
- /* Not having a precision after a dot is an error. */\r
- if (consumed == 0) {\r
- PyErr_Format(PyExc_ValueError,\r
- "Format specifier missing precision");\r
- return 0;\r
- }\r
-\r
- }\r
-\r
- /* Finally, parse the type field. */\r
-\r
- if (end-ptr > 1) {\r
- /* More than one char remain, invalid conversion spec. */\r
- PyErr_Format(PyExc_ValueError, "Invalid conversion specification");\r
- return 0;\r
- }\r
-\r
- if (end-ptr == 1) {\r
- format->type = ptr[0];\r
- ++ptr;\r
- }\r
-\r
- /* Do as much validating as we can, just by looking at the format\r
- specifier. Do not take into account what type of formatting\r
- we're doing (int, float, string). */\r
-\r
- if (format->thousands_separators) {\r
- switch (format->type) {\r
- case 'd':\r
- case 'e':\r
- case 'f':\r
- case 'g':\r
- case 'E':\r
- case 'G':\r
- case '%':\r
- case 'F':\r
- case '\0':\r
- /* These are allowed. See PEP 378.*/\r
- break;\r
- default:\r
- invalid_comma_type(format->type);\r
- return 0;\r
- }\r
- }\r
-\r
- return 1;\r
-}\r
-\r
-/* Calculate the padding needed. */\r
-static void\r
-calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,\r
- Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,\r
- Py_ssize_t *n_total)\r
-{\r
- if (width >= 0) {\r
- if (nchars > width)\r
- *n_total = nchars;\r
- else\r
- *n_total = width;\r
- }\r
- else {\r
- /* not specified, use all of the chars and no more */\r
- *n_total = nchars;\r
- }\r
-\r
- /* Figure out how much leading space we need, based on the\r
- aligning */\r
- if (align == '>')\r
- *n_lpadding = *n_total - nchars;\r
- else if (align == '^')\r
- *n_lpadding = (*n_total - nchars) / 2;\r
- else if (align == '<' || align == '=')\r
- *n_lpadding = 0;\r
- else {\r
- /* We should never have an unspecified alignment. */\r
- *n_lpadding = 0;\r
- assert(0);\r
- }\r
-\r
- *n_rpadding = *n_total - nchars - *n_lpadding;\r
-}\r
-\r
-/* Do the padding, and return a pointer to where the caller-supplied\r
- content goes. */\r
-static STRINGLIB_CHAR *\r
-fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,\r
- Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)\r
-{\r
- /* Pad on left. */\r
- if (n_lpadding)\r
- STRINGLIB_FILL(p, fill_char, n_lpadding);\r
-\r
- /* Pad on right. */\r
- if (n_rpadding)\r
- STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);\r
-\r
- /* Pointer to the user content. */\r
- return p + n_lpadding;\r
-}\r
-\r
-#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX\r
-/************************************************************************/\r
-/*********** common routines for numeric formatting *********************/\r
-/************************************************************************/\r
-\r
-/* Locale type codes. */\r
-#define LT_CURRENT_LOCALE 0\r
-#define LT_DEFAULT_LOCALE 1\r
-#define LT_NO_LOCALE 2\r
-\r
-/* Locale info needed for formatting integers and the part of floats\r
- before and including the decimal. Note that locales only support\r
- 8-bit chars, not unicode. */\r
-typedef struct {\r
- char *decimal_point;\r
- char *thousands_sep;\r
- char *grouping;\r
-} LocaleInfo;\r
-\r
-/* describes the layout for an integer, see the comment in\r
- calc_number_widths() for details */\r
-typedef struct {\r
- Py_ssize_t n_lpadding;\r
- Py_ssize_t n_prefix;\r
- Py_ssize_t n_spadding;\r
- Py_ssize_t n_rpadding;\r
- char sign;\r
- Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */\r
- Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including\r
- any grouping chars. */\r
- Py_ssize_t n_decimal; /* 0 if only an integer */\r
- Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,\r
- excluding the decimal itself, if\r
- present. */\r
-\r
- /* These 2 are not the widths of fields, but are needed by\r
- STRINGLIB_GROUPING. */\r
- Py_ssize_t n_digits; /* The number of digits before a decimal\r
- or exponent. */\r
- Py_ssize_t n_min_width; /* The min_width we used when we computed\r
- the n_grouped_digits width. */\r
-} NumberFieldWidths;\r
-\r
-\r
-/* Given a number of the form:\r
- digits[remainder]\r
- where ptr points to the start and end points to the end, find where\r
- the integer part ends. This could be a decimal, an exponent, both,\r
- or neither.\r
- If a decimal point is present, set *has_decimal and increment\r
- remainder beyond it.\r
- Results are undefined (but shouldn't crash) for improperly\r
- formatted strings.\r
-*/\r
-static void\r
-parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,\r
- Py_ssize_t *n_remainder, int *has_decimal)\r
-{\r
- STRINGLIB_CHAR *end = ptr + len;\r
- STRINGLIB_CHAR *remainder;\r
-\r
- while (ptr<end && isdigit(*ptr))\r
- ++ptr;\r
- remainder = ptr;\r
-\r
- /* Does remainder start with a decimal point? */\r
- *has_decimal = ptr<end && *remainder == '.';\r
-\r
- /* Skip the decimal point. */\r
- if (*has_decimal)\r
- remainder++;\r
-\r
- *n_remainder = end - remainder;\r
-}\r
-\r
-/* not all fields of format are used. for example, precision is\r
- unused. should this take discrete params in order to be more clear\r
- about what it does? or is passing a single format parameter easier\r
- and more efficient enough to justify a little obfuscation? */\r
-static Py_ssize_t\r
-calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,\r
- STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,\r
- Py_ssize_t n_number, Py_ssize_t n_remainder,\r
- int has_decimal, const LocaleInfo *locale,\r
- const InternalFormatSpec *format)\r
-{\r
- Py_ssize_t n_non_digit_non_padding;\r
- Py_ssize_t n_padding;\r
-\r
- spec->n_digits = n_number - n_remainder - (has_decimal?1:0);\r
- spec->n_lpadding = 0;\r
- spec->n_prefix = n_prefix;\r
- spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;\r
- spec->n_remainder = n_remainder;\r
- spec->n_spadding = 0;\r
- spec->n_rpadding = 0;\r
- spec->sign = '\0';\r
- spec->n_sign = 0;\r
-\r
- /* the output will look like:\r
- | |\r
- | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |\r
- | |\r
-\r
- sign is computed from format->sign and the actual\r
- sign of the number\r
-\r
- prefix is given (it's for the '0x' prefix)\r
-\r
- digits is already known\r
-\r
- the total width is either given, or computed from the\r
- actual digits\r
-\r
- only one of lpadding, spadding, and rpadding can be non-zero,\r
- and it's calculated from the width and other fields\r
- */\r
-\r
- /* compute the various parts we're going to write */\r
- switch (format->sign) {\r
- case '+':\r
- /* always put a + or - */\r
- spec->n_sign = 1;\r
- spec->sign = (sign_char == '-' ? '-' : '+');\r
- break;\r
- case ' ':\r
- spec->n_sign = 1;\r
- spec->sign = (sign_char == '-' ? '-' : ' ');\r
- break;\r
- default:\r
- /* Not specified, or the default (-) */\r
- if (sign_char == '-') {\r
- spec->n_sign = 1;\r
- spec->sign = '-';\r
- }\r
- }\r
-\r
- /* The number of chars used for non-digits and non-padding. */\r
- n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +\r
- spec->n_remainder;\r
-\r
- /* min_width can go negative, that's okay. format->width == -1 means\r
- we don't care. */\r
- if (format->fill_char == '0' && format->align == '=')\r
- spec->n_min_width = format->width - n_non_digit_non_padding;\r
- else\r
- spec->n_min_width = 0;\r
-\r
- if (spec->n_digits == 0)\r
- /* This case only occurs when using 'c' formatting, we need\r
- to special case it because the grouping code always wants\r
- to have at least one character. */\r
- spec->n_grouped_digits = 0;\r
- else\r
- spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,\r
- spec->n_digits,\r
- spec->n_min_width,\r
- locale->grouping,\r
- locale->thousands_sep);\r
-\r
- /* Given the desired width and the total of digit and non-digit\r
- space we consume, see if we need any padding. format->width can\r
- be negative (meaning no padding), but this code still works in\r
- that case. */\r
- n_padding = format->width -\r
- (n_non_digit_non_padding + spec->n_grouped_digits);\r
- if (n_padding > 0) {\r
- /* Some padding is needed. Determine if it's left, space, or right. */\r
- switch (format->align) {\r
- case '<':\r
- spec->n_rpadding = n_padding;\r
- break;\r
- case '^':\r
- spec->n_lpadding = n_padding / 2;\r
- spec->n_rpadding = n_padding - spec->n_lpadding;\r
- break;\r
- case '=':\r
- spec->n_spadding = n_padding;\r
- break;\r
- case '>':\r
- spec->n_lpadding = n_padding;\r
- break;\r
- default:\r
- /* Shouldn't get here, but treat it as '>' */\r
- spec->n_lpadding = n_padding;\r
- assert(0);\r
- break;\r
- }\r
- }\r
- return spec->n_lpadding + spec->n_sign + spec->n_prefix +\r
- spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +\r
- spec->n_remainder + spec->n_rpadding;\r
-}\r
-\r
-/* Fill in the digit parts of a numbers's string representation,\r
- as determined in calc_number_widths().\r
- No error checking, since we know the buffer is the correct size. */\r
-static void\r
-fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,\r
- STRINGLIB_CHAR *digits, Py_ssize_t n_digits,\r
- STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,\r
- LocaleInfo *locale, int toupper)\r
-{\r
- /* Used to keep track of digits, decimal, and remainder. */\r
- STRINGLIB_CHAR *p = digits;\r
-\r
-#ifndef NDEBUG\r
- Py_ssize_t r;\r
-#endif\r
-\r
- if (spec->n_lpadding) {\r
- STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);\r
- buf += spec->n_lpadding;\r
- }\r
- if (spec->n_sign == 1) {\r
- *buf++ = spec->sign;\r
- }\r
- if (spec->n_prefix) {\r
- memmove(buf,\r
- prefix,\r
- spec->n_prefix * sizeof(STRINGLIB_CHAR));\r
- if (toupper) {\r
- Py_ssize_t t;\r
- for (t = 0; t < spec->n_prefix; ++t)\r
- buf[t] = STRINGLIB_TOUPPER(buf[t]);\r
- }\r
- buf += spec->n_prefix;\r
- }\r
- if (spec->n_spadding) {\r
- STRINGLIB_FILL(buf, fill_char, spec->n_spadding);\r
- buf += spec->n_spadding;\r
- }\r
-\r
- /* Only for type 'c' special case, it has no digits. */\r
- if (spec->n_digits != 0) {\r
- /* Fill the digits with InsertThousandsGrouping. */\r
-#ifndef NDEBUG\r
- r =\r
-#endif\r
- STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,\r
- spec->n_digits, spec->n_min_width,\r
- locale->grouping, locale->thousands_sep);\r
-#ifndef NDEBUG\r
- assert(r == spec->n_grouped_digits);\r
-#endif\r
- p += spec->n_digits;\r
- }\r
- if (toupper) {\r
- Py_ssize_t t;\r
- for (t = 0; t < spec->n_grouped_digits; ++t)\r
- buf[t] = STRINGLIB_TOUPPER(buf[t]);\r
- }\r
- buf += spec->n_grouped_digits;\r
-\r
- if (spec->n_decimal) {\r
- Py_ssize_t t;\r
- for (t = 0; t < spec->n_decimal; ++t)\r
- buf[t] = locale->decimal_point[t];\r
- buf += spec->n_decimal;\r
- p += 1;\r
- }\r
-\r
- if (spec->n_remainder) {\r
- memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));\r
- buf += spec->n_remainder;\r
- p += spec->n_remainder;\r
- }\r
-\r
- if (spec->n_rpadding) {\r
- STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);\r
- buf += spec->n_rpadding;\r
- }\r
-}\r
-\r
-static char no_grouping[1] = {CHAR_MAX};\r
-\r
-/* Find the decimal point character(s?), thousands_separator(s?), and\r
- grouping description, either for the current locale if type is\r
- LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or\r
- none if LT_NO_LOCALE. */\r
-static void\r
-get_locale_info(int type, LocaleInfo *locale_info)\r
-{\r
- switch (type) {\r
- case LT_CURRENT_LOCALE: {\r
- struct lconv *locale_data = localeconv();\r
- locale_info->decimal_point = locale_data->decimal_point;\r
- locale_info->thousands_sep = locale_data->thousands_sep;\r
- locale_info->grouping = locale_data->grouping;\r
- break;\r
- }\r
- case LT_DEFAULT_LOCALE:\r
- locale_info->decimal_point = ".";\r
- locale_info->thousands_sep = ",";\r
- locale_info->grouping = "\3"; /* Group every 3 characters. The\r
- (implicit) trailing 0 means repeat\r
- infinitely. */\r
- break;\r
- case LT_NO_LOCALE:\r
- locale_info->decimal_point = ".";\r
- locale_info->thousands_sep = "";\r
- locale_info->grouping = no_grouping;\r
- break;\r
- default:\r
- assert(0);\r
- }\r
-}\r
-\r
-#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */\r
-\r
-/************************************************************************/\r
-/*********** string formatting ******************************************/\r
-/************************************************************************/\r
-\r
-static PyObject *\r
-format_string_internal(PyObject *value, const InternalFormatSpec *format)\r
-{\r
- Py_ssize_t lpad;\r
- Py_ssize_t rpad;\r
- Py_ssize_t total;\r
- STRINGLIB_CHAR *p;\r
- Py_ssize_t len = STRINGLIB_LEN(value);\r
- PyObject *result = NULL;\r
-\r
- /* sign is not allowed on strings */\r
- if (format->sign != '\0') {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Sign not allowed in string format specifier");\r
- goto done;\r
- }\r
-\r
- /* alternate is not allowed on strings */\r
- if (format->alternate) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Alternate form (#) not allowed in string format "\r
- "specifier");\r
- goto done;\r
- }\r
-\r
- /* '=' alignment not allowed on strings */\r
- if (format->align == '=') {\r
- PyErr_SetString(PyExc_ValueError,\r
- "'=' alignment not allowed "\r
- "in string format specifier");\r
- goto done;\r
- }\r
-\r
- /* if precision is specified, output no more that format.precision\r
- characters */\r
- if (format->precision >= 0 && len >= format->precision) {\r
- len = format->precision;\r
- }\r
-\r
- calc_padding(len, format->width, format->align, &lpad, &rpad, &total);\r
-\r
- /* allocate the resulting string */\r
- result = STRINGLIB_NEW(NULL, total);\r
- if (result == NULL)\r
- goto done;\r
-\r
- /* Write into that space. First the padding. */\r
- p = fill_padding(STRINGLIB_STR(result), len,\r
- format->fill_char=='\0'?' ':format->fill_char,\r
- lpad, rpad);\r
-\r
- /* Then the source string. */\r
- memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));\r
-\r
-done:\r
- return result;\r
-}\r
-\r
-\r
-/************************************************************************/\r
-/*********** long formatting ********************************************/\r
-/************************************************************************/\r
-\r
-#if defined FORMAT_LONG || defined FORMAT_INT\r
-typedef PyObject*\r
-(*IntOrLongToString)(PyObject *value, int base);\r
-\r
-static PyObject *\r
-format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,\r
- IntOrLongToString tostring)\r
-{\r
- PyObject *result = NULL;\r
- PyObject *tmp = NULL;\r
- STRINGLIB_CHAR *pnumeric_chars;\r
- STRINGLIB_CHAR numeric_char;\r
- STRINGLIB_CHAR sign_char = '\0';\r
- Py_ssize_t n_digits; /* count of digits need from the computed\r
- string */\r
- Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which\r
- produces non-digits */\r
- Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */\r
- Py_ssize_t n_total;\r
- STRINGLIB_CHAR *prefix = NULL;\r
- NumberFieldWidths spec;\r
- long x;\r
-\r
- /* Locale settings, either from the actual locale or\r
- from a hard-code pseudo-locale */\r
- LocaleInfo locale;\r
-\r
- /* no precision allowed on integers */\r
- if (format->precision != -1) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Precision not allowed in integer format specifier");\r
- goto done;\r
- }\r
-\r
- /* special case for character formatting */\r
- if (format->type == 'c') {\r
- /* error to specify a sign */\r
- if (format->sign != '\0') {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Sign not allowed with integer"\r
- " format specifier 'c'");\r
- goto done;\r
- }\r
-\r
- /* Error to specify a comma. */\r
- if (format->thousands_separators) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Thousands separators not allowed with integer"\r
- " format specifier 'c'");\r
- goto done;\r
- }\r
-\r
- /* taken from unicodeobject.c formatchar() */\r
- /* Integer input truncated to a character */\r
-/* XXX: won't work for int */\r
- x = PyLong_AsLong(value);\r
- if (x == -1 && PyErr_Occurred())\r
- goto done;\r
-#ifdef Py_UNICODE_WIDE\r
- if (x < 0 || x > 0x10ffff) {\r
- PyErr_SetString(PyExc_OverflowError,\r
- "%c arg not in range(0x110000) "\r
- "(wide Python build)");\r
- goto done;\r
- }\r
-#else\r
- if (x < 0 || x > 0xffff) {\r
- PyErr_SetString(PyExc_OverflowError,\r
- "%c arg not in range(0x10000) "\r
- "(narrow Python build)");\r
- goto done;\r
- }\r
-#endif\r
- numeric_char = (STRINGLIB_CHAR)x;\r
- pnumeric_chars = &numeric_char;\r
- n_digits = 1;\r
-\r
- /* As a sort-of hack, we tell calc_number_widths that we only\r
- have "remainder" characters. calc_number_widths thinks\r
- these are characters that don't get formatted, only copied\r
- into the output string. We do this for 'c' formatting,\r
- because the characters are likely to be non-digits. */\r
- n_remainder = 1;\r
- }\r
- else {\r
- int base;\r
- int leading_chars_to_skip = 0; /* Number of characters added by\r
- PyNumber_ToBase that we want to\r
- skip over. */\r
-\r
- /* Compute the base and how many characters will be added by\r
- PyNumber_ToBase */\r
- switch (format->type) {\r
- case 'b':\r
- base = 2;\r
- leading_chars_to_skip = 2; /* 0b */\r
- break;\r
- case 'o':\r
- base = 8;\r
- leading_chars_to_skip = 2; /* 0o */\r
- break;\r
- case 'x':\r
- case 'X':\r
- base = 16;\r
- leading_chars_to_skip = 2; /* 0x */\r
- break;\r
- default: /* shouldn't be needed, but stops a compiler warning */\r
- case 'd':\r
- case 'n':\r
- base = 10;\r
- break;\r
- }\r
-\r
- /* The number of prefix chars is the same as the leading\r
- chars to skip */\r
- if (format->alternate)\r
- n_prefix = leading_chars_to_skip;\r
-\r
- /* Do the hard part, converting to a string in a given base */\r
- tmp = tostring(value, base);\r
- if (tmp == NULL)\r
- goto done;\r
-\r
- pnumeric_chars = STRINGLIB_STR(tmp);\r
- n_digits = STRINGLIB_LEN(tmp);\r
-\r
- prefix = pnumeric_chars;\r
-\r
- /* Remember not to modify what pnumeric_chars points to. it\r
- might be interned. Only modify it after we copy it into a\r
- newly allocated output buffer. */\r
-\r
- /* Is a sign character present in the output? If so, remember it\r
- and skip it */\r
- if (pnumeric_chars[0] == '-') {\r
- sign_char = pnumeric_chars[0];\r
- ++prefix;\r
- ++leading_chars_to_skip;\r
- }\r
-\r
- /* Skip over the leading chars (0x, 0b, etc.) */\r
- n_digits -= leading_chars_to_skip;\r
- pnumeric_chars += leading_chars_to_skip;\r
- }\r
-\r
- /* Determine the grouping, separator, and decimal point, if any. */\r
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r
- (format->thousands_separators ?\r
- LT_DEFAULT_LOCALE :\r
- LT_NO_LOCALE),\r
- &locale);\r
-\r
- /* Calculate how much memory we'll need. */\r
- n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,\r
- n_digits, n_remainder, 0, &locale, format);\r
-\r
- /* Allocate the memory. */\r
- result = STRINGLIB_NEW(NULL, n_total);\r
- if (!result)\r
- goto done;\r
-\r
- /* Populate the memory. */\r
- fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,\r
- prefix, format->fill_char == '\0' ? ' ' : format->fill_char,\r
- &locale, format->type == 'X');\r
-\r
-done:\r
- Py_XDECREF(tmp);\r
- return result;\r
-}\r
-#endif /* defined FORMAT_LONG || defined FORMAT_INT */\r
-\r
-/************************************************************************/\r
-/*********** float formatting *******************************************/\r
-/************************************************************************/\r
-\r
-#ifdef FORMAT_FLOAT\r
-#if STRINGLIB_IS_UNICODE\r
-static void\r
-strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)\r
-{\r
- Py_ssize_t i;\r
- for (i = 0; i < len; ++i)\r
- buffer[i] = (Py_UNICODE)charbuffer[i];\r
-}\r
-#endif\r
-\r
-/* much of this is taken from unicodeobject.c */\r
-static PyObject *\r
-format_float_internal(PyObject *value,\r
- const InternalFormatSpec *format)\r
-{\r
- char *buf = NULL; /* buffer returned from PyOS_double_to_string */\r
- Py_ssize_t n_digits;\r
- Py_ssize_t n_remainder;\r
- Py_ssize_t n_total;\r
- int has_decimal;\r
- double val;\r
- Py_ssize_t precision = format->precision;\r
- Py_ssize_t default_precision = 6;\r
- STRINGLIB_CHAR type = format->type;\r
- int add_pct = 0;\r
- STRINGLIB_CHAR *p;\r
- NumberFieldWidths spec;\r
- int flags = 0;\r
- PyObject *result = NULL;\r
- STRINGLIB_CHAR sign_char = '\0';\r
- int float_type; /* Used to see if we have a nan, inf, or regular float. */\r
-\r
-#if STRINGLIB_IS_UNICODE\r
- Py_UNICODE *unicode_tmp = NULL;\r
-#endif\r
-\r
- /* Locale settings, either from the actual locale or\r
- from a hard-code pseudo-locale */\r
- LocaleInfo locale;\r
-\r
- /* Alternate is not allowed on floats. */\r
- if (format->alternate) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Alternate form (#) not allowed in float format "\r
- "specifier");\r
- goto done;\r
- }\r
-\r
- if (type == '\0') {\r
- /* Omitted type specifier. This is like 'g' but with at least one\r
- digit after the decimal point, and different default precision.*/\r
- type = 'g';\r
- default_precision = PyFloat_STR_PRECISION;\r
- flags |= Py_DTSF_ADD_DOT_0;\r
- }\r
-\r
- if (type == 'n')\r
- /* 'n' is the same as 'g', except for the locale used to\r
- format the result. We take care of that later. */\r
- type = 'g';\r
-\r
- val = PyFloat_AsDouble(value);\r
- if (val == -1.0 && PyErr_Occurred())\r
- goto done;\r
-\r
- if (type == '%') {\r
- type = 'f';\r
- val *= 100;\r
- add_pct = 1;\r
- }\r
-\r
- if (precision < 0)\r
- precision = default_precision;\r
-\r
- /* Cast "type", because if we're in unicode we need to pass a\r
- 8-bit char. This is safe, because we've restricted what "type"\r
- can be. */\r
- buf = PyOS_double_to_string(val, (char)type, precision, flags,\r
- &float_type);\r
- if (buf == NULL)\r
- goto done;\r
- n_digits = strlen(buf);\r
-\r
- if (add_pct) {\r
- /* We know that buf has a trailing zero (since we just called\r
- strlen() on it), and we don't use that fact any more. So we\r
- can just write over the trailing zero. */\r
- buf[n_digits] = '%';\r
- n_digits += 1;\r
- }\r
-\r
- /* Since there is no unicode version of PyOS_double_to_string,\r
- just use the 8 bit version and then convert to unicode. */\r
-#if STRINGLIB_IS_UNICODE\r
- unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));\r
- if (unicode_tmp == NULL) {\r
- PyErr_NoMemory();\r
- goto done;\r
- }\r
- strtounicode(unicode_tmp, buf, n_digits);\r
- p = unicode_tmp;\r
-#else\r
- p = buf;\r
-#endif\r
-\r
- /* Is a sign character present in the output? If so, remember it\r
- and skip it */\r
- if (*p == '-') {\r
- sign_char = *p;\r
- ++p;\r
- --n_digits;\r
- }\r
-\r
- /* Determine if we have any "remainder" (after the digits, might include\r
- decimal or exponent or both (or neither)) */\r
- parse_number(p, n_digits, &n_remainder, &has_decimal);\r
-\r
- /* Determine the grouping, separator, and decimal point, if any. */\r
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r
- (format->thousands_separators ?\r
- LT_DEFAULT_LOCALE :\r
- LT_NO_LOCALE),\r
- &locale);\r
-\r
- /* Calculate how much memory we'll need. */\r
- n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,\r
- n_remainder, has_decimal, &locale, format);\r
-\r
- /* Allocate the memory. */\r
- result = STRINGLIB_NEW(NULL, n_total);\r
- if (result == NULL)\r
- goto done;\r
-\r
- /* Populate the memory. */\r
- fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,\r
- format->fill_char == '\0' ? ' ' : format->fill_char, &locale,\r
- 0);\r
-\r
-done:\r
- PyMem_Free(buf);\r
-#if STRINGLIB_IS_UNICODE\r
- PyMem_Free(unicode_tmp);\r
-#endif\r
- return result;\r
-}\r
-#endif /* FORMAT_FLOAT */\r
-\r
-/************************************************************************/\r
-/*********** complex formatting *****************************************/\r
-/************************************************************************/\r
-\r
-#ifdef FORMAT_COMPLEX\r
-\r
-static PyObject *\r
-format_complex_internal(PyObject *value,\r
- const InternalFormatSpec *format)\r
-{\r
- double re;\r
- double im;\r
- char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */\r
- char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */\r
-\r
- InternalFormatSpec tmp_format = *format;\r
- Py_ssize_t n_re_digits;\r
- Py_ssize_t n_im_digits;\r
- Py_ssize_t n_re_remainder;\r
- Py_ssize_t n_im_remainder;\r
- Py_ssize_t n_re_total;\r
- Py_ssize_t n_im_total;\r
- int re_has_decimal;\r
- int im_has_decimal;\r
- Py_ssize_t precision = format->precision;\r
- Py_ssize_t default_precision = 6;\r
- STRINGLIB_CHAR type = format->type;\r
- STRINGLIB_CHAR *p_re;\r
- STRINGLIB_CHAR *p_im;\r
- NumberFieldWidths re_spec;\r
- NumberFieldWidths im_spec;\r
- int flags = 0;\r
- PyObject *result = NULL;\r
- STRINGLIB_CHAR *p;\r
- STRINGLIB_CHAR re_sign_char = '\0';\r
- STRINGLIB_CHAR im_sign_char = '\0';\r
- int re_float_type; /* Used to see if we have a nan, inf, or regular float. */\r
- int im_float_type;\r
- int add_parens = 0;\r
- int skip_re = 0;\r
- Py_ssize_t lpad;\r
- Py_ssize_t rpad;\r
- Py_ssize_t total;\r
-\r
-#if STRINGLIB_IS_UNICODE\r
- Py_UNICODE *re_unicode_tmp = NULL;\r
- Py_UNICODE *im_unicode_tmp = NULL;\r
-#endif\r
-\r
- /* Locale settings, either from the actual locale or\r
- from a hard-code pseudo-locale */\r
- LocaleInfo locale;\r
-\r
- /* Alternate is not allowed on complex. */\r
- if (format->alternate) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Alternate form (#) not allowed in complex format "\r
- "specifier");\r
- goto done;\r
- }\r
-\r
- /* Neither is zero pading. */\r
- if (format->fill_char == '0') {\r
- PyErr_SetString(PyExc_ValueError,\r
- "Zero padding is not allowed in complex format "\r
- "specifier");\r
- goto done;\r
- }\r
-\r
- /* Neither is '=' alignment . */\r
- if (format->align == '=') {\r
- PyErr_SetString(PyExc_ValueError,\r
- "'=' alignment flag is not allowed in complex format "\r
- "specifier");\r
- goto done;\r
- }\r
-\r
- re = PyComplex_RealAsDouble(value);\r
- if (re == -1.0 && PyErr_Occurred())\r
- goto done;\r
- im = PyComplex_ImagAsDouble(value);\r
- if (im == -1.0 && PyErr_Occurred())\r
- goto done;\r
-\r
- if (type == '\0') {\r
- /* Omitted type specifier. Should be like str(self). */\r
- type = 'g';\r
- default_precision = PyFloat_STR_PRECISION;\r
- if (re == 0.0 && copysign(1.0, re) == 1.0)\r
- skip_re = 1;\r
- else\r
- add_parens = 1;\r
- }\r
-\r
- if (type == 'n')\r
- /* 'n' is the same as 'g', except for the locale used to\r
- format the result. We take care of that later. */\r
- type = 'g';\r
-\r
- if (precision < 0)\r
- precision = default_precision;\r
-\r
- /* Cast "type", because if we're in unicode we need to pass a\r
- 8-bit char. This is safe, because we've restricted what "type"\r
- can be. */\r
- re_buf = PyOS_double_to_string(re, (char)type, precision, flags,\r
- &re_float_type);\r
- if (re_buf == NULL)\r
- goto done;\r
- im_buf = PyOS_double_to_string(im, (char)type, precision, flags,\r
- &im_float_type);\r
- if (im_buf == NULL)\r
- goto done;\r
-\r
- n_re_digits = strlen(re_buf);\r
- n_im_digits = strlen(im_buf);\r
-\r
- /* Since there is no unicode version of PyOS_double_to_string,\r
- just use the 8 bit version and then convert to unicode. */\r
-#if STRINGLIB_IS_UNICODE\r
- re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));\r
- if (re_unicode_tmp == NULL) {\r
- PyErr_NoMemory();\r
- goto done;\r
- }\r
- strtounicode(re_unicode_tmp, re_buf, n_re_digits);\r
- p_re = re_unicode_tmp;\r
-\r
- im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));\r
- if (im_unicode_tmp == NULL) {\r
- PyErr_NoMemory();\r
- goto done;\r
- }\r
- strtounicode(im_unicode_tmp, im_buf, n_im_digits);\r
- p_im = im_unicode_tmp;\r
-#else\r
- p_re = re_buf;\r
- p_im = im_buf;\r
-#endif\r
-\r
- /* Is a sign character present in the output? If so, remember it\r
- and skip it */\r
- if (*p_re == '-') {\r
- re_sign_char = *p_re;\r
- ++p_re;\r
- --n_re_digits;\r
- }\r
- if (*p_im == '-') {\r
- im_sign_char = *p_im;\r
- ++p_im;\r
- --n_im_digits;\r
- }\r
-\r
- /* Determine if we have any "remainder" (after the digits, might include\r
- decimal or exponent or both (or neither)) */\r
- parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);\r
- parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);\r
-\r
- /* Determine the grouping, separator, and decimal point, if any. */\r
- get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :\r
- (format->thousands_separators ?\r
- LT_DEFAULT_LOCALE :\r
- LT_NO_LOCALE),\r
- &locale);\r
-\r
- /* Turn off any padding. We'll do it later after we've composed\r
- the numbers without padding. */\r
- tmp_format.fill_char = '\0';\r
- tmp_format.align = '<';\r
- tmp_format.width = -1;\r
-\r
- /* Calculate how much memory we'll need. */\r
- n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,\r
- n_re_digits, n_re_remainder,\r
- re_has_decimal, &locale, &tmp_format);\r
-\r
- /* Same formatting, but always include a sign, unless the real part is\r
- * going to be omitted, in which case we use whatever sign convention was\r
- * requested by the original format. */\r
- if (!skip_re)\r
- tmp_format.sign = '+';\r
- n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,\r
- n_im_digits, n_im_remainder,\r
- im_has_decimal, &locale, &tmp_format);\r
-\r
- if (skip_re)\r
- n_re_total = 0;\r
-\r
- /* Add 1 for the 'j', and optionally 2 for parens. */\r
- calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,\r
- format->width, format->align, &lpad, &rpad, &total);\r
-\r
- result = STRINGLIB_NEW(NULL, total);\r
- if (result == NULL)\r
- goto done;\r
-\r
- /* Populate the memory. First, the padding. */\r
- p = fill_padding(STRINGLIB_STR(result),\r
- n_re_total + n_im_total + 1 + add_parens * 2,\r
- format->fill_char=='\0' ? ' ' : format->fill_char,\r
- lpad, rpad);\r
-\r
- if (add_parens)\r
- *p++ = '(';\r
-\r
- if (!skip_re) {\r
- fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);\r
- p += n_re_total;\r
- }\r
- fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);\r
- p += n_im_total;\r
- *p++ = 'j';\r
-\r
- if (add_parens)\r
- *p++ = ')';\r
-\r
-done:\r
- PyMem_Free(re_buf);\r
- PyMem_Free(im_buf);\r
-#if STRINGLIB_IS_UNICODE\r
- PyMem_Free(re_unicode_tmp);\r
- PyMem_Free(im_unicode_tmp);\r
-#endif\r
- return result;\r
-}\r
-#endif /* FORMAT_COMPLEX */\r
-\r
-/************************************************************************/\r
-/*********** built in formatters ****************************************/\r
-/************************************************************************/\r
-PyObject *\r
-FORMAT_STRING(PyObject *obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len)\r
-{\r
- InternalFormatSpec format;\r
- PyObject *result = NULL;\r
-\r
- /* check for the special case of zero length format spec, make\r
- it equivalent to str(obj) */\r
- if (format_spec_len == 0) {\r
- result = STRINGLIB_TOSTR(obj);\r
- goto done;\r
- }\r
-\r
- /* parse the format_spec */\r
- if (!parse_internal_render_format_spec(format_spec, format_spec_len,\r
- &format, 's', '<'))\r
- goto done;\r
-\r
- /* type conversion? */\r
- switch (format.type) {\r
- case 's':\r
- /* no type conversion needed, already a string. do the formatting */\r
- result = format_string_internal(obj, &format);\r
- break;\r
- default:\r
- /* unknown */\r
- unknown_presentation_type(format.type, obj->ob_type->tp_name);\r
- goto done;\r
- }\r
-\r
-done:\r
- return result;\r
-}\r
-\r
-#if defined FORMAT_LONG || defined FORMAT_INT\r
-static PyObject*\r
-format_int_or_long(PyObject* obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len,\r
- IntOrLongToString tostring)\r
-{\r
- PyObject *result = NULL;\r
- PyObject *tmp = NULL;\r
- InternalFormatSpec format;\r
-\r
- /* check for the special case of zero length format spec, make\r
- it equivalent to str(obj) */\r
- if (format_spec_len == 0) {\r
- result = STRINGLIB_TOSTR(obj);\r
- goto done;\r
- }\r
-\r
- /* parse the format_spec */\r
- if (!parse_internal_render_format_spec(format_spec,\r
- format_spec_len,\r
- &format, 'd', '>'))\r
- goto done;\r
-\r
- /* type conversion? */\r
- switch (format.type) {\r
- case 'b':\r
- case 'c':\r
- case 'd':\r
- case 'o':\r
- case 'x':\r
- case 'X':\r
- case 'n':\r
- /* no type conversion needed, already an int (or long). do\r
- the formatting */\r
- result = format_int_or_long_internal(obj, &format, tostring);\r
- break;\r
-\r
- case 'e':\r
- case 'E':\r
- case 'f':\r
- case 'F':\r
- case 'g':\r
- case 'G':\r
- case '%':\r
- /* convert to float */\r
- tmp = PyNumber_Float(obj);\r
- if (tmp == NULL)\r
- goto done;\r
- result = format_float_internal(tmp, &format);\r
- break;\r
-\r
- default:\r
- /* unknown */\r
- unknown_presentation_type(format.type, obj->ob_type->tp_name);\r
- goto done;\r
- }\r
-\r
-done:\r
- Py_XDECREF(tmp);\r
- return result;\r
-}\r
-#endif /* FORMAT_LONG || defined FORMAT_INT */\r
-\r
-#ifdef FORMAT_LONG\r
-/* Need to define long_format as a function that will convert a long\r
- to a string. In 3.0, _PyLong_Format has the correct signature. In\r
- 2.x, we need to fudge a few parameters */\r
-#if PY_VERSION_HEX >= 0x03000000\r
-#define long_format _PyLong_Format\r
-#else\r
-static PyObject*\r
-long_format(PyObject* value, int base)\r
-{\r
- /* Convert to base, don't add trailing 'L', and use the new octal\r
- format. We already know this is a long object */\r
- assert(PyLong_Check(value));\r
- /* convert to base, don't add 'L', and use the new octal format */\r
- return _PyLong_Format(value, base, 0, 1);\r
-}\r
-#endif\r
-\r
-PyObject *\r
-FORMAT_LONG(PyObject *obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len)\r
-{\r
- return format_int_or_long(obj, format_spec, format_spec_len,\r
- long_format);\r
-}\r
-#endif /* FORMAT_LONG */\r
-\r
-#ifdef FORMAT_INT\r
-/* this is only used for 2.x, not 3.0 */\r
-static PyObject*\r
-int_format(PyObject* value, int base)\r
-{\r
- /* Convert to base, and use the new octal format. We already\r
- know this is an int object */\r
- assert(PyInt_Check(value));\r
- return _PyInt_Format((PyIntObject*)value, base, 1);\r
-}\r
-\r
-PyObject *\r
-FORMAT_INT(PyObject *obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len)\r
-{\r
- return format_int_or_long(obj, format_spec, format_spec_len,\r
- int_format);\r
-}\r
-#endif /* FORMAT_INT */\r
-\r
-#ifdef FORMAT_FLOAT\r
-PyObject *\r
-FORMAT_FLOAT(PyObject *obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len)\r
-{\r
- PyObject *result = NULL;\r
- InternalFormatSpec format;\r
-\r
- /* check for the special case of zero length format spec, make\r
- it equivalent to str(obj) */\r
- if (format_spec_len == 0) {\r
- result = STRINGLIB_TOSTR(obj);\r
- goto done;\r
- }\r
-\r
- /* parse the format_spec */\r
- if (!parse_internal_render_format_spec(format_spec,\r
- format_spec_len,\r
- &format, '\0', '>'))\r
- goto done;\r
-\r
- /* type conversion? */\r
- switch (format.type) {\r
- case '\0': /* No format code: like 'g', but with at least one decimal. */\r
- case 'e':\r
- case 'E':\r
- case 'f':\r
- case 'F':\r
- case 'g':\r
- case 'G':\r
- case 'n':\r
- case '%':\r
- /* no conversion, already a float. do the formatting */\r
- result = format_float_internal(obj, &format);\r
- break;\r
-\r
- default:\r
- /* unknown */\r
- unknown_presentation_type(format.type, obj->ob_type->tp_name);\r
- goto done;\r
- }\r
-\r
-done:\r
- return result;\r
-}\r
-#endif /* FORMAT_FLOAT */\r
-\r
-#ifdef FORMAT_COMPLEX\r
-PyObject *\r
-FORMAT_COMPLEX(PyObject *obj,\r
- STRINGLIB_CHAR *format_spec,\r
- Py_ssize_t format_spec_len)\r
-{\r
- PyObject *result = NULL;\r
- InternalFormatSpec format;\r
-\r
- /* check for the special case of zero length format spec, make\r
- it equivalent to str(obj) */\r
- if (format_spec_len == 0) {\r
- result = STRINGLIB_TOSTR(obj);\r
- goto done;\r
- }\r
-\r
- /* parse the format_spec */\r
- if (!parse_internal_render_format_spec(format_spec,\r
- format_spec_len,\r
- &format, '\0', '>'))\r
- goto done;\r
-\r
- /* type conversion? */\r
- switch (format.type) {\r
- case '\0': /* No format code: like 'g', but with at least one decimal. */\r
- case 'e':\r
- case 'E':\r
- case 'f':\r
- case 'F':\r
- case 'g':\r
- case 'G':\r
- case 'n':\r
- /* no conversion, already a complex. do the formatting */\r
- result = format_complex_internal(obj, &format);\r
- break;\r
-\r
- default:\r
- /* unknown */\r
- unknown_presentation_type(format.type, obj->ob_type->tp_name);\r
- goto done;\r
- }\r
-\r
-done:\r
- return result;\r
-}\r
-#endif /* FORMAT_COMPLEX */\r