Namespaces
Variants

std::codecvt<InternT,ExternT,StateT>:: length, do_length

From cppreference.net
ヘッダーで定義 <locale>
public :

int length ( StateT & state, const ExternT * from, const ExternT * from_end,

std:: size_t max ) const ;
(1)
protected :

virtual int do_length ( StateT & state, const ExternT * from, const ExternT * from_end,

std:: size_t max ) const ;
(2)
1) 公開メンバ関数。最も派生したクラスのメンバ関数 do_length を呼び出します。
2) ExternT 文字配列で定義された文字を [ from , from_end ) から、初期変換状態 state を指定して、最大 max 個の InternT 文字へ変換を試み、その変換で消費される ExternT 文字数を返す。 state を、仮想的な [ to , to + max ) 出力バッファに対して do_in ( state, from, from_end, from, to, to + max, to ) を実行したかのように変更する。

目次

戻り値

ExternT 文字が do_in() によって変換された場合に消費される文字数。これは、 from_end - from のすべての文字が消費されるか、 max 個の InternT 文字が生成されるか、または変換エラーが発生するまでの数値です。

変換を行わない特殊化 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_length
std:: min ( max, from_end - from ) を返すことが要求されていた
要求されない

関連項目

[virtual]
ExternT から InternT への文字列変換を実行する(ファイルからの読み取り時など)
(仮想保護メンバー関数)