std::codecvt<InternT,ExternT,StateT>::length, do_length
本地环境与平面 | |||||||||||||||||||||
本地环境 | |||||||||||||||||||||
平面类别基类 | |||||||||||||||||||||
ctype(字符类别)平面 | |||||||||||||||||||||
numeric(数值)平面 | |||||||||||||||||||||
collate(对照比较)平面 | |||||||||||||||||||||
time(时间)平面 | |||||||||||||||||||||
monetary(货币)平面 | |||||||||||||||||||||
messages(消息)平面 | |||||||||||||||||||||
字符分类与转换 | |||||||||||||||||||||
字符分类 | |||||||||||||||||||||
转换 | |||||||||||||||||||||
|
|||||||||||||||||||||
编码转换平面 | |||||||||||||||||||||
|
|
||||||||||||||||||||
C 本地环境 | |||||||||||||||||||||
在标头
<locale>
定义
|
||
public:
int length( StateT& state, const
ExternT* from, const
ExternT* from_end, |
(1) | |
protected:
virtual int
do_length( StateT&
state, const ExternT*
from, const ExternT*
from_end, |
(2) | |
do_length
。[from, from_end)
定义的字符数组的 ExternT
字符,到最多 max 个 InternT
字符,并返回这种转换会消耗的
ExternT
字符数。如同以对某虚构的 [to, to + max)
输出缓冲区执行 do_in(state, from, from_end,
from, to, to + max, to)
一般修改 state。返回值
假如以 do_in()
转换直至消耗所有 from_end - from 个字符,或产生 max 个 InternT
字符,或出现转换错误时消耗的
ExternT
字符数。
非转换特化 std::codecvt<char, char, std::mbstate_t> 返回 std::min(max, from_end - from)。
示例
#include <locale> #include <string> #include <iostream> int main() { using facet_type = std::codecvt<wchar_t, char, std::mbstate_t>; // 窄多字节编码 std::string s = "z\u00df\u6c34\U0001d10b"; // 或 u8"zß水𝄋" // 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; facet_type& codecvt_facet = std::use_facet<facet_type>(std::locale("en_US.utf8")); std::mbstate_t mb = std::mbstate_t(); std::cout << s.size() << " 个字节中只有开头的 " << codecvt_facet.length(mb, &s[0], &s[s.size()], 2) << " 个字节会被消耗,以产生开头的 2 个字符\n"; }
输出:
10 个字节中只有开头的 3 个字节会被消耗,以产生开头的 2 个字符
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 75 | C++98 | 未指明对 state 的影响
|
已指明 |
LWG 305 | C++98 | std::codecvt<char, char, std::mbstate_t>::do_length 要求返回 std::min(max, from_end - from) |
不再要求 |
参阅
[虚]
|
将字符串从 ExternT 转换到 InternT ,例如在从文件读取时 (虚受保护成员函数) |