blob: b987469afbdff62bd2dcfd9ac558713e1831fef2 [file] [log] [blame]
Georg Brandl79e3d552008-01-19 22:14:27 +00001.. highlightlang:: c
2
3.. _string-conversion:
4
5String conversion and formatting
6================================
7
8Functions for number conversion and formatted string output.
9
10
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011.. c:function:: int PyOS_snprintf(char *str, size_t size, const char *format, ...)
Georg Brandl79e3d552008-01-19 22:14:27 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010017.. c:function:: int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
Georg Brandl79e3d552008-01-19 22:14:27 +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
Sandro Tosi98ed08f2012-01-14 16:42:02 +010023: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 Brandl79e3d552008-01-19 22:14:27 +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.
30Both functions require that ``str != NULL``, ``size > 0`` and ``format !=
31NULL``.
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033If the platform doesn't have :c:func:`vsnprintf` and the buffer size needed to
Georg Brandl79e3d552008-01-19 22:14:27 +000034avoid truncation exceeds *size* by more than 512 bytes, Python aborts with a
35*Py_FatalError*.
36
37The return value (*rv*) for these functions should be interpreted as follows:
38
39* When ``0 <= rv < size``, the output conversion was successful and *rv*
40 characters were written to *str* (excluding the trailing ``'\0'`` byte at
41 *str*[*rv*]).
42
43* When ``rv >= size``, the output conversion was truncated and a buffer with
44 ``rv + 1`` bytes would have been needed to succeed. *str*[*size*-1] is ``'\0'``
45 in this case.
46
47* When ``rv < 0``, "something bad happened." *str*[*size*-1] is ``'\0'`` in
48 this case too, but the rest of *str* is undefined. The exact cause of the error
49 depends on the underlying platform.
50
51The following functions provide locale-independent string to number conversions.
52
53
Sandro Tosi98ed08f2012-01-14 16:42:02 +010054.. c:function:: double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
Mark Dickinson09823a22009-10-31 09:42:39 +000055
Sandro Tosi98ed08f2012-01-14 16:42:02 +010056 Convert a string ``s`` to a :c:type:`double`, raising a Python
Mark Dickinson09823a22009-10-31 09:42:39 +000057 exception on failure. The set of accepted strings corresponds to
58 the set of strings accepted by Python's :func:`float` constructor,
59 except that ``s`` must not have leading or trailing whitespace.
60 The conversion is independent of the current locale.
61
62 If ``endptr`` is ``NULL``, convert the whole string. Raise
63 ValueError and return ``-1.0`` if the string is not a valid
64 representation of a floating-point number.
65
66 If endptr is not ``NULL``, convert as much of the string as
67 possible and set ``*endptr`` to point to the first unconverted
68 character. If no initial segment of the string is the valid
69 representation of a floating-point number, set ``*endptr`` to point
70 to the beginning of the string, raise ValueError, and return
71 ``-1.0``.
72
73 If ``s`` represents a value that is too large to store in a float
74 (for example, ``"1e500"`` is such a string on many platforms) then
75 if ``overflow_exception`` is ``NULL`` return ``Py_HUGE_VAL`` (with
76 an appropriate sign) and don't set any exception. Otherwise,
77 ``overflow_exception`` must point to a Python exception object;
78 raise that exception and return ``-1.0``. In both cases, set
79 ``*endptr`` to point to the first character after the converted value.
80
81 If any other error occurs during the conversion (for example an
82 out-of-memory error), set the appropriate Python exception and
83 return ``-1.0``.
84
85 .. versionadded:: 2.7
86
87
Sandro Tosi98ed08f2012-01-14 16:42:02 +010088.. c:function:: double PyOS_ascii_strtod(const char *nptr, char **endptr)
Georg Brandl79e3d552008-01-19 22:14:27 +000089
Sandro Tosi98ed08f2012-01-14 16:42:02 +010090 Convert a string to a :c:type:`double`. This function behaves like the Standard C
91 function :c:func:`strtod` does in the C locale. It does this without changing the
Georg Brandl79e3d552008-01-19 22:14:27 +000092 current locale, since that would not be thread-safe.
93
Sandro Tosi98ed08f2012-01-14 16:42:02 +010094 :c:func:`PyOS_ascii_strtod` should typically be used for reading configuration
Georg Brandl79e3d552008-01-19 22:14:27 +000095 files or other non-user input that should be locale independent.
96
Mark Dickinson09823a22009-10-31 09:42:39 +000097 See the Unix man page :manpage:`strtod(2)` for details.
98
Georg Brandl79e3d552008-01-19 22:14:27 +000099 .. versionadded:: 2.4
100
Mark Dickinson09823a22009-10-31 09:42:39 +0000101 .. deprecated:: 2.7
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100102 Use :c:func:`PyOS_string_to_double` instead.
Mark Dickinson09823a22009-10-31 09:42:39 +0000103
Georg Brandl79e3d552008-01-19 22:14:27 +0000104
105
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100106.. c:function:: char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)
Georg Brandl79e3d552008-01-19 22:14:27 +0000107
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100108 Convert a :c:type:`double` to a string using the ``'.'`` as the decimal
109 separator. *format* is a :c:func:`printf`\ -style format string specifying the
Georg Brandl79e3d552008-01-19 22:14:27 +0000110 number format. Allowed conversion characters are ``'e'``, ``'E'``, ``'f'``,
111 ``'F'``, ``'g'`` and ``'G'``.
112
113 The return value is a pointer to *buffer* with the converted string or NULL if
114 the conversion failed.
115
116 .. versionadded:: 2.4
Eric Smith068f0652009-04-25 21:40:15 +0000117 .. deprecated:: 2.7
118 This function is removed in Python 2.7 and 3.1. Use :func:`PyOS_double_to_string`
119 instead.
Georg Brandl79e3d552008-01-19 22:14:27 +0000120
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000121
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100122.. c:function:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
Eric Smith068f0652009-04-25 21:40:15 +0000123
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100124 Convert a :c:type:`double` *val* to a string using supplied
Eric Smith068f0652009-04-25 21:40:15 +0000125 *format_code*, *precision*, and *flags*.
126
Eric Smitha985a3a2009-05-05 18:26:08 +0000127 *format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``,
128 ``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision*
129 must be 0 and is ignored. The ``'r'`` format code specifies the
130 standard :func:`repr` format.
Eric Smith068f0652009-04-25 21:40:15 +0000131
132 *flags* can be zero or more of the values *Py_DTSF_SIGN*,
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000133 *Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together:
Eric Smith068f0652009-04-25 21:40:15 +0000134
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000135 * *Py_DTSF_SIGN* means to always precede the returned string with a sign
136 character, even if *val* is non-negative.
Eric Smith068f0652009-04-25 21:40:15 +0000137
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000138 * *Py_DTSF_ADD_DOT_0* means to ensure that the returned string will not look
139 like an integer.
Eric Smith068f0652009-04-25 21:40:15 +0000140
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000141 * *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100142 documentation for the :c:func:`PyOS_snprintf` ``'#'`` specifier for
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000143 details.
Eric Smith068f0652009-04-25 21:40:15 +0000144
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000145 If *ptype* is non-NULL, then the value it points to will be set to one of
146 *Py_DTST_FINITE*, *Py_DTST_INFINITE*, or *Py_DTST_NAN*, signifying that
147 *val* is a finite number, an infinite number, or not a number, respectively.
Eric Smith068f0652009-04-25 21:40:15 +0000148
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000149 The return value is a pointer to *buffer* with the converted string or
Eric Smithf254aab2009-04-26 10:05:11 +0000150 *NULL* if the conversion failed. The caller is responsible for freeing the
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100151 returned string by calling :c:func:`PyMem_Free`.
Eric Smith068f0652009-04-25 21:40:15 +0000152
153 .. versionadded:: 2.7
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000154
Georg Brandlafb0d6e2009-04-26 06:01:04 +0000155
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100156.. c:function:: double PyOS_ascii_atof(const char *nptr)
Georg Brandl79e3d552008-01-19 22:14:27 +0000157
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100158 Convert a string to a :c:type:`double` in a locale-independent way.
Georg Brandl79e3d552008-01-19 22:14:27 +0000159
Mark Dickinson09823a22009-10-31 09:42:39 +0000160 See the Unix man page :manpage:`atof(2)` for details.
161
Georg Brandl79e3d552008-01-19 22:14:27 +0000162 .. versionadded:: 2.4
163
Mark Dickinson09823a22009-10-31 09:42:39 +0000164 .. deprecated:: 3.1
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100165 Use :c:func:`PyOS_string_to_double` instead.
Georg Brandl79e3d552008-01-19 22:14:27 +0000166
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000167
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100168.. c:function:: char* PyOS_stricmp(char *s1, char *s2)
Georg Brandl79e3d552008-01-19 22:14:27 +0000169
Benjamin Peterson90f36732008-07-12 20:16:19 +0000170 Case insensitive comparison of strings. The function works almost
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100171 identically to :c:func:`strcmp` except that it ignores the case.
Georg Brandl79e3d552008-01-19 22:14:27 +0000172
173 .. versionadded:: 2.6
174
175
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100176.. c:function:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size)
Georg Brandl79e3d552008-01-19 22:14:27 +0000177
Benjamin Peterson90f36732008-07-12 20:16:19 +0000178 Case insensitive comparison of strings. The function works almost
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100179 identically to :c:func:`strncmp` except that it ignores the case.
Georg Brandl79e3d552008-01-19 22:14:27 +0000180
181 .. versionadded:: 2.6