'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
'std::wstring_convert' to convert as much as possible (from a UTF8 file-read chunk)
我正在从 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,则"т"将部分丢失,并且转换将引发异常。
问题:
如何检测这些部分加载的字符?(在本例中为"т")这将允许我在没有它的情况下进行转换,并且可能比计划提前一点转移下一个块,以便下次包含这个有问题的"т"?
我不想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)
为假。然后检查当前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;
}
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- VS2019编译器将没有BOM文件的UTF8错误解释为ANSI
- 在Qt中读取utf8文件到QString
- 将Unicode的UTF8表示形式写入文件
- 如何在C++中将文件编码格式设置为UTF8
- 如何编写指向始终使用utf8格式的文件的char*指针
- 警告 C4819 在 Visual Studio C++ 2013 Express - 没有 BOM 的 UTF8 文件
- Exiv2:如何读取照片与UTF8文件路径
- c++文件加载时UTF8置乱
- 用UTF8编程地挂载FAT32文件系统