blob: 52fe197465502bd89a37b3c4094aaa62ba996d64 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _string-conversion:
4
5String conversion and formatting
6================================
7
8Functions for number conversion and formatted string output.
9
10
11.. cfunction:: 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(2)`.
15
16
17.. cfunction:: 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(2)`.
22
23:cfunc:`PyOS_snprintf` and :cfunc:`PyOS_vsnprintf` wrap the Standard C library
24functions :cfunc:`snprintf` and :cfunc:`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`` and ``format !=
31NULL``.
32
33If the platform doesn't have :cfunc:`vsnprintf` and the buffer size needed to
34avoid 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
54.. cfunction:: double PyOS_ascii_strtod(const char *nptr, char **endptr)
55
56 Convert a string to a :ctype:`double`. This function behaves like the Standard C
57 function :cfunc:`strtod` does in the C locale. It does this without changing the
58 current locale, since that would not be thread-safe.
59
60 :cfunc:`PyOS_ascii_strtod` should typically be used for reading configuration
61 files or other non-user input that should be locale independent.
62
63 See the Unix man page :manpage:`strtod(2)` for details.
64
65
Georg Brandl57e1b502009-04-27 16:54:22 +000066.. cfunction:: char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
68 Convert a :ctype:`double` to a string using the ``'.'`` as the decimal
69 separator. *format* is a :cfunc:`printf`\ -style format string specifying the
70 number format. Allowed conversion characters are ``'e'``, ``'E'``, ``'f'``,
71 ``'F'``, ``'g'`` and ``'G'``.
72
73 The return value is a pointer to *buffer* with the converted string or NULL if
74 the conversion failed.
75
Eric Smithcc32a112009-04-26 21:35:14 +000076 .. deprecated:: 3.1
77 Use :cfunc:`PyOS_double_to_string` instead.
78
79
80.. cfunction:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
81
82 Convert a :ctype:`double` *val* to a string using supplied
83 *format_code*, *precision*, and *flags*.
84
85 *format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, ``'g'``,
86 ``'G'``, ``'s'``, or ``'r'``. For ``'s'`` and ``'r'``, the supplied
87 *precision* must be 0 and is ignored. These specify the standard
88 :func:`str` and :func:`repr` formats, respectively.
89
90 *flags* can be zero or more of the values *Py_DTSF_SIGN*,
91 *Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together:
92
93 * *Py_DTSF_SIGN* means to always precede the returned string with a sign
94 character, even if *val* is non-negative.
95
96 * *Py_DTSF_ADD_DOT_0* means to ensure that the returned string will not look
97 like an integer.
98
99 * *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the
100 documentation for the :cfunc:`PyOS_snprintf` ``'#'`` specifier for
101 details.
102
103 If *ptype* is non-NULL, then the value it points to will be set to one of
104 *Py_DTST_FINITE*, *Py_DTST_INFINITE*, or *Py_DTST_NAN*, signifying that
105 *val* is a finite number, an infinite number, or not a number, respectively.
106
107 The return value is a pointer to *buffer* with the converted string or
108 *NULL* if the conversion failed. The caller is responsible for freeing the
109 returned string by calling :cfunc:`PyMem_Free`.
110
111 .. versionadded:: 3.1
112
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
114.. cfunction:: double PyOS_ascii_atof(const char *nptr)
115
116 Convert a string to a :ctype:`double` in a locale-independent way.
117
118 See the Unix man page :manpage:`atof(2)` for details.
119
Georg Brandl48310cd2009-01-03 21:18:54 +0000120
Georg Brandl57e1b502009-04-27 16:54:22 +0000121.. cfunction:: char* PyOS_stricmp(char *s1, char *s2)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
Georg Brandl2ee470f2008-07-16 12:55:28 +0000123 Case insensitive comparison of strings. The function works almost
124 identically to :cfunc:`strcmp` except that it ignores the case.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000125
126
Georg Brandl57e1b502009-04-27 16:54:22 +0000127.. cfunction:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000128
Georg Brandl2ee470f2008-07-16 12:55:28 +0000129 Case insensitive comparison of strings. The function works almost
130 identically to :cfunc:`strncmp` except that it ignores the case.