我知道我可以使用mbrtowc()在C中迭代一个多字节字符串.但是如果我想倒退呢?或者换句话说,我如何找到以前有效的多字节字符.我尝试了以下方法,它至少部分地使用默认的en_us.UTF-8语言环境在我的Ubuntu系统上运行:
char *str = "\xc2\xa2\xc2\xa1xyzwxfd\xc2\xa9", *tmp = NULL;
wchar_t wc = 0;
size_t ret = 0, width = 1;
mbstate_t state = {0};
//Iterate through 2 characters using mbrtowc()
tmp = str;
tmp += mbrtowc(&wc, tmp, MB_CUR_MAX, &state);
tmp += mbrtowc(&wc, tmp, MB_CUR_MAX, &state);
//This is a simplified version of my code. I didnt test this
//exact code but this general idea did work.
for(tmp--; (ret = mbrtowc(&wc, tmp, width, &state)) == (size_t)(-1) || ret == (size_t)(-2); width++, tmp--)
if(width …Run Code Online (Sandbox Code Playgroud)