Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: 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 | .. 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 |
| 24 | functions :cfunc:`snprintf` and :cfunc:`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`` and ``format != |
| 31 | NULL``. |
| 32 | |
| 33 | If the platform doesn't have :cfunc:`vsnprintf` and the buffer size needed to |
| 34 | avoid truncation exceeds *size* by more than 512 bytes, Python aborts with a |
| 35 | *Py_FatalError*. |
| 36 | |
| 37 | The 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 | |
| 51 | The 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 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 65 | .. deprecated:: 3.1 |
| 66 | Use :cfunc:`PyOS_string_to_double` instead. |
| 67 | |
| 68 | |
| 69 | .. cfunction:: double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception) |
| 70 | |
| 71 | Convert a string ``s`` to a :ctype:`double`, raising a Python |
| 72 | exception on failure. The set of accepted strings corresponds to |
| 73 | the set of strings accepted by Python's :func:`float` constructor, |
| 74 | except that ``s`` must not have leading or trailing whitespace. |
| 75 | The conversion is independent of the current locale. |
| 76 | |
| 77 | If ``endptr`` is ``NULL``, convert the whole string. Raise |
| 78 | ValueError and return ``-1.0`` if the string is not a valid |
| 79 | representation of a floating-point number. |
| 80 | |
| 81 | If endptr is not ``NULL``, convert as much of the string as |
| 82 | possible and set ``*endptr`` to point to the first unconverted |
| 83 | character. If no initial segment of the string is the valid |
| 84 | representation of a floating-point number, set ``*endptr`` to point |
| 85 | to the beginning of the string, raise ValueError, and return |
| 86 | ``-1.0``. |
| 87 | |
| 88 | If ``s`` represents a value that is too large to store in a float |
| 89 | (for example, ``"1e500"`` is such a string on many platforms) then |
| 90 | if ``overflow_exception`` is ``NULL`` return ``Py_HUGE_VAL`` (with |
| 91 | an appropriate sign) and don't set any exception. Otherwise, |
| 92 | ``overflow_exception`` must point to a Python exception object; |
| 93 | raise that exception and return ``-1.0``. In both cases, set |
| 94 | ``*endptr`` to point to the first character after the converted value. |
| 95 | |
| 96 | If any other error occurs during the conversion (for example an |
| 97 | out-of-memory error), set the appropriate Python exception and |
| 98 | return ``-1.0``. |
| 99 | |
| 100 | .. versionadded:: 3.1 |
| 101 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 57e1b50 | 2009-04-27 16:54:22 +0000 | [diff] [blame] | 103 | .. cfunction:: char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 104 | |
| 105 | Convert a :ctype:`double` to a string using the ``'.'`` as the decimal |
| 106 | separator. *format* is a :cfunc:`printf`\ -style format string specifying the |
| 107 | number format. Allowed conversion characters are ``'e'``, ``'E'``, ``'f'``, |
| 108 | ``'F'``, ``'g'`` and ``'G'``. |
| 109 | |
| 110 | The return value is a pointer to *buffer* with the converted string or NULL if |
| 111 | the conversion failed. |
| 112 | |
Eric Smith | cc32a11 | 2009-04-26 21:35:14 +0000 | [diff] [blame] | 113 | .. deprecated:: 3.1 |
| 114 | Use :cfunc:`PyOS_double_to_string` instead. |
| 115 | |
| 116 | |
| 117 | .. cfunction:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype) |
| 118 | |
| 119 | Convert a :ctype:`double` *val* to a string using supplied |
| 120 | *format_code*, *precision*, and *flags*. |
| 121 | |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 122 | *format_code* must be one of ``'e'``, ``'E'``, ``'f'``, ``'F'``, |
| 123 | ``'g'``, ``'G'`` or ``'r'``. For ``'r'``, the supplied *precision* |
| 124 | must be 0 and is ignored. The ``'r'`` format code specifies the |
| 125 | standard :func:`repr` format. |
Eric Smith | cc32a11 | 2009-04-26 21:35:14 +0000 | [diff] [blame] | 126 | |
| 127 | *flags* can be zero or more of the values *Py_DTSF_SIGN*, |
| 128 | *Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together: |
| 129 | |
| 130 | * *Py_DTSF_SIGN* means to always precede the returned string with a sign |
| 131 | character, even if *val* is non-negative. |
| 132 | |
| 133 | * *Py_DTSF_ADD_DOT_0* means to ensure that the returned string will not look |
| 134 | like an integer. |
| 135 | |
| 136 | * *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the |
| 137 | documentation for the :cfunc:`PyOS_snprintf` ``'#'`` specifier for |
| 138 | details. |
| 139 | |
| 140 | If *ptype* is non-NULL, then the value it points to will be set to one of |
| 141 | *Py_DTST_FINITE*, *Py_DTST_INFINITE*, or *Py_DTST_NAN*, signifying that |
| 142 | *val* is a finite number, an infinite number, or not a number, respectively. |
| 143 | |
| 144 | The return value is a pointer to *buffer* with the converted string or |
| 145 | *NULL* if the conversion failed. The caller is responsible for freeing the |
| 146 | returned string by calling :cfunc:`PyMem_Free`. |
| 147 | |
| 148 | .. versionadded:: 3.1 |
| 149 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 150 | |
| 151 | .. cfunction:: double PyOS_ascii_atof(const char *nptr) |
| 152 | |
| 153 | Convert a string to a :ctype:`double` in a locale-independent way. |
| 154 | |
| 155 | See the Unix man page :manpage:`atof(2)` for details. |
| 156 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 157 | .. deprecated:: 3.1 |
Mark Dickinson | aa8be96 | 2009-10-31 09:40:56 +0000 | [diff] [blame] | 158 | Use :cfunc:`PyOS_string_to_double` instead. |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 159 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 57e1b50 | 2009-04-27 16:54:22 +0000 | [diff] [blame] | 161 | .. cfunction:: char* PyOS_stricmp(char *s1, char *s2) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 163 | Case insensitive comparison of strings. The function works almost |
| 164 | identically to :cfunc:`strcmp` except that it ignores the case. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 165 | |
| 166 | |
Georg Brandl | 57e1b50 | 2009-04-27 16:54:22 +0000 | [diff] [blame] | 167 | .. cfunction:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 168 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 169 | Case insensitive comparison of strings. The function works almost |
| 170 | identically to :cfunc:`strncmp` except that it ignores the case. |