是否有使用std :: locale和C流功能的C等效mbsrtowcs和wcsrtombs類型函數?
我試圖找出使用標準庫在std :: string和std :: wstring之間來回轉換的最佳方法.似乎std :: locale幾乎可以做到這一點,但我對某些細節或其可能存在的侷限性有點不確定.
一些細節:我在Linux上,它使用utf-8作爲本機編碼.我想從utf-8 std :: string轉到std :: wstring並返回而不會丟失信息.
我認爲Windows上的語言環境可能存在一些限制,但我並不特別關注它們.只要答案適用於Linux並且沒有libstdc以外的依賴關係,即沒有提升依賴性,我很高興.
讚賞背景信息的鏈接.
注意:似乎有些混亂.多個char可以表示UTF-8中的單個字符,因此在從wchar_t轉換爲char時不考慮此問題的函數將不起作用.
解決方法:
locale對於此任務來說是過度的 – UTF-8和UTF-16可以通過簡單的二進制轉換來回轉換.這是基於我的answer to an earlier question的一些代碼.
std::string UTF16to8(const wchar_t * in)
{
std::string out;
if (in == NULL)
return out;
unsigned int codepoint = 0;
for (in; *in != 0; ++in)
{
if (*in >= 0xd800 && *in <= 0xdbff)
codepoint = ((*in - 0xd800) << 10) + 0x10000;
else
{
if (*in >= 0xdc00 && *in <= 0xdfff)
codepoint |= *in - 0xdc00;
else
codepoint = *in;
if (codepoint <= 0x7f)
out.append(1, static_cast<char>(codepoint));
else if (codepoint <= 0x7ff)
{
out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else if (codepoint <= 0xffff)
{
out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else
{
out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
codepoint = 0;
}
}
return out;
}
std::wstring UTF8to16(const char * in)
{
std::wstring out;
if (in == NULL)
return out;
unsigned int codepoint = 0;
int following = 0;
for (in; *in != 0; ++in)
{
unsigned char ch = *in;
if (ch <= 0x7f)
{
codepoint = ch;
following = 0;
}
else if (ch <= 0xbf)
{
if (following > 0)
{
codepoint = (codepoint << 6) | (ch & 0x3f);
--following;
}
}
else if (ch <= 0xdf)
{
codepoint = ch & 0x1f;
following = 1;
}
else if (ch <= 0xef)
{
codepoint = ch & 0x0f;
following = 2;
}
else
{
codepoint = ch & 0x07;
following = 3;
}
if (following == 0)
{
if (codepoint > 0xffff)
{
out.append(1, static_cast<wchar_t>(0xd800 + (codepoint >> 10)));
out.append(1, static_cast<wchar_t>(0xdc00 + (codepoint & 0x03ff)));
}
else
out.append(1, static_cast<wchar_t>(codepoint));
codepoint = 0;
}
}
return out;
}
如果你的wchar_t是32位而不是16位,那麼這是一個使用的版本(未經測試).
std::string UTF32to8(const wchar_t * in)
{
assert(sizeof(wchar_t) >= 4);
std::string out;
if (in == NULL)
return out;
for (in; *in != 0; ++in)
{
unsigned int codepoint = *in;
if (codepoint <= 0x7f)
out.append(1, static_cast<char>(codepoint));
else if (codepoint <= 0x7ff)
{
out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else if (codepoint <= 0xffff)
{
out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else
{
out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
}
return out;
}
std::wstring UTF8to32(const char * in)
{
assert(sizeof(wchar_t) >= 4);
std::wstring out;
if (in == NULL)
return out;
wchar_t codepoint = 0;
int following = 0;
for (in; *in != 0; ++in)
{
unsigned char ch = *in;
if (ch <= 0x7f)
{
codepoint = ch;
following = 0;
}
else if (ch <= 0xbf)
{
if (following > 0)
{
codepoint = (codepoint << 6) | (ch & 0x3f);
--following;
}
}
else if (ch <= 0xdf)
{
codepoint = ch & 0x1f;
following = 1;
}
else if (ch <= 0xef)
{
codepoint = ch & 0x0f;
following = 2;
}
else
{
codepoint = ch & 0x07;
following = 3;
}
if (following == 0)
{
out.append(1, codepoint);
codepoint = 0;
}
}
return out;
}
來源: https://codeday.me/bug/20190626/1298707.html