名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | locale‎ | codecvt
 
 
 
 
 
定義於標頭檔案 <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) 嘗試轉換由 [fromfrom_end) 定義的字元陣列中的 ExternT 字元,給定初始轉換狀態 state,最多轉換為 maxInternT 字元,並返回這種轉換將消耗的 ExternT 字元數。修改 state,如同透過為某個虛構的輸出緩衝區 [toto + max) 執行 do_in(state, from, from_end, from, to, to + max, to)

目錄

[編輯] 返回值

若透過 do_in() 轉換,直到所有 from_end - from 字元都被消耗或產生了 maxInternT 字元,或發生轉換錯誤,則將消耗的 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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
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)
未要求

[編輯] 參閱

[虛]
將字串從 ExternT 轉換為 InternT,例如從檔案讀取時
(虛保護成員函式) [編輯]