我正在从utf-8文本文件中提取文本,并逐块执行以提高性能。std::ifstream.read(myChunkBuff_str, myChunkBuff_str.length())
Here is a more detailed example
每个块约有1.6万个字符。
我的下一步是将std::string
转换为可以让我分别处理这些“复杂字符”的内容,从而将std::string
转换为std::wstring
。
我正在使用以下函数进行转换,taken from here:
#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,则'т'将部分丢失,并且转换将引发异常。
问题:
如何检测这些部分加载的字符? (在这种情况下为“т”),这将允许我在没有它的情况下进行转换,并且也许比计划的要早移出下一个块,以便下次包含此有问题的“т”?
我不想在这些函数周围添加
try
或catch
,因为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)
为 false 为止。然后检查当前size(c)
char的UTF-8
是否长于缓冲区末尾。免责声明-上次我查看这些功能是否有效,但有一段时间没有使用它们。
编辑:添加。
如果您想手动完成全部操作,则最好发布代码以将
UTF-8
多字节字符转换为UTF-16
多字节或UTF-32
char。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 = (0b01111111 >> 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;
}