日本-日本語
日本HPホーム 製品 & サービス OpenVMS製品情報
≫  お問い合わせ


OpenVMS マニュアル


 

OpenVMS ドキュメント
ライブラリ

タイトル
目次
まえがき
リファレンス・セクション
   ≫ライブラリ関数一覧
索引
PDF
OpenVMS ホーム

HP OpenVMS
HP C ランタイム・ライブラリ・リファレンス・マニュアル (下巻)


目次 索引




変更後の文字列を strcmp関数に渡したときに,未変更の文字列を strcoll関数に渡したときと同じ結果が得られるように,文字列を変更します。

形式

#include <string.h>

size_t strxfrm (char *s1, const char *s2, size_t maxchar);


引数



s1, s2

文字列へのポインタ。

maxchar

s1 に格納するバイト数の最大値 (終端の null を含みます)。

説明

strxfrm 関数は, s2 がポイントしている文字列を変換し,結果として得られた文字列を,s1 がポイントする配列に格納します。 s1 がポイントする配列には,終端の null を含めて, maxchar バイト以下の文字が格納されます。

maxchar の値が,変換後の文字列 ( 終端の null を含む ) を格納するのに必要なサイズよりも小さかった場合, s1 がポイントする配列の内容は不定となります。この場合,関数は変換後の文字列のサイズを返します。

maxchar が 0 の場合,s1 は NULL ポインタであってよく,関数は変換を行う前に,s1 配列の必要なサイズを返します。

文字列比較関数の strcollstrcmpは,比較する 2 つの文字列を与えられたときに,異なる結果を生成することがあります。これは, strcmpが文字列の中の文字のコード・ポイント値を単純に比較するのに対し, strcollが比較のためにロケール情報を使用することが原因です。ロケールによっては, strcollによるの比較はマルチパスの操作になり, strcmpよりも速度が遅くなります。

strxfrm関数の目的は, 2 つの変換後の文字列を strcmp関数に渡したときの結果が, 2 つの元の文字列を strcoll関数に渡したときと同じになるように,文字列を変換することです。 strxfrm関数は,同じ文字列に対して strcollを使って多数の比較を行わなくてはならないアプリケーションで有用です。この場合には,(ロケールによっては) strxfrmを使って文字列の変換を行った後に, strcmpで比較を行った方が効率的なことがあります。


戻り値

x s1 がポイントする,結果として得られる文字列の長さ ( 終端の null 文字は含みません )。

エラー条件のための戻り値は予約されていません。ただし,関数は errno を EINVAL に設定することがあります。 s2 がポイントする文字列は,照合シーケンスのドメイン外の文字を含んでいます。


/* This program verifies that two transformed strings when      */ 
/* passed through strxfrm and then compared, provide the same   */ 
/* result as if passed through strcoll without any              */ 
/* transformation.  
 
#include <string.h>                                        
#include <stdio.h> 
#include <stdlib.h> 
#include <locale.h> 
 
#define  BUFF_SIZE  256 
 
main() 
{ 
    char string1[BUFF_SIZE]; 
    char string2[BUFF_SIZE]; 
    int errno; 
    int coll_result; 
    int strcmp_result; 
    size_t strxfrm_result1; 
    size_t strxfrm_result2; 
 
    /* setlocale to French locale */ 
 
    if (setlocale(LC_ALL, "fr_FR.ISO8859-1") == NULL) { 
        perror("setlocale"); 
        exit(EXIT_FAILURE); 
    } 
 
    /* collate string 1 and string 2 and store the result */ 
 
    errno = 0; 
    coll_result = strcoll("<a`>bcd", "abcz"); 
    if (errno) { 
        perror("strcoll"); 
        exit(EXIT_FAILURE); 
    } 
 
    else { 
        /* Transform the strings (using strxfrm) into string1   */ 
        /* and string2                                          */ 
 
        strxfrm_result1 = strxfrm(string1, "<a`>bcd", BUFF_SIZE); 
 
        if (strxfrm_result1 == ((size_t) - 1)) { 
            perror("strxfrm"); 
            exit(EXIT_FAILURE); 
        } 
 
        else if (strxfrm_result1 > BUFF_SIZE) { 
            perror("\n** String is too long **\n"); 
            exit(EXIT_FAILURE); 
        } 
 
        else { 
            strxfrm_result2 = strxfrm(string2, "abcz", BUFF_SIZE); 
            if (strxfrm_result2 == ((size_t) - 1)) { 
                perror("strxfrm"); 
                exit(EXIT_FAILURE); 
            } 
 
            else if (strxfrm_result2 > BUFF_SIZE) { 
                perror("\n** String is too long **\n"); 
                exit(EXIT_FAILURE); 
            } 
 
            /* Compare the two transformed strings and verify   */ 
            /* that the result is the same as the result from   */ 
            /* strcoll on the original strings                  */ 
            else { 
                strcmp_result = strcmp(string1, string2); 
                if (strcmp_result == 0 && (coll_result == 0)) { 
                    printf("\nReturn value from strcoll() and " 
                      "return value from strcmp() are both zero."); 
                   printf("\nThe program was successful\n\n"); 
                } 
 
                else if ((strcmp_result < 0) && (coll_result < 0)) { 
                    printf("\nReturn value from strcoll() and " 
                 "return value from strcmp() are less than zero."); 
                    printf("\nThe program successful\n\n"); 
                } 
 
                else if ((strcmp_result > 0) && (coll_result > 0)) { 
                    printf("\nReturn value from strcoll() and " 
              "return value from strcmp() are greater than zero."); 
                    printf("\nThe program was successful\n\n"); 
                } 
 
                else { 
                printf("** Error **\n"); 
                printf("\nReturn values are not of the same type"); 
                } 
            } 
        } 
   } 
} 

この例のプログラムを実行すると,次の結果が出力されます。

Return value from strcoll() and return value 
               from strcmp() are less than zero. 
The program was successful 


目次 索引

© 2012 Hewlett-Packard Development Company, L.P.