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
facet 定义了转换,则将源范围
[
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转换刻面被 basic_filebuf 所使用,且 -
do_in
(
state, from, from_end, from_next, to, to_end, to_next
)
将返回
ok当 to ! = to_end 时。
目录 |
返回值
类型为 std::codecvt_base::result 的值,用于指示成功状态如下:
ok
|
转换完成 |
partial
|
输出缓冲区空间不足或源缓冲区意外结束 |
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"; // 或 u8"zß水𝄋" // 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b" // 注意以下操作也可通过 wstring_convert 实现 std::mbstate_t mb = std::mbstate_t(); // 初始移位状态 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); // 为简洁起见省略了错误检查 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
使用时需要支持 |
参见
|
[virtual]
|
从关联文件读取数据
(
std::basic_filebuf<CharT,Traits>
的虚保护成员函数)
|
|
将字节字符串转换为宽字符串
(
std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>
的公开成员函数)
|
|
|
在给定状态条件下将窄多字节字符串转换为宽字符串
(函数) |
|
|
[virtual]
|
将字符串从
InternT
转换为
ExternT
,例如写入文件时
(虚保护成员函数) |