1.. highlight:: c 2 3.. _string-conversion: 4 5String conversion and formatting 6================================ 7 8Functions for number conversion and formatted string output. 9 10 11.. c:function:: int PyOS_snprintf(char *str, size_t size, const char *format, ...) 12 13 Output not more than *size* bytes to *str* according to the format string 14 *format* and the extra arguments. See the Unix man page :manpage:`snprintf(3)`. 15 16 17.. c:function:: int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) 18 19 Output not more than *size* bytes to *str* according to the format string 20 *format* and the variable argument list *va*. Unix man page 21 :manpage:`vsnprintf(3)`. 22 23:c:func:`PyOS_snprintf` and :c:func:`PyOS_vsnprintf` wrap the Standard C library 24functions :c:func:`snprintf` and :c:func:`vsnprintf`. Their purpose is to 25guarantee consistent behavior in corner cases, which the Standard C functions do 26not. 27 28The wrappers ensure that ``str[size-1]`` is always ``'\0'`` upon return. They 29never write more than *size* bytes (including the trailing ``'\0'``) into str. 30Both functions require that ``str != NULL``, ``size > 0``, ``format != NULL`` 31and ``size < INT_MAX``. Note that this means there is no equivalent to the C99 32``n = snprintf(NULL, 0, ...)`` which would determine the necessary buffer size. 33 34The return value (*rv*) for these functions should be interpreted as follows: 35 36* When ``0 <= rv < size``, the output conversion was successful and *rv* 37 characters were written to *str* (excluding the trailing ``'\0'`` byte at 38 ``str[rv]``). 39 40* When ``rv >= size``, the output conversion was truncated and a buffer with 41 ``rv + 1`` bytes would have been needed to succeed. ``str[size-1]`` is ``'\0'`` 42 in this case. 43 44* When ``rv < 0``, "something bad happened." ``str[size-1]`` is ``'\0'`` in 45 this case too, but the rest of *str* is undefined. The exact cause of the error 46 depends on the underlying platform. 47 48 49The following functions provide locale-independent string to number conversions. 50 51.. c:function:: double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception) 52 53 Convert a string ``s`` to a :c:expr:`double`, raising a Python 54 exception on failure. The set of accepted strings corresponds to 55 the set of strings accepted by Python's :func:`float` constructor, 56 except that ``s`` must not have leading or trailing whitespace. 57 The conversion is independent of the current locale. 58 59 If ``endptr`` is ``NULL``, convert the whole string. Raise 60 :exc:`ValueError` and return ``-1.0`` if the string is not a valid 61 representation of a floating-point number. 62 63 If endptr is not ``NULL``, convert as much of the string as 64 possible and set ``*endptr`` to point to the first unconverted 65 character. If no initial segment of the string is the valid 66 representation of a floating-point number, set ``*endptr`` to point 67 to the beginning of the string, raise ValueError, and return 68 ``-1.0``. 69 70 If ``s`` represents a value that is too large to store in a float 71 (for example, ``"1e500"`` is such a string on many platforms) then 72 if ``overflow_exception`` is ``NULL`` return ``Py_HUGE_VAL`` (with 73 an appropriate sign) and don't set any exception. Otherwise, 74 ``overflow_exception`` must point to a Python exception object; 75 raise that exception and return ``-1.0``. In both cases, set 76 ``*endptr`` to point to the first character after the converted value. 77 78 If any other error occurs during the conversion (for example an 79 out-of-memory error), set the appropriate Python exception and 80 return ``-1.0``. 81 82 .. versionadded:: 3.1 83 84 85.. c:function:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype) 86 87 Convert a :c:expr:`double` *val* to a string using supplied 88 *format_code*, *precision*, and *flags*. 89 90 *format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, 91 ``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision* 92 must be 0 and is ignored. The ``'r'`` format code specifies the 93 standard :func:`repr` format. 94 95 *flags* can be zero or more of the values ``Py_DTSF_SIGN``, 96 ``Py_DTSF_ADD_DOT_0``, or ``Py_DTSF_ALT``, or-ed together: 97 98 * ``Py_DTSF_SIGN`` means to always precede the returned string with a sign 99 character, even if *val* is non-negative. 100 101 * ``Py_DTSF_ADD_DOT_0`` means to ensure that the returned string will not look 102 like an integer. 103 104 * ``Py_DTSF_ALT`` means to apply "alternate" formatting rules. See the 105 documentation for the :c:func:`PyOS_snprintf` ``'#'`` specifier for 106 details. 107 108 If *ptype* is non-``NULL``, then the value it points to will be set to one of 109 ``Py_DTST_FINITE``, ``Py_DTST_INFINITE``, or ``Py_DTST_NAN``, signifying that 110 *val* is a finite number, an infinite number, or not a number, respectively. 111 112 The return value is a pointer to *buffer* with the converted string or 113 ``NULL`` if the conversion failed. The caller is responsible for freeing the 114 returned string by calling :c:func:`PyMem_Free`. 115 116 .. versionadded:: 3.1 117 118 119.. c:function:: int PyOS_stricmp(const char *s1, const char *s2) 120 121 Case insensitive comparison of strings. The function works almost 122 identically to :c:func:`strcmp` except that it ignores the case. 123 124 125.. c:function:: int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size) 126 127 Case insensitive comparison of strings. The function works almost 128 identically to :c:func:`strncmp` except that it ignores the case. 129