是否有从icu std::wstring
创建UnicodeString
的icu函数?我一直在搜索ICU手册,但找不到。
((我知道我可以将UnicodeString
转换为UTF8
,然后转换为依赖于平台的wchar_t*
,但我正在UnicodeString
中寻找一个可以进行此转换的功能。
C ++标准没有规定std::wstring
的任何特定编码。在Windows系统上,wchar_t
是16位,在Linux,macOS和其他几个平台上,wchar_t
是32位。就C ++的std::wstring
而言,它只是wchar_t
的任意序列,几乎与std::string
只是char
的任意序列一样。
icu::UnicodeString
似乎没有内置的创建std::wstring
的方法,但是如果您确实要创建std::wstring
,则可以使用基于C的API u_strToWCS()
,如下所示:
u_strToWCS()
[据推测,icu::UnicodeString ustr = /* get from somewhere */;
std::wstring wstr;
int32_t requiredSize;
UErrorCode error = U_ZERO_ERROR;
// obtain the size of string we need
u_strToWCS(nullptr, 0, &requiredSize, ustr.getBuffer(), ustr.length(), &error);
// resize accordingly (this will not include any terminating null character, but it also doesn't need to either)
wstr.resize(requiredSize);
// copy the UnicodeString buffer to the std::wstring.
u_strToWCS(wstr.data(), wstr.size(), nullptr, ustr.getBuffer(), ustr.length(), &error);
将使用最有效的方法从u_strToWCS()
转换为UChar
(如果它们的大小相同,那么我想它只是简单的副本)。