std:: text_encoding
|
定义于头文件
<text_encoding>
|
||
|
struct
text_encoding
;
|
(C++26 起) | |
text_encoding
类提供了一种识别字符编码的机制。它用于在编译时确定翻译环境的
普通字符字面量编码
,并在运行时确定执行环境的字符编码。
每个
text_encoding
对象封装一个
字符编码方案
,该方案通过
text_encoding::id
中的枚举值以及由空终止字节字符串表示的对应名称进行唯一标识。这些信息可分别通过
mib()
和
name()
成员函数获取。对象是否表示在翻译环境或执行环境中实现的字符编码方案由实现定义。
text_encoding
类属于
可平凡复制
类型。表示字符编码方案对应名称的数组对象
直接内嵌于
text_encoding
对象内部。存储的名称长度上限为
max_name_length
个字符(不包含空字符
'
\0
'
)。
该类支持已注册和未注册的字符编码。已注册编码指在 IANA字符集注册表 中列出的字符编码,但以下字符编码除外:
- NATS-DANO (33)
- NATS-DANO-ADD (34)
此外,该类为已注册的字符编码提供以下访问功能:
- 主名称 :注册表中指定的官方名称。
- 别名 :注册表中别名的实现定义超集。
- MIBenum 值 :用于标识编码字符集的唯一标识符。
未注册的编码可以用枚举值 id :: other 或 id :: unknown 及自定义名称来表示。
一个
text_encoding
对象
e
,当其MIBenum值既非
id
::
other
亦非
id
::
unknown
时,将维持以下不变式:
- * e. name ( ) ! = ' \0 ' 为 true ,且
- e. mib ( ) == std:: text_encoding ( e. name ( ) ) . mib ( ) 为 true 。
目录 |
成员类型
|
表示字符编码的MIBenum值
(公开成员枚举) |
|
字符编码别名的
view
视图
(公开成员类) |
成员常量
| 名称 | 值 |
|
constexpr
std::
size_t
max_name_length
[静态]
|
63
(公开静态成员常量) |
数据成员
| 成员 | 描述 |
std
::
text_encoding
::
id
mib_
(private)
|
以
id
::
unknown
作为默认值的 MIBenum 值
( 仅说明用途的成员对象* ) |
char
[
max_name_length
+
1
]
name_
(private)
|
存储的主名称
( 仅说明用途的成员对象* ) |
成员函数
创建操作 |
|
构造新的
text_encoding
对象
(公开成员函数) |
|
|
[static]
|
构造新的
text_encoding
,表示
普通字符字面量编码
(公开静态成员函数) |
|
[static]
|
构造新的
text_encoding
,表示执行环境的实现定义字符编码方案
(公开静态成员函数) |
观察器 |
|
|
返回当前字符编码的 MIBenum 值
(公开成员函数) |
|
|
返回当前字符编码的主名称
(公开成员函数) |
|
返回当前字符编码别名的
view
(公开成员函数) |
|
|
[static]
|
检查执行环境的字符编码方案是否与指定 MIB 值匹配
(公开静态成员函数) |
辅助功能 |
|
|
[static]
(private)
|
使用
字符集别名匹配
比较两个别名名称
( 仅用于说明的静态成员函数* ) |
非成员函数
|
(C++26)
|
比较两个
text_encoding
对象。
(公开成员函数) |
辅助类
|
(C++26)
|
std::text_encoding
的哈希支持
(类模板特化) |
注释
在处理字符编码时,需注意两个不同已注册字符编码的主名称或别名,按照Unicode技术标准中描述的 字符集别名匹配 规则进行比较时并不等效。
为方便起见,
text_encoding::id
的枚举项作为
text_encoding
的成员直接引入,可直接访问。这意味着
text_encoding
::
ASCII
与
text_encoding
::
id
::
ASCII
指向同一实体。
建议实现应将已注册编码视为不可互换的。 此外,除非存在明确先例,否则不应使用已注册编码的主名称来描述相似但不同的未注册编码。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_text_encoding
|
202306L
|
(C++26) |
std::text_encoding
|
示例
#include <locale> #include <print> #include <text_encoding> int main() { // 字面量编码在编译时已知 constexpr std::text_encoding literal_encoding = std::text_encoding::literal(); // 检查字面量编码 static_assert(literal_encoding.mib() != std::text_encoding::other && literal_encoding.mib() != std::text_encoding::unknown); // 环境编码仅在运行时可知 std::text_encoding env_encoding = std::text_encoding::environment(); // 默认区域设置的关联编码 std::text_encoding locale_encoding = std::locale("").encoding(); std::println("The literal encoding is {}", literal_encoding.name()); std::println("The aliases of literal encoding:"); for (const char* alias_name : literal_encoding.aliases()) std::println(" -> {}", alias_name); if (env_encoding == locale_encoding) std::println("Both environment and locale encodings are the same"); std::println("The environment encoding is {}", env_encoding.name()); std::println("The aliases of environment encoding:"); for (const char* alias_name : env_encoding.aliases()) std::println(" -> {}", alias_name); }
可能的输出:
The literal encoding is UTF-8 The aliases of literal encoding: -> UTF-8 -> csUTF8 Both environment and locale encodings are the same The environment encoding is ANSI_X3.4-1968 The aliases of environment encoding: -> US-ASCII -> iso-ir-6 -> ANSI_X3.4-1968 -> ANSI_X3.4-1986 -> ISO_646.irv:1991 -> ISO646-US -> us -> IBM367 -> cp367 -> csASCII -> ASCII
参见
|
封装文化差异的多态facet集合
(类) |