Konwersja i formatowanie ciągów znaków

funkcja do konwersji liczb i formatowania napis wyjście.

int PyOS_snprintf(char *str, size_t size, const char *format, ...)
Part of the Stable ABI.

Wyjście nie więcej niż size bajtów do str zgodnie z format napis format i ekstra argument. Zobacz stronę podręcznika Unix snprintf(3).

int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
Part of the Stable ABI.

Wyprowadzaj nie więcej niż size bajtów do str zgodnie z ciągiem format napis ującym format i listą argumentów zmiennych va. Strona podręcznika systemu Unix vsnprintf(3).

PyOS_snprintf() I PyOS_vsnprintf() owinąć Standardowe C biblioteka funkcja snprintf() I vsnprintf(). Ich celem jest zagwarantowanie spójnego zachowania w skrajnych przypadkach, czego nie zapewniają standardowe funkcje języka C.

Wraptory zapewniają, że str[size-1] zawsze jest to '\0' wartość zwracaćz. Nigdy nie zapisują więcej niż size bajtów (wliczając końcowy``»0»`` ) do str. Obie funkcja wymagają aby str != NULL, size > 0, format != NULL i size < INT_MAX. Należy pamiętać, że oznacza to brak odpowiednika C99 n = snprintf(NULL, 0, ...), który określałby wymagany rozmiar bufor size.

The zwracana wartość (rv) dla tych funkcja należy interpretować je następująco:

  • Gdy 0 <= rv < size, konwersja danych wyjściowych zakończyła się powodzeniem, a znaki rv znak zapisane w str (z wyłączeniem końcowego '\0' bajt o godz str[rv])

  • Gdy rv >= size, konwersja wyjściowa została skrócona, a bufor z rv + 1 bajtów byłoby potrzebnych do osiągnięcia sukcesu. str[size-1] w '\0' tym przypadku.

  • When rv < 0, „something bad happened.” str[size-1] is '\0' in this case too, but the rest of str is undefined. The exact cause of the error depends on the underlying platform.

Poniższe funkcje umożliwiają niezależną od ustawień regionalnych konwersję ciągów znaków na liczby.

double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)
Part of the Stable ABI.

Konwertuj ciąg napis s a double, rzucić a Python Wyjątek w przypadku błędu. Zestaw akceptowanych ciągów znaków odpowiada zestawowi ciągów znaków akceptowanych float`przez konstruktor Python ``s`(), z tym wyjątkiem, że nie mogą one zawierać spacji na początku ani na końcu. Konwersja jest niezależna od bieżącej lokalizacji.

Jeśli endptr jest NULL, przekonwertować cały napis. Rzucić ValueError I zwracać -1.0 jeśli napis nie jest ważny prawidłową reprezentacją liczby zmiennoprzecinkowej.

Jeśli endptr jest różny od NULL , konwertuj tak dużą część napis jak to możliwe *endptr, i ustaw wskaźnik na pierwszy nieprzekonwertowany znak. Jeżeli żaden początkowy segment napis nie jest prawidłową reprezentacją liczby zmiennoprzecinkowej, ustaw go tak *endptr, aby wskazywał na początek ciągu, zgłoś wyjątek ValueError i zwróć -1.0 .

If s represents a value that is too large to store in a float (for example, "1e500" is such a string on many platforms) then if overflow_exception is NULL return Py_HUGE_VAL (with an appropriate sign) and don’t set any exception. Otherwise, overflow_exception must point to a Python exception object; raise that exception and return -1.0. In both cases, set *endptr to point to the first character after the converted value.

Jeżeli podczas konwersji wystąpi jakikolwiek inny błąd (na przykład błąd braku pamięci), ustaw odpowiedni wyjątek Pythona i zwracać -1.0.

Nowe w wersji 3.1.

char *PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)
Part of the Stable ABI.

Konwertuje double val na napis ciąg znaków przy użyciu dostarczonych format_code, precision i flags.

format_code must be one of 'e', 'E', 'f', 'F', 'g', 'G' or 'r'. For 'r', the supplied precision must be 0 and is ignored. The 'r' format code specifies the standard repr() format.

flags can be zero or more of the values Py_DTSF_SIGN, Py_DTSF_ADD_DOT_0, or Py_DTSF_ALT, or-ed together:

  • Py_DTSF_SIGN means to always precede the returned string with a sign character, even if val is non-negative.

  • Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look like an integer.

  • Py_DTSF_ALT means to apply „alternate” formatting rules. See the documentation for the PyOS_snprintf() '#' specifier for details.

If ptype is non-NULL, then the value it points to will be set to one of Py_DTST_FINITE, Py_DTST_INFINITE, or Py_DTST_NAN, signifying that val is a finite number, an infinite number, or not a number, respectively.

The return value is a pointer to buffer with the converted string or NULL if the conversion failed. The caller is responsible for freeing the returned string by calling PyMem_Free().

Nowe w wersji 3.1.

int PyOS_stricmp(const char *s1, const char *s2)

Case insensitive comparison of strings. The function works almost identically to strcmp() except that it ignores the case.

int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t size)

Case insensitive comparison of strings. The function works almost identically to strncmp() except that it ignores the case.