c++ - 此 UTF-8 实现是实现定义的还是定义明确的?

标签 c++ string unicode utf-8

我只是四处浏览寻找一些 UTF-8 代码点的实现(不,不是抄袭)并且偶然发现了 this :

typedef unsigned char char8_t;
typedef std::basic_string<unsigned char> u8string;

这段代码是否忽略了 CHAR_BIT 只需要至少 8,但可能更大这一事实?还是在这种情况下这无关紧要并且代码很好?如果是,那这是为什么?

此外,有人(大概是 SO 成员@NicolBolas?)写道:

const char *str = u8"This is a UTF-8 string.";

This is pretty much how UTF-8 will be used in C++ for string literals.

我以为 UTF-8 中的一个代码单元总是恰好是八位!
来自 Unicode 标准 8.0.0,第 2.5 章:

In the Unicode character encoding model, precisely defined encoding forms specify how each integer (code point) for a Unicode character is to be expressed as a sequence of one or more code units. The Unicode Standard provides three distinct encoding forms for Unicode characters, using 8-bit, 16- bit, and 32-bit units. These are named UTF-8, UTF-16, and UTF-32, respectively.

(删除了换行符,删除了换行符上的连字符,添加了强调。)

那么他为什么声称使用了 const char* 而不是 const uint8_t*(或建议的假设性 const char8_t*)?

最佳答案

uint8_t 仅存在于内存可以恰好 8 位访问的系统上。 UTF-8 没有任何这样的要求。它使用适合 8 位的值,但不对这些值的实际存储方式施加任何要求。每个 8 位值都可以存储为 16 位或 32 位或任何对其运行的系统有意义的存储;唯一的要求是该值必须正确。

关于c++ - 此 UTF-8 实现是实现定义的还是定义明确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560711/

相关文章:

c++ - 试图将 push_back 放入指向抽象类的 vector 中

C++:矩阵类

c - 编辑字符串(字符数组)复制到输入字符串(scanf 或 fgets.. gets)可能吗?

python - ReportLab 中的 Unicode 处理

C#使用UTF8编码发送unicode短信

c++ - 枚举成员的值可以自动递减而不是递增吗?

c++ - 提高 Rcpp 编译速度

c++ - 是否可以在 C++ 中进行返回交换,而不是按值返回?

java - 如何使用 Java 在文本文件中复制以特定字符开头和结尾的字符串

windows - 在 Windows 中识别 unicode 编码文本文件的最佳方法是什么?