std::codecvt<InternT,ExternT,StateT>::in, std::codecvt<InternT,ExternT,StateT>::do_in
定義於標頭檔案 <locale> |
||
public: result in( StateT& state, |
(1) | |
protected: virtual result do_in( StateT& state, |
(2) | |
do_in
。codecvt
facette 定義了轉換,則將外部字元從源範圍 [
from,
from_end)
轉換到內部字元,並將結果置於始於 to 的後續位置。轉換的外部字元數不超過 from_end - from,寫入的內部字元數不超過 to_end - to。使 from_next 和 to_next 指向成功轉換的最後一個元素之後一位。若此 codecvt
facet 不定義轉換,則不轉換任何字元。to_next 被設定為等於 to,state 不變,並返回 std::codecvt_base::noconv。
do_in(state, from, from_end, from_next, to, to + 1, to_next) 必須返回 ok
,如果
- 此
codecvt
facet 被 basic_filebuf 使用,並且 - do_in(state, from, from_end, from_next, to, to_end, to_next) 會返回
ok
,其中 to != to_end。
目錄 |
[編輯] 返回值
一個 std::codecvt_base::result 型別的值,指示成功狀態,如下所示
ok
|
轉換完成 |
部分
|
輸出緩衝區空間不足或源緩衝區意外結束 |
error
|
遇到無法轉換的字元 |
noconv
|
此 facet 不執行轉換,未寫入輸出 |
非轉換特化 std::codecvt<char, char, std::mbstate_t> 總是返回 std::codecvt_base::noconv。
[編輯] 注意
要求 from <= from_end && to <= to_end,並且 state 要麼表示初始移位狀態,要麼透過轉換序列中前面的字元獲得。
對 state 的影響是故意未指定的。在標準 facet 中,它用於維護移位狀態,例如呼叫 std::mbsrtowcs 時,並因此更新以反映處理的最後一個外部字元後的轉換狀態,但使用者定義的 facet 可以自由地使用它來維護任何其他狀態,例如計算遇到的特殊字元的數量。
[編輯] 示例
#include <iostream> #include <locale> #include <string> int main() { std::locale::global(std::locale("en_US.utf8")); auto const& f = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>> (std::locale()); std::string external = "z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b" // note that the following can be done with wstring_convert std::mbstate_t mb = std::mbstate_t(); // initial shift state std::wstring internal(external.size(), '\0'); const char* from_next; wchar_t* to_next; f.in(mb, &external[0], &external[external.size()], from_next, &internal[0], &internal[internal.size()], to_next); // error checking skipped for brevity internal.resize(to_next - &internal[0]); std::wcout << L"The string in wide encoding: " << internal << '\n'; }
輸出
The string in wide encoding: zß水𝄋
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 76 | C++98 | 不清楚轉換是否要求 支援一次產生一個內部字元 |
僅在被使用時才要求 由 basic_filebuf |
[編輯] 另見
[虛] |
從關聯檔案讀取 ( std::basic_filebuf<CharT,Traits> 的虛保護成員函式) |
將位元組字串轉換為寬字串 ( std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc> 的公有成員函式) | |
將窄多位元組字元字串轉換為寬字串,給定狀態 (函式) | |
[虛] |
將字串從 InternT 轉換為 ExternT ,例如寫入檔案時(虛保護成員函式) |