文本处理库
第 28 章导读:数值转换、locale、编码识别、格式化、正则和 C 字符串工具各管什么;字面量编码与执行环境编码的区别;新设施默认与 locale 无关的设计趋势。
本节要点
- 第 28 章收录处理文本内容的库:
<charconv>、<locale>、<text_encoding>、<format>、<regex>和 C 的字符串工具。 - 程序里至少有两种编码:字面量编码在编译时确定,执行环境编码在运行时由 C locale 决定,两者可以毫无关系。
- 依赖 locale 的 C 函数(
isalpha、mbrtowc、printf)按当前 C locale 的编码解读字节,而不是按编译器给字面量用的编码。 - C++17 以后加入的文本设施默认与 locale 无关:
to_chars、std::format、C++26 的to_string(double),只有显式要求时才本地化。
本章的组成 [text.general]#
[text.general] 只有一张表(表 89),列出六个子条款:
| 子条款 | 头文件 | 本站笔记 | 内容 |
|---|---|---|---|
| [charconv] | <charconv> |
基础数值转换 | 与 locale 无关、不抛异常、不分配内存的数值与文本互转 |
| [localization] | <locale>、<clocale> |
locale 与 facet、标准 facet、C 库 locale | C++98 的本地化框架:一个 locale 是一组按类型索引的 facet |
| [text.encoding] | <text_encoding> |
文本编码识别 | C++26 查询字面量编码和执行环境编码 |
| [format] | <format> |
格式化库、格式串、formatter | 类型安全、编译期检查格式串的 std::format |
| [re] | <regex> |
正则表达式、匹配与结果 | C++11 的 std::regex |
| [text.c.strings] | <cctype>、<cwctype>、<cwchar>、<cuchar>、<cstdlib> |
C 字符串工具 | C 库的字符分类,以及多字节与宽字符的转换 |
format 对数值的默认输出是借 to_chars 定义的;format 的 L 选项又通过 locale 的 numpunct facet 取得千位分隔符。所以这几部分并不独立,charconv 是最底层。
两种编码#
字面量编码:编译时确定#
#include <cstdio>
#include <text_encoding>
int main() {
constexpr auto lit = std::text_encoding::literal(); // 编译期就确定
static_assert(lit == std::text_encoding::UTF8); // Clang/GCC 默认;MSVC 需 /utf-8
static_assert(sizeof("中") == 4); // 3 个 UTF-8 code unit + '\0'
// 运行期:取决于进程环境(POSIX 上是 "" locale 对应的编码)
std::printf("environment: %s\n", std::text_encoding::environment().name());
}本机(Clang 23 + libc++,未设置 LANG)运行输出 environment: US-ASCII;设置 LANG=zh_CN.GB18030 后输出 GB18030。两种情况下字面量编码都是 UTF-8。
[lex.charset]/4 规定,字符字面量和字符串字面量中的字符被编码成一个或多个 code unit(编码单元,某个字符类型的整数值),所用的编码叫这种字面量的字面量编码(literal encoding)。无前缀的普通字面量用普通字面量编码,L 前缀的用宽字面量编码。
[lex.charset]/5 只给了少量保证:基本字面量字符集中的每个字符编成单个非负的 code unit,空字符编成 0,数字 '0' 到 '9' 的值连续。其余由实现定义。u8、u、U 前缀的字面量则固定为 UTF-8、UTF-16、UTF-32。
字面量编码在编译时确定,写进了目标文件。Clang 和 GCC 默认用 UTF-8,可以用 -fexec-charset= 修改;MSVC 默认跟随系统代码页,加 /utf-8 才是 UTF-8。这套术语是 C++23 的 P2314R4 整理出来的,此前标准用的是含义模糊的 execution character set。
执行环境编码:运行时随 locale 变化#
库函数解读 char 序列时用的是另一套编码。[character.seq.general]/1.2 定义了执行字符集(execution character set):它是基本字面量字符集的超集,编码是 locale-specific 的,也就是会随 setlocale 或 std::locale::global 在运行时改变(¶1.1)。
标准原文
The encodings of the execution character sets can be unrelated to any literal encoding.
执行字符集的编码可以与任何字面量编码都没有关系。
mbrtowc、isalpha、printf("%ls")、std::codecvt<wchar_t, char, mbstate_t> 解读字节时,依据的是当前 C locale 的编码。一个以 UTF-8 字面量编码编译的程序在 LANG=C 下运行时,"中" 的三个字节在 C 库看来可能是三个无关的单字节字符,也可能是非法序列(见 [text.c.strings] 的例子)。两种编码不一致时,把字面量原样交给依赖 locale 的 C 函数就会得到乱码。
C++26 的 std::text_encoding([text.encoding],P1885R12)让程序第一次能在标准范围内查询这两种编码:literal() 是 consteval 的,返回字面量编码;environment() 在运行时返回环境的编码。
libc++ 与 macOS 的可用性标注
libc++ 的 text_encoding::environment() 实现在动态库里,并用可用性宏标注为 LLVM 23 才提供。在 macOS 上链接系统自带的 libc++ 时,调用它会报错 'environment' is unavailable。本站的例子是加 -D_LIBCPP_DISABLE_AVAILABILITY 并链接 Homebrew LLVM 自带的 libc++ 验证的。literal() 只在头文件里实现,不受影响。
新设施默认与 locale 无关#
C++98 的本地化模型默认本地化:iostream 的每次格式化都查询流上 imbue 的 locale,C 的 printf、strtod 查询全局 C locale。这带来三个问题:
- 结果不可预测:任何一个库调用
setlocale(LC_ALL, ""),JSON 序列化器就可能输出1,5而不是1.5。 - 慢:每次格式化都要经过虚函数调用和 facet 查找。
- 线程不安全:
setlocale与几十个 C 库函数之间存在数据竞争([c.locales])。
C++17 之后新增的文本设施反过来,默认与 locale 无关,只在显式要求时本地化:
#include <charconv>
#include <cstdio>
#include <format>
#include <locale>
#include <string>
int main() {
std::locale::global(std::locale("de_DE.UTF-8")); // 有名字,所以也会 setlocale(LC_ALL, ...)
char buf[32];
std::snprintf(buf, sizeof buf, "%.1f", 1.5);
std::printf("printf : %s\n", buf); // 1,5:跟随 C locale
auto r = std::to_chars(buf, buf + sizeof buf, 1.5);
std::printf("to_chars : %.*s\n", int(r.ptr - buf), buf); // 1.5:永远是 "C"
std::printf("format {} : %s\n", std::format("{}", 1.5).c_str()); // 1.5
std::printf("format {:L}: %s\n", std::format("{:L}", 1234567.5).c_str()); // 1.234.567,5
std::printf("to_string : %s\n", std::to_string(1.5).c_str());
}| 设施 | 默认行为 | 如何本地化 |
|---|---|---|
to_chars/from_chars(C++17,P0067R5) |
永远按 "C" locale |
不能 |
std::format(C++20,P0645R10) |
与 locale 无关 | 格式说明里写 L,可另传 std::locale 实参([format.string.std]/17) |
std::print(C++23) |
同 format |
同上 |
to_string(double) |
C++26 起定义为 format("{}", val)(P2587R3) |
不能 |
本机最后一行输出 1,500000。草案的 [string.conversions] 已规定浮点 to_string 返回 format("{}", val),应输出 1.5;截至写作时 libc++ 仍按旧规则用 sprintf("%f") 实现,结果随 C locale 变化。
locale 并没有被废弃。std::locale、facet 和 iostream 的本地化仍是规范的一部分,{:L} 背后用的也是 numpunct facet。变化在于默认值:需要本地化的地方显式开启,其余地方得到确定、可往返的输出。
另一方面,C++26 删除了 <codecvt> 头文件(codecvt_utf8 等)以及 wstring_convert、wbuffer_convert([diff.cpp23.depr])。标准目前没有替代它们的 Unicode 转码设施,text_encoding 只识别编码,不做转换。
思考题#
思考题 1
以 UTF-8 字面量编码编译的程序里,std::isalpha('é') 有什么问题?
答案
'é' 在 UTF-8 下需要两个 code unit,放不进一个 char。按 [lex.ccon],这样的普通字符字面量是ill-formed,Clang 报错 character too large for enclosing character literal type。
改写成 std::isalpha(s[0])(s 是 "é")也不对。s[0] 是 0xC3,在 char 有符号的平台上是负值,传给 <cctype> 的 isalpha 是 UB,因为 C 标准要求实参能用 unsigned char 表示或等于 EOF。即使先转成 unsigned char,结果也取决于当前 C locale 的编码([character.seq.general]/1.2),与字面量编码无关。
思考题 2
为什么 text_encoding::literal() 可以是 consteval,而 environment() 连 constexpr 都不是?
答案
字面量编码是翻译时确定的实现定义属性([lex.charset]/5),编译器在编译时就知道。Clang 通过预定义宏 __clang_literal_encoding__ 把它告诉标准库。
执行环境的编码取决于程序运行时的进程环境,编译时无从知道。[text.encoding.members]/14 规定在 POSIX 上它是 "" locale 的编码;注 2 说明它不受 setlocale 影响,反映的是环境,而不是当前的 C locale。
小结#
| 编码 | 何时确定 | 由谁决定 | 出处 |
|---|---|---|---|
| 普通、宽字面量编码 | 编译时 | 实现(编译选项) | [lex.charset]/4 |
u8、u、U 字面量编码 |
编译时 | 固定为 UTF-8/16/32 | [lex.charset]/5 |
| 执行字符集编码 | 运行时,可变 | 当前 C locale | [character.seq.general]/1.2 |
| 环境编码 | 运行时 | 进程环境(POSIX 上是 "" locale) |
[text.encoding.members]/14 |
某个 std::locale 的编码 |
运行时 | 该 locale 对象 | [locale.members]/7 |
条款一览
- 28.1General 概述[text.general]
- 28.2Primitive numeric conversions 基本数值转换[charconv]完成
- 28.3Localization library 本地化库[localization]完成
- 28.3.1General 概述[localization.general]
- 28.3.2Header <locale> synopsis 头文件 <locale> 概要[locale.syn]
- 28.3.3Locales 区域设置[locales]
- 28.3.3.1Class locale 类 locale[locale]
- 28.3.3.2locale globals locale 全局函数[locale.global.templates]
- 28.3.3.3Convenience interfaces 便利接口[locale.convenience]
- 28.3.4Standard locale categories 标准 locale 类别[locale.categories]完成
- 28.3.4.1General 概述[locale.categories.general]
- 28.3.4.2The ctype category ctype 类别[category.ctype]
- 28.3.4.2.1General 概述[category.ctype.general]
- 28.3.4.2.2Class template ctype 类模板 ctype[locale.ctype]
- 28.3.4.2.3Class template ctype_byname 类模板 ctype_byname[locale.ctype.byname]
- 28.3.4.2.4ctype<char> specialization ctype<char> 特化[facet.ctype.special]
- 28.3.4.2.5Class template codecvt 类模板 codecvt[locale.codecvt]
- 28.3.4.2.6Class template codecvt_byname 类模板 codecvt_byname[locale.codecvt.byname]
- 28.3.4.3The numeric category 数值类别[category.numeric]
- 28.3.4.4The numeric punctuation facet 数值标点刻面[facet.numpunct]
- 28.3.4.5The collate category 排序规则类别(collate)[category.collate]
- 28.3.4.6The time category 时间类别[category.time]
- 28.3.4.6.1General 概述[category.time.general]
- 28.3.4.6.2Class template time_get 类模板 time_get[locale.time.get]
- 28.3.4.6.3Class template time_get_byname 类模板 time_get_byname[locale.time.get.byname]
- 28.3.4.6.4Class template time_put 类模板 time_put[locale.time.put]
- 28.3.4.6.5Class template time_put_byname 类模板 time_put_byname[locale.time.put.byname]
- 28.3.4.7The monetary category 货币类别[category.monetary]
- 28.3.4.7.1General 概述[category.monetary.general]
- 28.3.4.7.2Class template money_get 类模板 money_get[locale.money.get]
- 28.3.4.7.3Class template money_put 类模板 money_put[locale.money.put]
- 28.3.4.7.4Class template moneypunct 类模板 moneypunct[locale.moneypunct]
- 28.3.4.7.5Class template moneypunct_byname 类模板 moneypunct_byname[locale.moneypunct.byname]
- 28.3.4.8The message retrieval category 消息检索类别[category.messages]
- 28.3.5C library locales C 库区域设置[c.locales]完成
- 28.4Text encodings identification 文本编码识别[text.encoding]完成
- 28.4.1Header <text_encoding> synopsis 头文件 <text_encoding> 概要[text.encoding.syn]
- 28.4.2Class text_encoding 类 text_encoding[text.encoding.class]
- 28.4.2.1Overview 概览[text.encoding.overview]
- 28.4.2.2General 概述[text.encoding.general]
- 28.4.2.3Members 成员[text.encoding.members]
- 28.4.2.4Comparison functions 比较函数[text.encoding.cmp]
- 28.4.2.5Class text_encoding::aliases_view 类 text_encoding::aliases_view[text.encoding.aliases]
- 28.4.2.6Enumeration text_encoding::id 枚举 text_encoding::id[text.encoding.id]
- 28.4.2.7Hash support 散列支持[text.encoding.hash]
- 28.5Formatting 格式化[format]完成
- 28.5.1Header <format> synopsis 头文件 <format> 概要[format.syn]
- 28.5.2Format string 格式字符串[format.string]草稿
- 28.5.3Error reporting 错误报告[format.err.report]
- 28.5.4Class template basic_format_string 类模板 basic_format_string[format.fmt.string]
- 28.5.5Formatting functions 格式化函数[format.functions]
- 28.5.6Formatter 格式化器[format.formatter]草稿
- 28.5.6.1Formatter requirements 格式化器要求[formatter.requirements]
- 28.5.6.2Formatter locking 格式化器加锁[format.formatter.locking]
- 28.5.6.3Concept formattable 概念 formattable[format.formattable]
- 28.5.6.4Formatter specializations 格式化器特化[format.formatter.spec]
- 28.5.6.5Formatting escaped characters and strings 格式化转义字符与字符串[format.string.escaped]
- 28.5.6.6Class template basic_format_parse_context 类模板 basic_format_parse_context[format.parse.ctx]
- 28.5.6.7Class template basic_format_context 类模板 basic_format_context[format.context]
- 28.5.7Formatting of ranges 范围的格式化[format.range]
- 28.5.7.1Variable template format_kind 变量模板 format_kind[format.range.fmtkind]
- 28.5.7.2Class template range_formatter 类模板 range_formatter[format.range.formatter]
- 28.5.7.3Class template range-default-formatter 类模板 range-default-formatter[format.range.fmtdef]
- 28.5.7.4Specialization of range-default-formatter for maps range-default-formatter 针对映射的特化[format.range.fmtmap]
- 28.5.7.5Specialization of range-default-formatter for sets range-default-formatter 针对集合的特化[format.range.fmtset]
- 28.5.7.6Specialization of range-default-formatter for strings range-default-formatter 针对字符串的特化[format.range.fmtstr]
- 28.5.8Arguments 格式化实参[format.arguments]
- 28.5.9Tuple formatter 元组格式化器[format.tuple]
- 28.5.10Class format_error 类 format_error[format.error]
- 28.6Regular expressions library 正则表达式库[re]草稿
- 28.6.1General 概述[re.general]
- 28.6.2Requirements 要求[re.req]
- 28.6.3Header <regex> synopsis 头文件 <regex> 概要[re.syn]
- 28.6.4Namespace std::regex_constants 命名空间 std::regex_constants[re.const]
- 28.6.5Class regex_error 类 regex_error[re.badexp]
- 28.6.6Class template regex_traits 类模板 regex_traits[re.traits]
- 28.6.7Class template basic_regex 类模板 basic_regex[re.regex]
- 28.6.8Class template sub_match 类模板 sub_match[re.submatch]
- 28.6.9Class template match_results 类模板 match_results[re.results]
- 28.6.9.1General 概述[re.results.general]
- 28.6.9.2Constructors 构造函数[re.results.const]
- 28.6.9.3State 状态[re.results.state]
- 28.6.9.4Size 大小[re.results.size]
- 28.6.9.5Element access 元素访问[re.results.acc]
- 28.6.9.6Formatting 格式化[re.results.form]
- 28.6.9.7Allocator 分配器[re.results.all]
- 28.6.9.8Swap 交换[re.results.swap]
- 28.6.9.9Non-member functions 非成员函数[re.results.nonmember]
- 28.6.10Regular expression algorithms 正则表达式算法[re.alg]草稿
- 28.6.11Regular expression iterators 正则表达式迭代器[re.iter]
- 28.6.12Modified ECMAScript regular expression grammar 修改后的 ECMAScript 正则表达式文法[re.grammar]
- 28.7Null-terminated sequence utilities 空终止序列工具[text.c.strings]完成
- 28.7.1Header <cctype> synopsis 头文件 <cctype> 概要[cctype.syn]
- 28.7.2Header <cwctype> synopsis 头文件 <cwctype> 概要[cwctype.syn]
- 28.7.3Header <cwchar> synopsis 头文件 <cwchar> 概要[cwchar.syn]
- 28.7.4Header <cuchar> synopsis 头文件 <cuchar> 概要[cuchar.syn]
- 28.7.5Multibyte / wide string and character conversion functions 多字节/宽字符串与字符转换函数[c.mb.wcs]