Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +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 | .. versionadded:: 2.4 |
| 64 | |
| 65 | See the Unix man page :manpage:`strtod(2)` for details. |
| 66 | |
| 67 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 68 | .. cfunction:: char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d) |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 69 | |
| 70 | Convert a :ctype:`double` to a string using the ``'.'`` as the decimal |
| 71 | separator. *format* is a :cfunc:`printf`\ -style format string specifying the |
| 72 | number format. Allowed conversion characters are ``'e'``, ``'E'``, ``'f'``, |
| 73 | ``'F'``, ``'g'`` and ``'G'``. |
| 74 | |
| 75 | The return value is a pointer to *buffer* with the converted string or NULL if |
| 76 | the conversion failed. |
| 77 | |
| 78 | .. versionadded:: 2.4 |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 79 | .. deprecated:: 2.7 |
| 80 | This function is removed in Python 2.7 and 3.1. Use :func:`PyOS_double_to_string` |
| 81 | instead. |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 82 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 83 | |
| 84 | .. cfunction:: char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype) |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 85 | |
| 86 | Convert a :ctype:`double` *val* to a string using supplied |
| 87 | *format_code*, *precision*, and *flags*. |
| 88 | |
Eric Smith | a985a3a | 2009-05-05 18:26:08 +0000 | [diff] [blame] | 89 | *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 Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 93 | |
| 94 | *flags* can be zero or more of the values *Py_DTSF_SIGN*, |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 95 | *Py_DTSF_ADD_DOT_0*, or *Py_DTSF_ALT*, or-ed together: |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 96 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 97 | * *Py_DTSF_SIGN* means to always precede the returned string with a sign |
| 98 | character, even if *val* is non-negative. |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 99 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 100 | * *Py_DTSF_ADD_DOT_0* means to ensure that the returned string will not look |
| 101 | like an integer. |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 102 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 103 | * *Py_DTSF_ALT* means to apply "alternate" formatting rules. See the |
| 104 | documentation for the :cfunc:`PyOS_snprintf` ``'#'`` specifier for |
| 105 | details. |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 106 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 107 | 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 |
| 109 | *val* is a finite number, an infinite number, or not a number, respectively. |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 110 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 111 | The return value is a pointer to *buffer* with the converted string or |
Eric Smith | f254aab | 2009-04-26 10:05:11 +0000 | [diff] [blame] | 112 | *NULL* if the conversion failed. The caller is responsible for freeing the |
| 113 | returned string by calling :cfunc:`PyMem_Free`. |
Eric Smith | 068f065 | 2009-04-25 21:40:15 +0000 | [diff] [blame] | 114 | |
| 115 | .. versionadded:: 2.7 |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 116 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 117 | |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 118 | .. cfunction:: double PyOS_ascii_atof(const char *nptr) |
| 119 | |
| 120 | Convert a string to a :ctype:`double` in a locale-independent way. |
| 121 | |
| 122 | .. versionadded:: 2.4 |
| 123 | |
| 124 | See the Unix man page :manpage:`atof(2)` for details. |
| 125 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 126 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 127 | .. cfunction:: char* PyOS_stricmp(char *s1, char *s2) |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 128 | |
Benjamin Peterson | 90f3673 | 2008-07-12 20:16:19 +0000 | [diff] [blame] | 129 | Case insensitive comparison of strings. The function works almost |
| 130 | identically to :cfunc:`strcmp` except that it ignores the case. |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 131 | |
| 132 | .. versionadded:: 2.6 |
| 133 | |
| 134 | |
Georg Brandl | afb0d6e | 2009-04-26 06:01:04 +0000 | [diff] [blame] | 135 | .. cfunction:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size) |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 136 | |
Benjamin Peterson | 90f3673 | 2008-07-12 20:16:19 +0000 | [diff] [blame] | 137 | Case insensitive comparison of strings. The function works almost |
| 138 | identically to :cfunc:`strncmp` except that it ignores the case. |
Georg Brandl | 79e3d55 | 2008-01-19 22:14:27 +0000 | [diff] [blame] | 139 | |
| 140 | .. versionadded:: 2.6 |