strverscmpのヘルプ・マニュアル
日本語 英語
strverscmp --help
man strverscmp
STRVERSCMP(3) Linux Programmer’s Manual STRVERSCMP(3)
名前
strverscmp - 2つのバージョン文字列を比較する
書式
#define _GNU_SOURCE
#include
int strverscmp(const char *s1, const char *s2);
説明
jan1, jan2, ..., jan9, jan10, ... といった名前のファイルがある状況はよ
くあるが、 ls(1) を実行したときに jan1, jan10, ..., jan2, ..., jan9 の
順 番で表示されるのには違和感がある。これを修正するために、GNU は ls(1)
に -v オプションを導入した。この機能は versionsort(3) を使って実装さ れ
ているが、この中で strverscmp() が使用されている。
このように strverscmp() の役目は2つの文字列を比較して「正しい」順序を探
すことである。これに対して strcmp(3) は辞書順で比較した結果を返すだけで
あ る。関数 strverscmp() はロケールのカテゴリである LC_COLLATE を使用し
ない。このことから、この関数が主にアスキー文字から成る文字列を想定し て
いることが分かる。
こ の関数の動作は以下の通りである。両方の文字列が等しい場合、0 を返す。
それ以外の場合、その直前までは両方の文字列が等しく、その直後のバイト で
両 者に違いがあるような、バイトの境界を探す。見つかったバイト境界を含む
数字列(数字だけの文字列)の最長一致検索を行う。 (数字列は境界から始ま っ
ていても、境界で終わっていてもよい)。 2つの文字列から得られた数字列の一
方または両方が空であれば、 strcmp(3) が返した結果を関数の返り値として返
す。すなわち、バイト値を比較した結果を返す。それ以外の(数字列が両方とも
空でない)場合、両方の数字列を数字順で比較する。このとき、1つ以上の 0 が
先 頭 にある数字列は、前に小数点がついているものと解釈される。(先頭に 0
が多くある数字列ほど前に来ることになる) この結果、順序は次のようになる:
000, 00, 01, 010, 09, 0, 1, 9, 10
返り値
関数 strverscmp() は、ゼロよりも 1)小さい、2)等しい、3)大きいのいずれか
の整数を返す。それぞれは、s1 が s2 よりも、 1)小さい、2)等しい、3)大 き
い ことを示す。
準拠
この関数は GNU による拡張である。
関連項目
rename(1), strcasecmp(3), strcmp(3), strcoll(3), feature_test_macros(7)
GNU 2001-12-19 STRVERSCMP(3)
STRVERSCMP(3) Linux Programmer’s Manual STRVERSCMP(3)
NAME
strverscmp - compare two version strings
SYNOPSIS
#define _GNU_SOURCE
#include
int strverscmp(const char *s1, const char *s2);
DESCRIPTION
Often one has files jan1, jan2, ..., jan9, jan10, ... and it feels
wrong when ls(1) orders them jan1, jan10, ..., jan2, ..., jan9. In
order to rectify this, GNU introduced the -v option to ls(1), which is
implemented using versionsort(3), which again uses strverscmp().
Thus, the task of strverscmp() is to compare two strings and find the
"right" order, while strcmp(3) only finds the lexicographic order.
This function does not use the locale category LC_COLLATE, so is meant
mostly for situations where the strings are expected to be in ASCII.
What this function does is the following. If both strings are equal,
return 0. Otherwise find the position between two bytes with the prop-
erty that before it both strings are equal, while directly after it
there is a difference. Find the largest consecutive digit strings con-
taining (or starting at, or ending at) this position. If one or both
of these is empty, then return what strcmp(3) would have returned
(numerical ordering of byte values). Otherwise, compare both digit
strings numerically, where digit strings with one or more leading zeros
are interpreted as if they have a decimal point in front (so that in
particular digit strings with more leading zeros come before digit
strings with fewer leading zeros). Thus, the ordering is 000, 00, 01,
010, 09, 0, 1, 9, 10.
RETURN VALUE
The strverscmp() function returns an integer less than, equal to, or
greater than zero if s1 is found, respectively, to be earlier than,
equal to, or later than s2.
CONFORMING TO
This function is a GNU extension.
SEE ALSO
rename(1), strcasecmp(3), strcmp(3), strcoll(3), feature_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2001-12-19 STRVERSCMP(3)