名前空間
変種
操作

「cpp/string/multibyte/mbrtoc32」の版間の差分

提供: cppreference.com
< cpp‎ | string‎ | multibyte
(r2.7.3) (ロボットによる 追加: de, en, es, fr, it, pt, ru, zh)
 
(1人の利用者による、間の2版が非表示)
1行: 1行:
{{tr_note}}
 
 
{{cpp/title|mbrtoc32}}
 
{{cpp/title|mbrtoc32}}
 
{{cpp/string/multibyte/navbar}}
 
{{cpp/string/multibyte/navbar}}
{{ddcl | header=cuchar | notes={{mark since c++11}} |
+
{{ddcl | header=cuchar | sincec++11 |
 
std::size_t mbrtoc32( char32_t* pc32,
 
std::size_t mbrtoc32( char32_t* pc32,
 
                       const char* s,
 
                       const char* s,
9行: 8行:
 
}}
 
}}
  
{{tr|その32ビットの文字表現(一般的には、UTF-32)にナローマルチバイト文字に変換し.|Converts a narrow multibyte character to its 32-bit character representation (typically, UTF-32).}}
+
UTF-32
  
{{tr|{{tt|s}}がNULLポインタでない場合は、次のマルチバイト文字(任意のシフトシーケンスを含む)を完了するのに必要なバイト数を決定するために{{tt|n}}が指すバイトから始めて、マルチバイト文字列の中で最も{{tt|s}}バイトを検査します。関数は{{tt|s}}で次のマルチバイト文字が完全かつ有効であると判断した場合、対応する32ビットの文字に変換して{{c|*pc32}}に格納({{tt|pc32}}nullでない場合).|If {{tt|s}} is not a null pointer, inspects at most {{tt|n}} bytes of the multibyte character string, beginning with the byte pointed to by {{tt|s}} to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in {{tt|s}} is complete and valid, converts it to the corresponding 32-bit character and stores it in {{c|*pc32}} (if {{tt|pc32}} is not null).}}
+
{{tt|s}} {{tt|s}} {{tt|n}} {{tt|s}} ({{tt|}} {{c|*pc32}}
  
{{tr|{{tt|*s}}にマルチバイト文字がマルチchar32_tシーケンス(UTF-32では可能ではない)に対応している場合、この関数の最初の呼び出しの後に、{{tt|*ps}}は{{tt|mbrtoc32}}隣にコールが追加char32_tを書き出すような方法で更新されます、{{tt|*s}}を考慮せずに.|If the multibyte character in {{tt|*s}} corresponds to a multi-char32_t sequence (not possible with UTF-32), then after the first call to this function, {{tt|*ps}} is updated in such a way that the next calls to {{tt|mbrtoc32}} will write out the additional char32_t, without considering {{tt|*s}}.}}
+
{{tt|*s}} char32_t (UTF-32 ){{tt|}} {{tt|}} char32_t {{tt|*}}
  
{{tr|{{tt|s}}がNULLポインターである場合、値{{tt|n}}{{tt|pc32}}無視され、コールが{{c|std::mbrtoc32(NULL, "", 1, ps)}}に相当するものであり.|If {{tt|s}} is a null pointer, the values of {{tt|n}} and {{tt|pc32}} are ignored and the call is equivalent to {{c|std::mbrtoc32(NULL, "", 1, ps)}}.}}
+
{{tt|s}} {{tt|n}} {{tt|pc32}} {{c|std::mbrtoc32(NULL, "", 1, ps)}}
  
{{tr|生産ワイド文字がヌル文字である場合、変換状態{{c|*ps}}は初期シフト状態を表します.|If the wide character produced is the null character, the conversion state {{c|*ps}} represents the initial shift state.}}
+
{{c|*ps}}
  
{{tr|マクロ{{c|__STDC_UTF_32__}}が定義されている場合、この機能によって使用される32ビットのエンコーディングはUTF-32でそれ以外の場合は、実装定義である.|If the macro {{c|__STDC_UTF_32__}} is defined, the 32-bit encoding used by this function is UTF-32, otherwise it is implementation-defined.}}
+
  
===パラメータ===
+
======
{{param list begin}}
+
{{begin}}
{{param list item | pc32 |{{tr| 結果として得られる32ビットの文字が書き込まれる場所へのポインタ| pointer to the location where the resulting 32-bit character will be written}}}}
+
{{| pc32 | }}
{{param list item | s |{{tr| 入力として使用されるマルチバイト文字列へのポインタ| pointer to the multibyte character string used as input}}}}
+
{{| s | }}
{{param list item | n |{{tr| 調べることができますsのバイト数を制限しません| limit on the number of bytes in s that can be examined}}}}
+
{{| n | s }}
{{param list item | ps |{{tr| マルチバイト文字列を解釈する際に使用される変換状態オブジェクトへのポインタ | pointer to the conversion state object used when interpreting the multibyte string }}}}
+
{{| ps | }}
{{param list end}}
+
{{end}}
  
===値を返します===
+
======
{{tr|:適用される次の最初|The first of the following that applies:}}
+
* {{tr|{{c|0}}ヌル文字であった文字は{{tt|s}}から変換(null以外の場合と{{c|*pc32}}に格納されている)場合|{{c|0}} if the character converted from {{tt|s}} (and stored in {{c|*pc32}} if non-null) was the null character}}
+
* {{tr|バイト数{{c|[1...n]}}首尾{{tt|s}}から変換されたマルチバイト文字の|the number of bytes {{c|[1...n]}} of the multibyte character successfully converted from {{tt|s}}}}
+
* {{tr|{{c|-3}}マルチ{{c|char32_t}}文字から次の文字{{c|char32_t}}今{{c|*pc32}}に書き込まれている場合。バイトはこのケースでは、入力から処理されません.|{{c|-3}} if the next {{c|char32_t}} from a multi-{{c|char32_t}} character has now been written to {{c|*pc32}}. No bytes are processed from the input in this case.}}
+
* {{tr|{{c|-2}}次回{{tt|n}}バイトが不完全だが、今のところ有効なマルチバイト文字で構成されている場合。何も{{c|*pc32}}に書き込まれません.|{{c|-2}} if the next {{tt|n}} bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to {{c|*pc32}}.}}
+
* {{tr|{{c|-1}}符号化エラーが発生した場合。 {{tt|*pc32}}が指定されていない場合は何も{{c|EILSEQ}}に書き込まれません、値{{c|errno}}は{{c|*ps}}と値に格納されている.|{{c|-1}} if encoding error occurs. Nothing is written to {{tt|*pc32}}, the value {{c|EILSEQ}} is stored in {{c|errno}} and the value if {{c|*ps}} is unspecified.}}
+
  
===も参照してください===
+
{{dcl list begin}}
+
{{}}
{{dcl list template | cpp/string/multibyte/dcl list c32rtomb}}
+
{{|}}
{{dcl list template | cpp/locale/codecvt/dcl list do_in | mem=std::codecvt<char32_t, char, std::mbstate_t>}}
+
{{||char32_t}}
{{dcl list see c | c/string/multibyte/mbrtoc32}}
+
{{c|c}}
{{dcl list end}}
+
{{}}
  
[[de:cpp/string/multibyte/mbrtoc32]]
+
[[en:cpp/string/multibyte/mbrtoc32]]
+
[[es:cpp/string/multibyte/mbrtoc32]]
+
[[fr:cpp/string/multibyte/mbrtoc32]]
+
[[it:cpp/string/multibyte/mbrtoc32]]
+
[[pt:cpp/string/multibyte/mbrtoc32]]
+
[[ru:cpp/string/multibyte/mbrtoc32]]
+
[[zh:cpp/string/multibyte/mbrtoc32]]
+
 +
 +
 +
 
 +
 +
 +
 +
 
 +
:string //
 +
 
 +
[ :
 +
 +
]
 +
 
 +
://
 +
 +
 +
 
 +
mbrtoc32
 +
 +
:
 +
//
 +
 +
 +
[
 +
 +
[
 +
:]
 +
 +
 +
 +
 +
]
 +
[
 +
[
 +
:
 +
 +
 +
 
 +
 +
 +
cpp/string/multibyte/
 +
cpp///:
 +
/string/multibyte/mbrtoc32
 +
 +
 
 +

2018年5月11日 (金) 18:57時点における最新版

ヘッダ <cuchar> で定義
std::size_t mbrtoc32( char32_t* pc32,

                      const char* s,
                      std::size_t n,

                      std::mbstate_t* ps );
(C++11以上)

ナローマルチバイト文字を UTF-32 文字表現に変換します。

s がヌルポインタでなければ、次のマルチバイト文字 (あらゆるシフトシーケンスを含む) を完全にするために必要なバイト数を決定するために s の指すバイトから始まるマルチバイト文字列を最大 n バイト調べます。 s の次のマルチバイト文字が完全かつ有効であると決定できれば、それを対応する32ビット文字に変換し、 (pc32 がヌルでなければ) それを *pc32 に格納します。

*s 内のマルチバイト文字が複数の char32_t のシーケンスに対応する場合 (UTF-32 では有り得ません)、この関数の最初の呼び出し後、次の mbrtoc32 の呼び出しが *s を見ることなく追加の char32_t を書き出すように、 *ps が更新されます。

s がヌルポインタの場合、 n および pc32 の値は無視され、その呼び出しは std::mbrtoc32(NULL, "", 1, ps) と同等になります。

生成されたワイド文字がヌル文字の場合、変換状態 *ps は初期シフト状態を表すようになります。

この関数が使用するマルチバイトエンコーディングは、現在アクティブな C のロケールによって指定されます。

目次

[編集] 引数

pc32 - 結果の32ビット文字が書き込まれる位置を指すポインタ
s - 入力として使用されるマルチバイト文字列を指すポインタ
n - 調べる s 内のバイト数の制限
ps - マルチバイト文字列を解釈するときに使用される変換状態オブジェクトを指すポインタ

[編集] 戻り値

以下のうちいずれか最初に適用されるものを返します。

  • s から変換された (そしてヌルでなければ *pc32 に格納された) 文字がヌル文字であった場合は 0
  • s からの変換に成功したマルチバイト文字列のバイト数 [1...n]
  • 複数の char32_t 文字から次の char32_t*pc32 に書き込まれた場合は (size_t)-3。 この場合、入力から処理されるバイトはありません。
  • 次の n バイトがそこまでの部分は有効だけれども不完全なマルチバイト文字を構成する場合は (size_t)-2*pc32 には何も書き込まれません。
  • エンコーディングエラーが発生した場合は (size_t)-1*pc32 には何も書き込まれず、値 EILSEQerrno に格納され、値 *ps は未規定になります。

[編集]

#include <iostream>
#include <iomanip>
#include <clocale>
#include <cstring>
#include <cwchar>
#include <cuchar>
#include <cassert>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
 
    std::string str = u8"z\u00df\u6c34\U0001F34C"; // or u8"zß水🍌"
 
    std::cout << "Processing " << str.size() << " bytes: [ " << std::showbase;
    for(unsigned char c: str) std::cout << std::hex << +c << ' ';
    std::cout << "]\n";
 
    std::mbstate_t state{}; // zero-initialized to initial state
    char32_t c32;
    const char *ptr = str.c_str(), *end = str.c_str() + str.size() + 1;
 
    while(std::size_t rc = std::mbrtoc32(&c32, ptr, end - ptr, &state))
    {   
        std::cout << "Next UTF-32 char: " << std::hex << c32 << " obtained from ";
        assert(rc != (std::size_t)-3); // no surrogates in UTF-32
        if(rc == (std::size_t)-1) break;
        if(rc == (std::size_t)-2) break;
        std::cout << std::dec << rc << " bytes [ ";
        for(std::size_t n = 0; n < rc; ++n)
            std::cout << std::hex << +(unsigned char)ptr[n] << ' ';
        std::cout << "]\n";
        ptr += rc;
    }
}

出力:

Processing 10 bytes: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c ]
Next UTF-32 char: 0x7a obtained from 1 bytes [ 0x7a ]
Next UTF-32 char: 0xdf obtained from 2 bytes [ 0xc3 0x9f ]
Next UTF-32 char: 0x6c34 obtained from 3 bytes [ 0xe6 0xb0 0xb4 ]
Next UTF-32 char: 0x1f34c obtained from 4 bytes [ 0xf0 0x9f 0x8d 0x8c ]

[編集] 関連項目

(C++11)
32ビットワイド文字をマルチバイト文字列に変換します
(関数) [edit]
[仮想]
ファイルから読み込む時などのために、文字列を externT から internT に変換します
(std::codecvt<InternT,ExternT,State>の仮想プロテクテッドメンバ関数) [edit]