blob: efbaa52e2dc46681540ce49df9eaf789db3941e3 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _string-conversion:
4
5String conversion and formatting
6================================
7
8Functions for number conversion and formatted string output.
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:function:: int PyOS_snprintf(char *str, size_t size, const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
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(2)`.
15
16
Georg Brandl60203b42010-10-06 10:11:56 +000017.. c:function:: int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
Georg Brandl54a3faa2008-01-20 09:30:57 +000018
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(2)`.
22
Georg Brandl60203b42010-10-06 10:11:56 +000023: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
Georg Brandl54a3faa2008-01-20 09:30:57 +000025guarantee 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.
Victor Stinner7ab92d52020-06-16 00:54:44 +020030Both functions require that ``str != NULL``, ``size > 0``, ``format != NULL``
31and ``size < INT_MAX``.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33The return value (*rv*) for these functions should be interpreted as follows:
34
35* When ``0 <= rv < size``, the output conversion was successful and *rv*
36 characters were written to *str* (excluding the trailing ``'\0'`` byte at
37 *str*[*rv*]).
38
39* When ``rv >= size``, the output conversion was truncated and a buffer with
40 ``rv + 1`` bytes would have been needed to succeed. *str*[*size*-1] is ``'\0'``
41 in this case.
42
43* When ``rv < 0``, "something bad happened." *str*[*size*-1] is ``'\0'`` in
44 this case too, but the rest of *str* is undefined. The exact cause of the error
45 depends on the underlying platform.
46
Georg Brandl54a3faa2008-01-20 09:30:57 +000047
Victor Stinner7ab92d52020-06-16 00:54:44 +020048The following functions provide locale-independent string to number conversions.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
Georg Brandl60203b42010-10-06 10:11:56 +000050.. c:function:: double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
Mark Dickinson725bfd82009-05-03 20:33:40 +000051
Georg Brandl60203b42010-10-06 10:11:56 +000052 Convert a string ``s`` to a :c:type:`double`, raising a Python
Mark Dickinson725bfd82009-05-03 20:33:40 +000053 exception on failure. The set of accepted strings corresponds to
54 the set of strings accepted by Python's :func:`float` constructor,
55 except that ``s`` must not have leading or trailing whitespace.
56 The conversion is independent of the current locale.
57
58 If ``endptr`` is ``NULL``, convert the whole string. Raise
Stéphane Wirtele483f022018-10-26 12:52:11 +020059 :exc:`ValueError` and return ``-1.0`` if the string is not a valid
Mark Dickinson725bfd82009-05-03 20:33:40 +000060 representation of a floating-point number.
61
62 If endptr is not ``NULL``, convert as much of the string as
63 possible and set ``*endptr`` to point to the first unconverted
64 character. If no initial segment of the string is the valid
65 representation of a floating-point number, set ``*endptr`` to point
66 to the beginning of the string, raise ValueError, and return
67 ``-1.0``.
68
69 If ``s`` represents a value that is too large to store in a float
70 (for example, ``"1e500"`` is such a string on many platforms) then
71 if ``overflow_exception`` is ``NULL`` return ``Py_HUGE_VAL`` (with
72 an appropriate sign) and don't set any exception. Otherwise,
73 ``overflow_exception`` must point to a Python exception object;
74 raise that exception and return ``-1.0``. In both cases, set
75 ``*endptr`` to point to the first character after the converted value.
76
77 If any other error occurs during the conversion (for example an
78 out-of-memory error), set the appropriate Python exception and
79 return ``-1.0``.
80
81 .. versionadded:: 3.1
82
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
Georg Brandl60203b42010-10-06 10:11:56 +000084.. c:function:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
Eric Smithcc32a112009-04-26 21:35:14 +000085
Georg Brandl60203b42010-10-06 10:11:56 +000086 Convert a :c:type:`double` *val* to a string using supplied
Eric Smithcc32a112009-04-26 21:35:14 +000087 *format_code*, *precision*, and *flags*.
88
Eric Smith63376222009-05-05 14:04:18 +000089 *format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``,
90 ``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision*
91 must be 0 and is ignored. The ``'r'`` format code specifies the
92 standard :func:`repr` format.
Eric Smithcc32a112009-04-26 21:35:14 +000093
Serhiy Storchakae835b312019-10-30 21:37:16 +020094 *flags* can be zero or more of the values ``Py_DTSF_SIGN``,
95 ``Py_DTSF_ADD_DOT_0``, or ``Py_DTSF_ALT``, or-ed together:
Eric Smithcc32a112009-04-26 21:35:14 +000096
Serhiy Storchakae835b312019-10-30 21:37:16 +020097 * ``Py_DTSF_SIGN`` means to always precede the returned string with a sign
Eric Smithcc32a112009-04-26 21:35:14 +000098 character, even if *val* is non-negative.
99
Serhiy Storchakae835b312019-10-30 21:37:16 +0200100 * ``Py_DTSF_ADD_DOT_0`` means to ensure that the returned string will not look
Eric Smithcc32a112009-04-26 21:35:14 +0000101 like an integer.
102
Serhiy Storchakae835b312019-10-30 21:37:16 +0200103 * ``Py_DTSF_ALT`` means to apply "alternate" formatting rules. See the
Georg Brandl60203b42010-10-06 10:11:56 +0000104 documentation for the :c:func:`PyOS_snprintf` ``'#'`` specifier for
Eric Smithcc32a112009-04-26 21:35:14 +0000105 details.
106
Serhiy Storchakae835b312019-10-30 21:37:16 +0200107 If *ptype* is non-``NULL``, then the value it points to will be set to one of
108 ``Py_DTST_FINITE``, ``Py_DTST_INFINITE``, or ``Py_DTST_NAN``, signifying that
Eric Smithcc32a112009-04-26 21:35:14 +0000109 *val* is a finite number, an infinite number, or not a number, respectively.
110
111 The return value is a pointer to *buffer* with the converted string or
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200112 ``NULL`` if the conversion failed. The caller is responsible for freeing the
Georg Brandl60203b42010-10-06 10:11:56 +0000113 returned string by calling :c:func:`PyMem_Free`.
Eric Smithcc32a112009-04-26 21:35:14 +0000114
115 .. versionadded:: 3.1
116
Georg Brandl54a3faa2008-01-20 09:30:57 +0000117
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300118.. c:function:: int PyOS_stricmp(const char *s1, const char *s2)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
Georg Brandl2ee470f2008-07-16 12:55:28 +0000120 Case insensitive comparison of strings. The function works almost
Georg Brandl60203b42010-10-06 10:11:56 +0000121 identically to :c:func:`strcmp` except that it ignores the case.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
123
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300124.. c:function:: int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000125
Georg Brandl2ee470f2008-07-16 12:55:28 +0000126 Case insensitive comparison of strings. The function works almost
Georg Brandl60203b42010-10-06 10:11:56 +0000127 identically to :c:func:`strncmp` except that it ignores the case.