gpt4 book ai didi

c++ - ICU:ucnv_convertEx——即时检测编码错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:17 25 4
gpt4 key购买 nike

是否可以在转换时使用 ICU 检测编码错误,或者是否有必要在转换前或后检查?

鉴于设置了从 UTF8 到 UTF32 的转换的初始化:

#include <stdio.h>
#include "unicode/ucnv.h" /* C Converter API */

static void eval(UConverter* from, UConverter* to);

int main(int argc, char** argv)
{
UConverter* from;
UConverter* to;
UErrorCode status;

/* Initialize converter from UTF8 to Unicode ___________________________*/
status = U_ZERO_ERROR;
from = ucnv_open("UTF-8", &status);
if( ! from || ! U_SUCCESS(status) ) return 1;
status = U_ZERO_ERROR;
to = ucnv_open("UTF32", &status);
if( ! to || ! U_SUCCESS(status) ) return 1;
/*______________________________________________________________________*/

eval(from, to);
return 0;
}

然后,通过

使用 ucnv_convertEx 应用转换
static void eval(UConverter* from, UConverter* to) 
{
UErrorCode status = U_ZERO_ERROR;
uint32_t drain[1024];
uint32_t* drain_p = &drain[0];
uint32_t* p = &drain[0];

/* UTF8 sequence with error in third byte ______________________________*/
const char source[] = { "\xED\x8A\x0A\x0A" };
const char* source_p = &source[0];

ucnv_convertEx(to, from, (char**)&drain_p, (char*)&drain[1024],
&source_p, &source[5],
NULL, NULL, NULL, NULL, /* reset = */TRUE, /* flush = */TRUE,
&status);

/* Print conversion result _____________________________________________*/
printf("source_p: source + %i;\n", (int)(source_p - &source[0]));
printf("status: %s;\n", u_errorName(status));
printf("drain: (n=%i)[", (int)(drain_p - &drain[0]));
for(p=&drain[0]; p != drain_p ; ++p) { printf("%06X ", (int)*p); }
printf("]\n");
}

其中 source 包含不允许的 UTF8 代码单元序列,函数应该以某种方式报告错误。将以上片段存储在“test.c”中,并用

编译以上代码
$ gcc test.c $(icu-config --ldflags) -o test

./test 的输出是(令人惊讶的):

source_p: source + 5;
status: U_ZERO_ERROR;
drain: (n=5)[00FEFF 00FFFD 00000A 00000A 000000 ]

因此,没有检测到错误的明显迹象。是否可以比手动检查内容更优雅地进行错误检测?

最佳答案

正如@Eljay 在评论中建议的那样,您可以使用错误回调。您甚至不需要自己编写,因为内置的 UCNV_TO_U_CALLBACK_STOP 会执行您想要的操作(即,对任何错误字符返回失败)。

int TestIt()
{
UConverter* utf8conv{};
UConverter* utf32conv{};
UErrorCode status{ U_ZERO_ERROR };

utf8conv = ucnv_open("UTF8", &status);

if (!U_SUCCESS(status))
{
return 1;
}

utf32conv = ucnv_open("UTF32", &status);

if (!U_SUCCESS(status))
{
return 2;
}

const char source[] = { "\xED\x8A\x0A\x0A" };
uint32_t target[10]{ 0 };

ucnv_setToUCallBack(utf8conv, UCNV_TO_U_CALLBACK_STOP, nullptr,
nullptr, nullptr, &status);

if (!U_SUCCESS(status))
{
return 3;
}

auto sourcePtr = source;
auto sourceEnd = source + ARRAYSIZE(source);
auto targetPtr = target;
auto targetEnd = reinterpret_cast<const char*>(target + ARRAYSIZE(target));

ucnv_convertEx(utf32conv, utf8conv, reinterpret_cast<char**>(&targetPtr),
targetEnd, &sourcePtr, sourceEnd, nullptr, nullptr, nullptr, nullptr,
TRUE, TRUE, &status);

if (!U_SUCCESS(status))
{
return 4;
}

printf("Converted '%s' to '", source);
for (auto start = target; start != targetPtr; start++)
{
printf("\\x%x", *start);
}
printf("'\r\n");

return 0;
}

对于无效的 Unicode 代码点,这应该返回 4,如果成功则打印出 UTF-32 值。我们不太可能从 ucnv_setToUCallBack 中得到错误,但我们会检查以防万一。在上面的示例中,我们为之前的操作传递了 nullptr,因为我们不关心它是什么,也不需要重置它。

关于c++ - ICU:ucnv_convertEx——即时检测编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com