字符串转换与格式化

用于数字转换和格式化字符串输出的函数

int PyOS_snprintf(char *str, size_t size, const char *format, ...)

根据格式字符串 format 和额外参数,输出不超过 size 字节到 str 。请参见Unix手册页 snprintf(2)

int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)

根据格式字符串 format 和 变量参数列表 va ,不能输出超过 size 字节到 str 。请参见Unix手册页 vsnprintf(2)

PyOS_snprintf() and PyOS_vsnprintf() wrap the Standard C library functions snprintf() and vsnprintf(). Their purpose is to guarantee consistent behavior in corner cases, which the Standard C functions do not.

The wrappers ensure that str*[*size-1] is always '\0' upon return. They never write more than size bytes (including the trailing '\0' into str. Both functions require that str != NULL, size > 0 and format != NULL.

If the platform doesn’t have vsnprintf() and the buffer size needed to avoid truncation exceeds size by more than 512 bytes, Python aborts with a Py_FatalError.

这些函数的返回值( rv )应按照以下规则被解释:

  • 0 <= rv < size ,输出转换成功而且 rv 个字符被写入 str (不包含末尾 str*[*rv] 的 '\0' 字节)

  • rv >= size ,输出转换被截断并且成功需要一个带有 rv + 1 字节的缓冲区。在这种情况下, str*[*size-1] 的值是 '\0'

  • rv < 0 ,会发生一些不好的事情。 在这种情况下, str*[*size-1] 的值也是 '\0' , 但是 str 的其余部分未被定义。错误的确切原因取决于底层平台。

以下函数提供与语言环境无关的字符串到数字转换。

double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)

将字符串 s 转换为 double 类型,失败时引发Python异常。接受的字符串的集合对应于被 Python 的 float() 构造函数接受的字符串的集合,除了 s 必须没有前导或尾随空格。转换必须独立于当前的区域。

If endptr is NULL, convert the whole string. Raise ValueError and return -1.0 if the string is not a valid representation of a floating-point number.

如果 endptr 不是 NULL ,尽可能多的转换字符串并将 *endptr 设置为指向第一个未转换的字符。如果字符串的初始段不是浮点数的有效的表达方式,将 * endptr 设置为指向字符串的开头,引发 ValueError 异常,并且返回 -1.0

如果 s 表示一个太大而不能存储在一个浮点数中的值(比方说, "1e500" 在许多平台上是一个字符串)然后如果 overflow_exceptionNULL 返回 Py_HUGE_VAL (用适当的符号)并且不设置任何异常。 在其他方面, overflow_exception 必须指向一个 Python 异常对象;引发异常并返回 -1.0 。在这两种情况下,设置 *endptr 指向转换值之后的第一个字符。

如果在转换期间发生任何其他错误(比如一个内存不足的错误),设置适当的 Python 异常并且返回 -1.0

2.7 新版功能.

double PyOS_ascii_strtod(const char *nptr, char **endptr)

Convert a string to a double. This function behaves like the Standard C function strtod() does in the C locale. It does this without changing the current locale, since that would not be thread-safe.

PyOS_ascii_strtod() should typically be used for reading configuration files or other non-user input that should be locale independent.

See the Unix man page strtod(2) for details.

2.4 新版功能.

2.7 版后已移除: Use PyOS_string_to_double() instead.

char* PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d)

Convert a double to a string using the '.' as the decimal separator. format is a printf()-style format string specifying the number format. Allowed conversion characters are 'e', 'E', 'f', 'F', 'g' and 'G'.

The return value is a pointer to buffer with the converted string or NULL if the conversion failed.

2.4 新版功能.

2.7 版后已移除: This function is removed in Python 2.7 and 3.1. Use PyOS_double_to_string() instead.

char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)

转换 double val 为一个使用 format_codeprecisionflags 的字符串

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().

2.7 新版功能.

double PyOS_ascii_atof(const char *nptr)

Convert a string to a double in a locale-independent way.

See the Unix man page atof(2) for details.

2.4 新版功能.

3.1 版后已移除: Use PyOS_string_to_double() instead.

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

字符串不区分大小写。该函数几乎与 strcmp() 的工作方式相同,只是它忽略了大小写。

2.6 新版功能.

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

字符串不区分大小写。该函数几乎与 strncmp() 的工作方式相同,只是它忽略了大小写。

2.6 新版功能.