命名空间
变体
操作

std::codecvt<InternT,ExternT,StateT>::in, std::codecvt<InternT,ExternT,StateT>::do_in

来自 cppreference.cn
< cpp‎ | locale‎ | codecvt
 
 
 
 
 
定义于头文件 <locale>
public:

result in( StateT& state,
           const ExternT* from,
           const ExternT* from_end,
           const ExternT*& from_next,
           InternT* to,
           InternT* to_end,

           InternT*& to_next ) const;
(1)
protected:

virtual result do_in( StateT& state,
                      const ExternT* from,
                      const ExternT* from_end,
                      const ExternT*& from_next,
                      InternT* to,
                      InternT* to_end,

                      InternT*& to_next ) const;
(2)
1) 公有成员函数,调用最派生类的成员函数 do_in
2) 如果此 codecvt facet 定义了一个转换,则将来自源范围 [fromfrom_end) 的外部字符转换为内部字符,并将结果放置在从 to 开始的后续位置。转换不超过 from_end - from 个外部字符,并且写入不超过 to_end - to 个内部字符。使 from_nextto_next 指向成功转换的最后一个元素之后的位置。

如果此 codecvt facet 未定义转换,则不转换任何字符。to_next 设置为等于 tostate 不变,并且返回 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 转换完成
partial 输出缓冲区空间不足或源缓冲区意外结束
error 遇到无法转换的字符
noconv 此 facet 是非转换的,未写入任何输出

非转换特化 std::codecvt<char, char, std::mbstate_t> 始终返回 std::codecvt_base::noconv

[编辑] 注解

要求 from <= from_end && to <= to_end 以及 state 表示初始移位状态或通过转换序列中前面的字符获得。

state 上的效果是故意未指定的。在标准 facets 中,它用于维护移位状态,例如在调用 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 76 C++98 不清楚是否需要转换来
支持一次生成一个内部字符
仅在被
basic_filebuf 使用时才需要

[编辑] 参见

[虚拟]
从关联的文件读取
(std::basic_filebuf<CharT,Traits> 的虚拟保护成员函数) [编辑]
将字节字符串转换为宽字符串
(std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc> 的公共成员函数) [编辑]
将窄多字节字符串转换为宽字符串,给定状态
(函数) [编辑]
[虚拟]
将字符串从 InternT 转换为 ExternT,例如在写入文件时
(虚拟保护成员函数) [编辑]