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 ,例如写入文件时(虚保护成员函数) |