문자열 변환과 포매팅

숫자 변환과 포맷된 문자열 출력을 위한 함수.

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

포맷 문자열 format 과 추가 인자에 따라 size 바이트를 넘지 않도록 str로 출력합니다. 유닉스 매뉴얼 페이지 snprintf(3)를 보십시오.

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

포맷 문자열 format 과 가변 인자 목록 va에 따라 size 바이트를 넘지 않도록 str로 출력합니다. 유닉스 매뉴얼 페이지 vsnprintf(3)를 보십시오.

PyOS_snprintf()PyOS_vsnprintf()는 표준 C 라이브러리 함수 snprintf()vsnprintf()를 감쌉니다. 그들의 목적은 경계 조건에서 표준 C 함수가 제공하지 않는 수준의 일관된 동작을 보장하는 것입니다.

래퍼는 반환 시 str[size-1]이 항상 '\0'이 되도록 합니다. str에 size 바이트(후행 '\0' 포함)를 초과해서 쓰지 않습니다. 두 함수 모두 str != NULL, size > 0, format != NULLsize < INT_MAX를 요구합니다. 이는 필요한 버퍼 크기를 결정하는 C99 n = snprintf(NULL, 0, ...)에 해당하는 것이 없다는 뜻임에 유의하십시오.

이 함수들의 반환 값(rv)은 다음과 같이 해석되어야 합니다:

  • 0 <= rv < size 일 때, 출력 변환에 성공했으며 rv 문자가 str에 기록되었습니다 (str[rv]의 후행 '\0' 바이트 제외).

  • rv >= size 일 때, 출력 변환이 잘렸고 성공하려면 rv + 1 바이트의 버퍼가 필요합니다. str[size-1]은 이때 '\0'입니다.

  • When rv < 0, the output conversion failed and 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.

다음 함수는 로케일 독립적인 문자열에서 숫자로의 변환을 제공합니다.

unsigned long PyOS_strtoul(const char *str, char **ptr, int base)
Part of the 안정 ABI.

Convert the initial part of the string in str to an unsigned long value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

Leading white space and case of characters are ignored. If base is zero it looks for a leading 0b, 0o or 0x to tell which base. If these are absent it defaults to 10. Base must be 0 or between 2 and 36 (inclusive). If ptr is non-NULL it will contain a pointer to the end of the scan.

If the converted value falls out of range of corresponding return type, range error occurs (errno is set to ERANGE) and ULONG_MAX is returned. If no conversion can be performed, 0 is returned.

See also the Unix man page strtoul(3).

Added in version 3.2.

long PyOS_strtol(const char *str, char **ptr, int base)
Part of the 안정 ABI.

Convert the initial part of the string in str to an long value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

Same as PyOS_strtoul(), but return a long value instead and LONG_MAX on overflows.

See also the Unix man page strtol(3).

Added in version 3.2.

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

문자열 sdouble로 변환하고, 실패 시 파이썬 예외를 발생시킵니다. 허용되는 문자열 집합은 s가 선행이나 후행 공백을 가질 수 없다는 점을 제외하고는 파이썬의 float() 생성자가 허용하는 문자열 집합에 대응합니다. 변환은 현재 로케일과 독립적입니다.

endptrNULL이면, 전체 문자열을 변환합니다. 문자열이 부동 소수점 숫자의 유효한 표현이 아니면 ValueError를 발생시키고 -1.0을 반환합니다.

endptr이 NULL이 아니면, 가능한 한 많은 문자열을 변환하고 *endptr이 변환되지 않은 첫 번째 문자를 가리키도록 설정합니다. 문자열의 초기 세그먼트가 부동 소수점 숫자의 유효한 표현이 아니면, *endptr이 문자열의 시작을 가리키도록 설정하고, ValueError를 발생시키고 -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_INFINITY (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.

변환 중 다른 에러가 발생하면 (예를 들어 메모리 부족 에러), 적절한 파이썬 예외를 설정하고 -1.0을 반환합니다.

Added in version 3.1.

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

제공된 format_code, precisionflags를 사용하여 double val을 문자열로 변환합니다.

format_code'e', 'E', 'f', 'F', 'g', 'G' 또는 'r' 중 하나여야 합니다. 'r'의 경우, 제공된 precision은 0이어야 하며 무시됩니다. 'r' 포맷 코드는 표준 repr() 형식을 지정합니다.

flagsPy_DTSF_SIGN, Py_DTSF_ADD_DOT_0 또는 Py_DTSF_ALT 값을 0개 이상 함께 or 할 수 있습니다:

  • Py_DTSF_SIGNval가 음수가 아닐 때도 항상 반환된 문자열 앞에 부호 문자가 오는 것을 뜻합니다.

  • Py_DTSF_ADD_DOT_0은 반환된 문자열이 정수처럼 보이지 않도록 하는 것을 뜻합니다.

  • Py_DTSF_ALT는 “대체” 포매팅 규칙을 적용하는 것을 뜻합니다. 자세한 내용은 PyOS_snprintf() '#' 지정자에 대한 설명서를 참조하십시오.

ptypeNULL이 아니면, 포인터가 가리키는 값은 Py_DTST_FINITE, Py_DTST_INFINITE 또는 Py_DTST_NAN 중 하나로 설정되어, val가 각각 유한 수, 무한 수 또는 NaN임을 나타냅니다.

반환 값은 변환된 문자열이 있는 buffer에 대한 포인터이거나, 변환에 실패하면 NULL입니다. 호출자는 PyMem_Free()를 호출하여 반환된 문자열을 해제해야 합니다.

Added in version 3.1.

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

대소 문자 구분 없는 문자열 비교. 이 함수는 대소 문자를 무시한다는 점만 제외하면 strcmp()와 거의 같게 작동합니다.

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

대소 문자 구분 없는 문자열 비교. 이 함수는 대소 문자를 무시한다는 점만 제외하면 strncmp()와 거의 같게 작동합니다.