4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
新闻详情
ICU:ucnv_convertEx-实时检测编码错误_三行代码
来自 : ask.sov5.cn/q/SSVIqmO... 发布时间:2021-03-24
/* 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 令人惊讶的是:

source_p: source + 5;status: U_ZERO_ERROR;drain: (n=5)[00FEFF 00FFFD 00000A 00000A 000000 ]
所以,没有明显的错误迹象错误检测能比手动检查内容更优雅吗?

本文链接: http://convertex.immuno-online.com/view-683952.html

发布于 : 2021-03-24 阅读(0)