【问题标题】:Bug in gcc wstring_convert?gcc wstring_convert中的错误?
【发布时间】:2021-09-23 15:16:46
【问题描述】:

我使用 MinGW 8.1.0 64 位。这段代码sn-p:

#include <clocale>
#if __has_include(<codecvt>)
#include <codecvt>
#endif
#include <cstdlib>
#include <locale>
#include <string>
#include <wchar.h>
#include <iostream>

int main() {
    auto utf8_decode = [](const std::string &str) -> std::wstring {
      std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
      return myconv.from_bytes(str);
    };

    std::string test = "=";
    auto s = utf8_decode(test);

    std::wcout << s << std::endl;

    return 0;
}

在 Windows 上输出象形文字(或一些乱码),但在 Linux 上输出 =(如预期)。 这是标准库中的错误还是我遗漏了什么?

【问题讨论】:

  • 听起来更像是环境中的“错误”...当您在 Windows 上运行时,您是否使用旧的“命令行”窗口(也称为“DOS 提示符”)?因为 IIRC 不能很好地处理 Unicode。
  • @Someprogrammerdude 我实际上可以在调试器中看到s 指向胡言乱语;计算它的长度也是不正确的,没有实际输出到标准输出。
  • @Amomum 定义“胡言乱语”。因为您认为“乱码”可能只是您不理解的编码。原始数据实际上是什么样的?实际尺寸是多少?
  • @RemyLebeau 好吧,在 linux 上它看起来就像 = - 我想它应该总是这样,因为该符号是 ASCII 的一部分并且在 utf8 中应该是相同的。还是这样?
  • 是的,= 在 ASCII 和 UTF-8 中的编码方式相同。但是你没有回答我的问题——你实际上看到了什么“胡言乱语”,你在哪里看到的?在调试器中?在航站楼?

标签: c++ windows gcc mingw wstring


【解决方案1】:

看来这确实是a bug in MinGW libstdc++.dll; codecvt 错误地选择了大端,所以= (0x3d) 变成了 (0x3d00)。

建议的解决方法 - 使用 codecvt_utf8&lt;wchar_t, 0x10ffff, std::little_endian&gt; 手动强制 little-endian

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2010-09-17
    • 2011-08-10
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    相关资源
    最近更新 更多