文字列の変換と書式化¶
数値変換と、書式化文字列出力のための関数群。
-
int PyOS_snprintf(char *str, size_t size, const char *format, ...)¶
- 次に属します: Stable ABI.
書式文字列 format と追加の引数から、 size バイトを超えない文字列を str に出力します。 Unix man page の snprintf(3) を参照してください。
-
int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)¶
- 次に属します: Stable ABI.
書式文字列 format と可変長引数リスト va から、 size バイトを超えない文字列を str に出力します。 Unix man page の vsnprintf(3) を参照してください。
PyOS_snprintf() と PyOS_vsnprintf() は標準Cライブラリの snprintf() と vsnprintf() 関数をラップします。これらの関数の目的は、C標準ライブラリが保証していないコーナーケースでの動作を保証することです。
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, format != NULL
and size < INT_MAX. Note that this means there is no equivalent to the C99
n = snprintf(NULL, 0, ...) which would determine the necessary buffer size.
これらの関数の戻り値 (以下では rv とします) は以下の意味を持ちます:
0 <= rv < sizeのとき、変換出力は成功して、(最後のstr[rv]にある'\0'を除いて) rv 文字が str に出力された。rv >= sizeのとき、変換出力は切り詰められており、成功するためにはrv + 1バイトが必要だったことを示します。str[size-1]は'\0'です。rv < 0のときは、何か悪いことが起こった時です。この場合でもstr[size-1]は'\0'ですが、str のそれ以外の部分は未定義です。エラーの正確な原因はプラットフォーム依存です。
以下の関数は locale 非依存な文字列から数値への変換を行ないます。
-
unsigned long PyOS_strtoul(const char *str, char **ptr, int base)¶
- 次に属します: Stable ABI.
Convert the initial part of the string in
strto an unsigned long value according to the givenbase, which must be between2and36inclusive, or be the special value0.Leading white space and case of characters are ignored. If
baseis zero it looks for a leading0b,0oor0xto tell which base. If these are absent it defaults to10. Base must be 0 or between 2 and 36 (inclusive). Ifptris non-NULLit 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 (
errnois set toERANGE) andULONG_MAXis returned. If no conversion can be performed,0is returned.See also the Unix man page strtoul(3).
バージョン 3.2 で追加.
-
long PyOS_strtol(const char *str, char **ptr, int base)¶
- 次に属します: Stable ABI.
Convert the initial part of the string in
strto an long value according to the givenbase, which must be between2and36inclusive, or be the special value0.Same as
PyOS_strtoul(), but return a long value instead andLONG_MAXon overflows.See also the Unix man page strtol(3).
バージョン 3.2 で追加.
-
double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)¶
- 次に属します: Stable ABI.
文字列
sを double に変換します。失敗したときは Python の例外を発生させます。受け入れられる文字列は、 Python のfloat()コンストラクタが受け付ける文字列に準拠しますが、sの先頭と末尾に空白文字があってはならないという部分が異なります。この変換は現在のロケールに依存しません。endptrがNULLの場合、変換は文字列全体に対して行われます。文字列が正しい浮動小数点数の表現になっていない場合は-1.0を返してValueErrorを発生させます。endptr が
NULLで無い場合、文字列を可能な範囲で変換して、*endptrに最初の変換されなかった文字へのポインタを格納します。文字列の先頭に正しい浮動小数点数の表現が無かった場合、*endptrを文字列の先頭に設定して、ValueError を発生させ、-1.0を返します。If
srepresents a value that is too large to store in a float (for example,"1e500"is such a string on many platforms) then ifoverflow_exceptionisNULLreturnPy_HUGE_VAL(with an appropriate sign) and don't set any exception. Otherwise,overflow_exceptionmust point to a Python exception object; raise that exception and return-1.0. In both cases, set*endptrto point to the first character after the converted value.それ以外のエラーが変換中に発生した場合(例えば out-of-memory エラー)、適切な Python の例外を設定して
-1.0を返します。バージョン 3.1 で追加.
-
char *PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)¶
- 次に属します: Stable ABI.
double val を指定された format_code, precision, flags に基づいて文字列に変換します。
format_code は
'e','E','f','F','g','G','r'のどれかでなければなりません。'r'の場合、 precision は 0 でなければならず、無視されます。'r'フォーマットコードは標準のrepr()フォーマットを指定しています。flags は 0 か、
Py_DTSF_SIGN,Py_DTSF_ADD_DOT_0,Py_DTSF_ALTか、これらの or を取ったものです:Py_DTSF_SIGNは、val が負で無いときも常に符号文字を先頭につけることを意味します。Py_DTSF_ADD_DOT_0は文字列が整数のように見えないことを保証します。Py_DTSF_ALTは "alternate" フォーマットルールを適用することを意味します。 詳細はPyOS_snprintf()の'#'指定を参照してください。
ptype が
NULLで無い場合、val が有限数、無限数、NaNのどれかに合わせて、Py_DTST_FINITE,Py_DTST_INFINITE,Py_DTST_NANのいずれかに設定されます。戻り値は変換後の文字列が格納された buffer へのポインタか、変換が失敗した場合は
NULLです。 呼び出し側は、返された文字列をPyMem_Free()を使って解放する責任があります。バージョン 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.