c++ - 这种转换 Unicode 字符的方法是否真的不可知字节序?

标签 c++ c++11 endianness

我只是对我自己的代码(当然还有测试)有点怀疑,并希望有人验证这是否是真正的不可知字节序的方法。

在跨平台项目内部,我使用 UTF-32 (std::u32string) 作为字符串类型。但为了更轻松地处理不同平台上的 I/O,我在将任何文本发送到文件或通过网络发送之前将 UTF-32 转换为 UTF-8。

我会说这种方法与字节序无关。 UTF-8 是一种面向字节的编码,这意味着计算机的字节顺序不会影响字节流。 32 位大字符在发送到流之前按照它们在字符串中出现的顺序转换为 UTF-8。

这是来自 Json String 类的代码片段,以提供我正在做的事情的示例

/**
*   Provides conversion facilities between UTF-8 and Unicode32 strings.
*/
typedef std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> UnicodeConverter;

/**
* Converts the JSON value to a valid JSON string.
* @param the UTF-8 stream to write to.
* @returns The UTF-8 stream.
*/
inline std::ostream& VToJson(std::ostream& os) const override { return EscapeJsonString(os << STRING_JSON_QUOTE) << STRING_JSON_QUOTE; }

/**
*   Streams a json string in its escaped form.
*   @param os the UTF-8 stream to write to.
*   @returns the original stream.
*/
std::ostream& JsonString::EscapeJsonString(std::ostream &os) const 
{
    UnicodeConverter conv;
    for each (char32_t c in i_Value)
    {
        // Check if character is a special character
        if (c == '\\')
            // Output escaped rev solidus
            os << "\\\\";
        else if (c == '/')
            // Output escaped solidus
            os << "\\/";
        else if (c == '\b')
            // Output escaped backspace
            os << "\\b";
        else if (c == '\f')
            // Output escaped formfeed
            os << "\\f";
        else if (c == '\n')
            // Output escaped new line
            os << "\\n";
        else if (c == '\r')
            // Output secaped carriage return
            os << "\\r";
        else if (c == '\t')
            // Output escaped tab
            os << "\\t";
        else if (is_unicode_control(c))
        {
            // Output escape
            os << "\\u";

            // Output hex representation
            std::stringstream str;
            str << std::setfill('0') << std::setw(4) << std::hex << c;
            os << str.str();
        }
        else
            // Normal character
            os << conv.to_bytes(c);
    }
    return os;
}

最佳答案

一般来说,这种方法可以实现字节序不可知,因为 UTF-32 仅用于具有相同字节序的系统,而在它与可能具有不同字节序的系统接口(interface)的每种情况下都使用 UTF-8 - 并且UTF-8 建立在字节流之上(因此没有字节顺序)。

但是,转换本身对字节顺序敏感,必须正确实现,以便字节顺序不会成为问题(例如,没有 memcopy,而是算术移位)。假设您的标准库实现正确执行此转换应该是合理的。

添加一些关于为什么此代码不受字节序 (22.5/4) 影响的说明:

For the facet codecvt_utf8:
- The facet shall convert between UTF-8 multibyte sequences and UCS2 or UCS4 (depending on the size of Elem) within the program.
- Endianness shall not affect how multibyte sequences are read or written.
- The multibyte sequences may be written as either a text or a binary file.

codecvt_mode 枚举类型的 endianess 成员仅用于读/写 UTF-16 和 UTF-32 多字节序列。

关于c++ - 这种转换 Unicode 字符的方法是否真的不可知字节序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621301/

相关文章:

c++ - 使用引用而不是重载运算符

尝试抛出异常时出现c++模板化函数错误

c++ - 将析构函数声明为 virtual 就足够了吗?

c++ - V8 中 Integer 和 Double 的区别

c++ - 什么是 lambda 函数类型? C++

Netty 和字节顺序

c++ - 使用新位图修改 CreateToolbarEx 函数时出现内存分配问题

c++ - 假设 C++11 中已知子级布局,重新解释基类是否安全?

c - 将 8 字节值转换为实际数字 :

c - 确定 C 编译器是小端还是大端