我们什么时候应该更喜欢宽字符字符串?
When should we prefer wide-character strings?
我正在对一个大型遗留 MFC 代码库进行现代化改造,其中包含真正的字符串类型混合体:
- CString
- std::string
- std::wstring
- 字符*
- wchar_t*
- _bstr_t
我想在内部对单一字符串类型进行标准化,然后转换为其他类型仅当绝对需要时由第三方API(即 COM或 MFC 函数)。我和我的同事正在争论的问题;我们应该标准化哪种字符串类型?
我更喜欢 C++ 标准字符串之一:std::string 或 std::wstring。我个人倾向于 std::string,因为我们不需要任何宽字符 - 它是一个内部代码库,不面向客户 UI(即不需要多语言支持)。 "Plain" 字符串允许我们使用简单、朴素的字符串文字("Hello world" vs L"Hello world" 或 _T("Hello world")).
编程社区有官方立场吗?当面对多种字符串类型时,通常使用什么作为标准 'internal' 存储格式?
如果我们谈论 Windows,那么我会使用 std::wstring(因为我们经常需要很酷的字符串功能),或者 wchar_t* 如果您只是传递字符串。
注意微软在这里推荐:Working with Strings
Windows natively supports Unicode strings for UI elements, file names,
and so forth. Unicode is the preferred character encoding, because it
supports all character sets and languages. Windows represents Unicode
characters using UTF-16 encoding, in which each character is encoded
as a 16-bit value. UTF-16 characters are called wide characters, to
distinguish them from 8-bit ANSI characters. The Visual C++ compiler
supports the built-in data type wchar_t for wide characters
另外:
When Microsoft introduced Unicode support to Windows, it eased the
transition by providing two parallel sets of APIs, one for ANSI
strings and the other for Unicode strings. [...] Internally, the ANSI
version translates the string to Unicode.
另外:
New applications should always call the Unicode versions. Many world
languages require Unicode. If you use ANSI strings, it will be
impossible to localize your application. The ANSI versions are also
less efficient, because the operating system must convert the ANSI
strings to Unicode at run time. [...] Most newer APIs in Windows have
just a Unicode version, with no corresponding ANSI version.
视情况而定。
为 Windows 编程时,我建议至少使用 std::wstring
:
- 资源(字符串、对话框等)
- 文件系统访问(Windows 允许文件和目录名称中的非 ASCII 字符(包括所有 "wrong kinds of apostrophes" 顺便说一句),这些无法使用 ANSI API 打开)
- COM(BSTR 总是宽字符)
- 其他面向用户的界面(剪贴板、系统错误报告等)
但是,使用单字符字符串处理内部 ASCII 数据文件和UTF-8-encoded-data更容易。它快速、高效且直接。
可能还有问题中未提及的其他方面,例如数据库或使用的 APIs、input/output 文件等及其字符集 - 所有这些都起作用在决定工作的最佳数据结构时。
"UTF-8 everywhere" 总的来说是个好主意。但是有 0 Windows API 采用 UTF-8。即使 std::experimental::filesystem
API 在 Windows 上使用 std::wstring
,在 POSIX 上使用 std::string
。
我正在对一个大型遗留 MFC 代码库进行现代化改造,其中包含真正的字符串类型混合体:
- CString
- std::string
- std::wstring
- 字符*
- wchar_t*
- _bstr_t
我想在内部对单一字符串类型进行标准化,然后转换为其他类型仅当绝对需要时由第三方API(即 COM或 MFC 函数)。我和我的同事正在争论的问题;我们应该标准化哪种字符串类型?
我更喜欢 C++ 标准字符串之一:std::string 或 std::wstring。我个人倾向于 std::string,因为我们不需要任何宽字符 - 它是一个内部代码库,不面向客户 UI(即不需要多语言支持)。 "Plain" 字符串允许我们使用简单、朴素的字符串文字("Hello world" vs L"Hello world" 或 _T("Hello world")).
编程社区有官方立场吗?当面对多种字符串类型时,通常使用什么作为标准 'internal' 存储格式?
如果我们谈论 Windows,那么我会使用 std::wstring(因为我们经常需要很酷的字符串功能),或者 wchar_t* 如果您只是传递字符串。
注意微软在这里推荐:Working with Strings
Windows natively supports Unicode strings for UI elements, file names, and so forth. Unicode is the preferred character encoding, because it supports all character sets and languages. Windows represents Unicode characters using UTF-16 encoding, in which each character is encoded as a 16-bit value. UTF-16 characters are called wide characters, to distinguish them from 8-bit ANSI characters. The Visual C++ compiler supports the built-in data type wchar_t for wide characters
另外:
When Microsoft introduced Unicode support to Windows, it eased the transition by providing two parallel sets of APIs, one for ANSI strings and the other for Unicode strings. [...] Internally, the ANSI version translates the string to Unicode.
另外:
New applications should always call the Unicode versions. Many world languages require Unicode. If you use ANSI strings, it will be impossible to localize your application. The ANSI versions are also less efficient, because the operating system must convert the ANSI strings to Unicode at run time. [...] Most newer APIs in Windows have just a Unicode version, with no corresponding ANSI version.
视情况而定。
为 Windows 编程时,我建议至少使用 std::wstring
:
- 资源(字符串、对话框等)
- 文件系统访问(Windows 允许文件和目录名称中的非 ASCII 字符(包括所有 "wrong kinds of apostrophes" 顺便说一句),这些无法使用 ANSI API 打开)
- COM(BSTR 总是宽字符)
- 其他面向用户的界面(剪贴板、系统错误报告等)
但是,使用单字符字符串处理内部 ASCII 数据文件和UTF-8-encoded-data更容易。它快速、高效且直接。
可能还有问题中未提及的其他方面,例如数据库或使用的 APIs、input/output 文件等及其字符集 - 所有这些都起作用在决定工作的最佳数据结构时。
"UTF-8 everywhere" 总的来说是个好主意。但是有 0 Windows API 采用 UTF-8。即使 std::experimental::filesystem
API 在 Windows 上使用 std::wstring
,在 POSIX 上使用 std::string
。