'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)

'std::wstring_convert' to convert as much as possible (from a UTF8 file-read chunk)

本文关键字:UTF8 文件 读取 wstring std convert 尽可能 转换      更新时间:2023-10-16

我正在从 utf-8 文本文件中获取文本,并按块进行以提高性能。

std::ifstream.read(myChunkBuff_str, myChunkBuff_str.length())

这是一个更详细的例子

我每个块大约有 16,000 个字符。 我的下一步是将此std::string转换为可以让我单独处理这些"复杂字符"的东西,从而将该std::string转换为std::wstring

我正在使用以下函数进行转换,从这里获取:

#include <string>
#include <codecvt>
#include <locale>
std::string narrow (const std::wstring& wide_string)
{
std::wstring_convert <std::codecvt_utf8 <wchar_t>, wchar_t> convert;
return convert.to_bytes (wide_string);
}
std::wstring widen (const std::string& utf8_string)
{
std::wstring_convert <std::codecvt_utf8 <wchar_t>, wchar_t> convert;
return convert.from_bytes (utf8_string);
}

但是,在块的末尾,其中一个俄语字符可能会被截断,并且转换将失败,并显示std::range_error exception

例如,在 UTF-8 中,"привет" 需要 15 个字符,"приве" 需要 13 个字符。 因此,如果我的块假设为 14,则"т"将部分丢失,并且转换将引发异常。

问题:

如何检测这些部分加载的字符?(在本例中为"т")这将允许我在没有它的情况下进行转换,并且可能比计划提前一点转移下一个块,以便下次包含这个有问题的"т"?

我不想trycatch这些功能,因为 try/catch 可能会减慢我的程序速度。它也没有告诉我"转换真正成功缺少多少性格"。

我知道wstring_convert::converted()但是如果我的程序在我到达它之前崩溃,那也不是很有用

您可以使用几个函数来执行此操作。UTF-8有一种方法可以检测多字节字符的开头和(从开始)多字节字符的大小。

所以两个功能:

// returns zero if this is the first byte of a UTF-8 char
// otherwise non-zero.
static unsigned is_continuation(char c)
{
return (c & 0b10000000) && !(c & 0b01000000);
}
// if c is the *first* byte of a UTF-8 multibyte character, returns 
// the total number of bytes of the character.
static unsigned size(const unsigned char c)
{
constexpr static const char u8char_size[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0
};
return u8char_size[(unsigned char)c];
}

您可以从缓冲区的末尾回溯,直到is_continuation(c)为假。然后检查当前UTF-8字符的size(c)是否比缓冲区的末尾长。

免责声明 - 上次我查看这些功能是否正常工作,但有一段时间没有使用它们了。

编辑:添加。

如果您想手动完成整个事情,我不妨发布代码以将UTF-8多字节字符转换为UTF-16多字节或UTF-32字符。

UTF-32很简单:

// returns a UTF-32 char from a `UTF-8` multibyte
// character pointed to by cp
static char32_t char32(const char* cp)
{
auto sz = size(*cp); // function above
if(sz == 1)
return *cp;
char32_t c32 = (0b0111'1111 >> sz) & (*cp);
for(unsigned i = 1; i < sz; ++i)
c32 = (c32 << 6) | (cp[i] & 0b0011'1111);
return c32;
}

UTF-16有点棘手:

// UTF-16 characters can be 1 or 2 characters wide...
using char16_pair = std::array<char16_t, 2>;
// outputs a UTF-16 char in cp16 from a `UTF-8` multibyte
// character pointed to by cp
//
// returns the number of characters in this `UTF-16` character
// (1 or 2).
static unsigned char16(const char* cp, char16_pair& cp16)
{
char32_t c32 = char32(cp);
if(c32 < 0xD800 || (c32 > 0xDFFF && c32 < 0x10000))
{
cp16[0] = char16_t(c32);
cp16[1] = 0;
return 1;
}
c32 -= 0x010000;
cp16[0] = ((0b1111'1111'1100'0000'0000 & c32) >> 10) + 0xD800;
cp16[1] = ((0b0000'0000'0011'1111'1111 & c32) >> 00) + 0xDC00;
return 2;
}