【问题标题】:`wide-exec-charset` dose not change the charset of `wchar_t``wide-exec-charset` 不会改变 `wchar_t` 的字符集
【发布时间】:2020-08-25 10:30:47
【问题描述】:

这是简单的 C++ 代码。

#include <iostream>
#include <locale>

int main() {
    std::wcout.sync_with_stdio(false);
    std::wcout.imbue(std::locale(""));
    std::cout << std::locale("").name() << std::endl; // C.UTF-8
    
    wchar_t s {L'\xd0b8'};
    std::wcout << s << std::endl;
    // expected: и
    // actual: 킸
}

  • '\xd0b8' 在 UTF-8 中是 и,在 UTF-16 中是

提到的reference

窄多字节字符串文字 (1) 和宽字符串文字 (2) 的编码是实现定义的。例如,gcc 使用命令行选项 -fexec-charset 和 -fwide-exec-charset 选择它们。

我在 Ubuntu 20.04.1 LTS (WSL) 中使用g++ -O3 -fwide-exec-charset=UTF-8 source.cc -o out.a &amp;&amp; out.a 编译并执行了代码,预期输出为и,但实际输出为。 g++ 不处理用于直接初始化宽字符s 的文字字符串中的十六进制。如何让g++考虑UTF-8中的宽字符?

系统信息:

> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-10ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)

【问题讨论】:

  • -fwide-exec-charset=UTF-8 没有多大意义。如果您想要宽字符编码,则选择像 UTF-16 这样的宽字符编码,而不是像 UTF-8 这样的多字节编码。事实上,你永远不应该使用任何宽字符。它是由微软和 Sun 早先做出的错误决定而产生的过时技术。
  • 我只是在测试wchar 并尝试了解它是如何工作的。我知道wchar 不可移植。

标签: c++ g++ c++17


【解决方案1】:

man gcc

  -fwide-exec-charset=charset
       Set the wide execution character set, used for wide string and character
       constants.  The default is UTF-32 or UTF-16, whichever corresponds to the width
       of "wchar_t".  As with -fexec-charset, charset can be any encoding supported by
       the system's "iconv" library routine; however, you will have problems with
       encodings that do not fit exactly in "wchar_t".

UTF-8 是一种多字节编码,不完全适合wchar_t。所以你已经得到了文档中指定的内容:问题。由于 Linux 上的sizeof(wchar_t) 为 4,因此您需要指定 4 字节宽的编码,例如 UTF-32。

除了与遗留代码接口之外,没有真正的理由将wchar_t 用于任何事情。

【讨论】:

  • 在将L'\xd0b8'更改为L'\x00010437'(UTF32BE中为?)并使用-fwide-exec-charset=UTF32LE编译后,预处理器仍然使用UTF32BE解码L'\x00010437'并打印?。似乎-fwide-exec-charset 也不适用于 UTF32LE 之类的 4 字节字符集。
  • \x00010437 是一个数字。无论使用什么字符集,它都表示为相同的位模式。试试L'?',你会发现它会根据-fwide-exec-charset 产生不同的位模式。您还将看到只有UTF32LE 的输出是?。这是因为标准库不知道您的特定源文件是使用特殊的-fwide-exec-charset 编译的。它不是程序范围的选项。标准库仍将宽字符解释为 UTF32LE。如果你想让你的整个程序在 UTF32BE 下工作,你需要用 UTF32BE 重新编译标准库。
  • 或者我猜你可以使用适当的std::codecvt&lt;wchar_t, ...&gt; facet。
  • 在尝试您的建议将L'\x00010437'更改为L'?'后,我终于意识到-fwide-exec-charset给出的字符集是从源文件转换为wchar_t的目标字符集。但是,我很好奇使用 UFT32BE 重新编译标准库是更改wcout 使用的字符集以读取wchar_t 中的字节的唯一方法,并且相应的源代码在哪里?我在 gcc 的存储库中探索了 libstdc++v3 几个小时,但我找不到将 UFT32LE 转换为 imbue 的字符集的代码。
  • 可能没有这样的代码,如果你需要这个转换你必须提供你自己的。你为什么需要这个?使用有效的方法,即char 和 UTF-8。
猜你喜欢
  • 1970-01-01
  • 2013-11-06
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
相关资源
最近更新 更多