std::codecvt<InternT,ExternT,StateT>::length, do_length
From cppreference.com
| ヘッダー <locale> で定義 |
||
| public: int length( StateT& state, const ExternT* from, const ExternT* from_end, |
(1) | |
| protected: virtual int do_length( StateT& state, const ExternT* from, const ExternT* from_end, |
(2) | |
1) 公開メンバ関数。最も派生したクラスのメンバ関数
do_lengthを呼び出します。2) 初期変換状態stateが与えられた、配列
[from, from_end)で定義されたExternT文字の配列から、最大max個のInternT文字への変換を試み、そのような変換が消費するExternT文字の数を返します。stateを、ある仮想的な出力バッファ[to, to + max)に対してdo_in(state, from, from_end, from, to, to + max, to)を実行したかのように変更します。目次 |
[編集] 戻り値
from_end - from文字すべてが消費されるか、max個のInternT文字が生成されるか、または変換エラーが発生するまで、do_in()によって変換された場合に消費されるExternT文字の数。
非変換特殊化std::codecvt<char, char, std::mbstate_t>はstd::min(max, from_end - from)を返します。
[編集] 例
このコードを実行
#include <iostream> #include <locale> #include <string> int main() { using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>; // narrow multibyte encoding std::string s = "z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b" std::locale loc("en_US.UTF-8"); facet_type const& codecvt_facet = std::use_facet<facet_type>(loc); std::mbstate_t mb = std::mbstate_t(); std::cout << "Only the first " << codecvt_facet.length(mb, s.data(), s.data() + s.size(), 2) << " bytes out of " << s.size() << " would be consumed" " to produce the first 2 characters\n"; }
出力
Only the first 3 bytes out of 10 would be consumed to produce the first 2 characters
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 75 | C++98 | stateへの影響が指定されていませんでした。 | 指定された |
| LWG 305 | C++98 | std::codecvt<wchar_t, char, std::mbstate_t>::do_lengthstd::min(max, from_end - from)を返す必要がありました。 |
要求されない |
[編集] 関連項目
| [virtual] |
ファイルからの読み込み時など、ExternT から InternT への文字列を変換します。(仮想保護メンバ関数) |