strverscmp

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

strverscmp - 2つのバージョン文字列を比較する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#define _GNU_SOURCE         /* feature_test_macros(7) 参照 */
#include <string.h>

int strverscmp(const char *s1, const char *s2);
 

説明

For a dataset like jan1, jan2, ..., jan9, jan10, ... sorting it lexicographically yields jan1, jan10, ..., jan2, ..., jan9. The task of strverscmp() is to compare two strings yielding the former order, while strcmp(3) finds only 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. This is different from the ordering produced by sort(1) -V.

この関数の動作は以下の通りである。 両方の文字列が等しい場合、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)大きい ことを示す。  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
strverscmp() Thread safetyMT-Safe
 

標準規格

GNU.  

例

The program below can be used to demonstrate the behavior of strverscmp(). It uses strverscmp() to compare the two strings given as its command-line arguments. An example of its use is the following:

$ ./a.out jan1 jan10;
jan1 < jan10
 

プログラムのソース

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    int res;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <string1> <string2>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    res = strverscmp(argv[1], argv[2]);

    printf("%s %s %s\n", argv[1],
           (res < 0) ? "<" : (res == 0) ? "==" : ">", argv[2]);

    exit(EXIT_SUCCESS);
}
 

関連項目

rename(1), strcasecmp(3), strcmp(3), strcoll(3)


 

Index

名前
ライブラリ
書式
説明
返り値
属性
標準規格
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.