| 1 |
.. highlight:: c |
| 2 |
|
| 3 |
.. _string-conversion: |
| 4 |
|
| 5 |
String conversion and formatting |
| 6 |
================================ |
| 7 |
|
| 8 |
Functions 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 |
| 24 |
functions :c:func:`snprintf` and :c:func:`vsnprintf`. Their purpose is to |
| 25 |
guarantee consistent behavior in corner cases, which the Standard C functions do |
| 26 |
not. |
| 27 |
|
| 28 |
The wrappers ensure that ``str[size-1]`` is always ``'\0'`` upon return. They |
| 29 |
never write more than *size* bytes (including the trailing ``'\0'``) into str. |
| 30 |
Both functions require that ``str != NULL``, ``size > 0``, ``format != NULL`` |
| 31 |
and ``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 |
|
| 34 |
The 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``, the output conversion failed and ``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 |
|
| 49 |
The following functions provide locale-independent string to number conversions. |
| 50 |
|
| 51 |
.. c:function:: unsigned long PyOS_strtoul(const char *str, char **ptr, int base) |
| 52 |
|
| 53 |
Convert the initial part of the string in ``str`` to an :c:expr:`unsigned |
| 54 |
long` value according to the given ``base``, which must be between ``2`` and |
| 55 |
``36`` inclusive, or be the special value ``0``. |
| 56 |
|
| 57 |
Leading white space and case of characters are ignored. If ``base`` is zero |
| 58 |
it looks for a leading ``0b``, ``0o`` or ``0x`` to tell which base. If |
| 59 |
these are absent it defaults to ``10``. Base must be 0 or between 2 and 36 |
| 60 |
(inclusive). If ``ptr`` is non-``NULL`` it will contain a pointer to the |
| 61 |
end of the scan. |
| 62 |
|
| 63 |
If the converted value falls out of range of corresponding return type, |
| 64 |
range error occurs (:c:data:`errno` is set to :c:macro:`!ERANGE`) and |
| 65 |
:c:macro:`!ULONG_MAX` is returned. If no conversion can be performed, ``0`` |
| 66 |
is returned. |
| 67 |
|
| 68 |
See also the Unix man page :manpage:`strtoul(3)`. |
| 69 |
|
| 70 |
.. versionadded:: 3.2 |
| 71 |
|
| 72 |
|
| 73 |
.. c:function:: long PyOS_strtol(const char *str, char **ptr, int base) |
| 74 |
|
| 75 |
Convert the initial part of the string in ``str`` to an :c:expr:`long` value |
| 76 |
according to the given ``base``, which must be between ``2`` and ``36`` |
| 77 |
inclusive, or be the special value ``0``. |
| 78 |
|
| 79 |
Same as :c:func:`PyOS_strtoul`, but return a :c:expr:`long` value instead |
| 80 |
and :c:macro:`LONG_MAX` on overflows. |
| 81 |
|
| 82 |
See also the Unix man page :manpage:`strtol(3)`. |
| 83 |
|
| 84 |
.. versionadded:: 3.2 |
| 85 |
|
| 86 |
|
| 87 |
.. c:function:: double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception) |
| 88 |
|
| 89 |
Convert a string ``s`` to a :c:expr:`double`, raising a Python |
| 90 |
exception on failure. The set of accepted strings corresponds to |
| 91 |
the set of strings accepted by Python's :func:`float` constructor, |
| 92 |
except that ``s`` must not have leading or trailing whitespace. |
| 93 |
The conversion is independent of the current locale. |
| 94 |
|
| 95 |
If ``endptr`` is ``NULL``, convert the whole string. Raise |
| 96 |
:exc:`ValueError` and return ``-1.0`` if the string is not a valid |
| 97 |
representation of a floating-point number. |
| 98 |
|
| 99 |
If endptr is not ``NULL``, convert as much of the string as |
| 100 |
possible and set ``*endptr`` to point to the first unconverted |
| 101 |
character. If no initial segment of the string is the valid |
| 102 |
representation of a floating-point number, set ``*endptr`` to point |
| 103 |
to the beginning of the string, raise ValueError, and return |
| 104 |
``-1.0``. |
| 105 |
|
| 106 |
If ``s`` represents a value that is too large to store in a float |
| 107 |
(for example, ``"1e500"`` is such a string on many platforms) then |
| 108 |
if ``overflow_exception`` is ``NULL`` return ``Py_INFINITY`` (with |
| 109 |
an appropriate sign) and don't set any exception. Otherwise, |
| 110 |
``overflow_exception`` must point to a Python exception object; |
| 111 |
raise that exception and return ``-1.0``. In both cases, set |
| 112 |
``*endptr`` to point to the first character after the converted value. |
| 113 |
|
| 114 |
If any other error occurs during the conversion (for example an |
| 115 |
out-of-memory error), set the appropriate Python exception and |
| 116 |
return ``-1.0``. |
| 117 |
|
| 118 |
.. versionadded:: 3.1 |
| 119 |
|
| 120 |
|
| 121 |
.. c:function:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype) |
| 122 |
|
| 123 |
Convert a :c:expr:`double` *val* to a string using supplied |
| 124 |
*format_code*, *precision*, and *flags*. |
| 125 |
|
| 126 |
*format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, |
| 127 |
``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision* |
| 128 |
must be 0 and is ignored. The ``'r'`` format code specifies the |
| 129 |
standard :func:`repr` format. |
| 130 |
|
| 131 |
*flags* can be zero or more of the following values or-ed together: |
| 132 |
|
| 133 |
.. c:namespace:: NULL |
| 134 |
|
| 135 |
.. c:macro:: Py_DTSF_SIGN |
| 136 |
|
| 137 |
Always precede the returned string with a sign |
| 138 |
character, even if *val* is non-negative. |
| 139 |
|
| 140 |
.. c:macro:: Py_DTSF_ADD_DOT_0 |
| 141 |
|
| 142 |
Ensure that the returned string will not look like an integer. |
| 143 |
|
| 144 |
.. c:macro:: Py_DTSF_ALT |
| 145 |
|
| 146 |
Apply "alternate" formatting rules. |
| 147 |
See the documentation for the :c:func:`PyOS_snprintf` ``'#'`` specifier for |
| 148 |
details. |
| 149 |
|
| 150 |
.. c:macro:: Py_DTSF_NO_NEG_0 |
| 151 |
|
| 152 |
Negative zero is converted to positive zero. |
| 153 |
|
| 154 |
.. versionadded:: 3.11 |
| 155 |
|
| 156 |
If *ptype* is non-``NULL``, then the value it points to will be set to one |
| 157 |
of the following constants depending on the type of *val*: |
| 158 |
|
| 159 |
.. list-table:: |
| 160 |
:header-rows: 1 |
| 161 |
:align: left |
| 162 |
|
| 163 |
* - *\*ptype* |
| 164 |
- type of *val* |
| 165 |
* - .. c:macro:: Py_DTST_FINITE |
| 166 |
- finite number |
| 167 |
* - .. c:macro:: Py_DTST_INFINITE |
| 168 |
- infinite number |
| 169 |
* - .. c:macro:: Py_DTST_NAN |
| 170 |
- not a number |
| 171 |
|
| 172 |
The return value is a pointer to *buffer* with the converted string or |
| 173 |
``NULL`` if the conversion failed. The caller is responsible for freeing the |
| 174 |
returned string by calling :c:func:`PyMem_Free`. |
| 175 |
|
| 176 |
.. versionadded:: 3.1 |
| 177 |
|
| 178 |
|
| 179 |
.. c:function:: int PyOS_mystricmp(const char *str1, const char *str2) |
| 180 |
int PyOS_mystrnicmp(const char *str1, const char *str2, Py_ssize_t size) |
| 181 |
|
| 182 |
Case insensitive comparison of strings. These functions work almost |
| 183 |
identically to :c:func:`!strcmp` and :c:func:`!strncmp` (respectively), |
| 184 |
except that they ignore the case of ASCII characters. |
| 185 |
|
| 186 |
Return ``0`` if the strings are equal, a negative value if *str1* sorts |
| 187 |
lexicographically before *str2*, or a positive value if it sorts after. |
| 188 |
|
| 189 |
In the *str1* or *str2* arguments, a NUL byte marks the end of the string. |
| 190 |
For :c:func:`!PyOS_mystrnicmp`, the *size* argument gives the maximum size |
| 191 |
of the string, as if NUL was present at the index given by *size*. |
| 192 |
|
| 193 |
These functions do not use the locale. |
| 194 |
|
| 195 |
|
| 196 |
.. c:function:: int PyOS_stricmp(const char *str1, const char *str2) |
| 197 |
int PyOS_strnicmp(const char *str1, const char *str2, Py_ssize_t size) |
| 198 |
|
| 199 |
Case insensitive comparison of strings. |
| 200 |
|
| 201 |
On Windows, these are aliases of :c:func:`!stricmp` and :c:func:`!strnicmp`, |
| 202 |
respectively. |
| 203 |
|
| 204 |
On other platforms, they are aliases of :c:func:`PyOS_mystricmp` and |
| 205 |
:c:func:`PyOS_mystrnicmp`, respectively. |
| 206 |
|
| 207 |
|
| 208 |
Character classification and conversion |
| 209 |
======================================= |
| 210 |
|
| 211 |
The following macros provide locale-independent (unlike the C standard library |
| 212 |
``ctype.h``) character classification and conversion. |
| 213 |
The argument must be a signed or unsigned :c:expr:`char`. |
| 214 |
|
| 215 |
|
| 216 |
.. c:macro:: Py_ISALNUM(c) |
| 217 |
|
| 218 |
Return true if the character *c* is an alphanumeric character. |
| 219 |
|
| 220 |
|
| 221 |
.. c:macro:: Py_ISALPHA(c) |
| 222 |
|
| 223 |
Return true if the character *c* is an alphabetic character (``a-z`` and ``A-Z``). |
| 224 |
|
| 225 |
|
| 226 |
.. c:macro:: Py_ISDIGIT(c) |
| 227 |
|
| 228 |
Return true if the character *c* is a decimal digit (``0-9``). |
| 229 |
|
| 230 |
|
| 231 |
.. c:macro:: Py_ISLOWER(c) |
| 232 |
|
| 233 |
Return true if the character *c* is a lowercase ASCII letter (``a-z``). |
| 234 |
|
| 235 |
|
| 236 |
.. c:macro:: Py_ISUPPER(c) |
| 237 |
|
| 238 |
Return true if the character *c* is an uppercase ASCII letter (``A-Z``). |
| 239 |
|
| 240 |
|
| 241 |
.. c:macro:: Py_ISSPACE(c) |
| 242 |
|
| 243 |
Return true if the character *c* is a whitespace character (space, tab, |
| 244 |
carriage return, newline, vertical tab, or form feed). |
| 245 |
|
| 246 |
|
| 247 |
.. c:macro:: Py_ISXDIGIT(c) |
| 248 |
|
| 249 |
Return true if the character *c* is a hexadecimal digit (``0-9``, ``a-f``, and |
| 250 |
``A-F``). |
| 251 |
|
| 252 |
|
| 253 |
.. c:macro:: Py_TOLOWER(c) |
| 254 |
|
| 255 |
Return the lowercase equivalent of the character *c*. |
| 256 |
|
| 257 |
|
| 258 |
.. c:macro:: Py_TOUPPER(c) |
| 259 |
|
| 260 |
Return the uppercase equivalent of the character *c*. |