功能返回1个UTF-8字符?

jma*_*erx 2 c++

我有一个函数,它前进1 utf-8字符并返回到达那里所需的字节数:

// Moves the iterator to next unicode character in the string,
//returns number of bytes skipped
template<typename _Iterator1, typename _Iterator2>
inline size_t bringToNextUnichar(_Iterator1& it,
    const _Iterator2& last) const {
    if(it == last) return 0;
    unsigned char c;
    size_t res = 1;
    for(++it; last != it; ++it, ++res) {
        c = *it;
        if(!(c&0x80) || ((c&0xC0) == 0xC0)) break;
    }

    return res;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能修改这个,以便我可以从任意字符返回一个unicode字符?

谢谢

rli*_*bby 5

UTF-8起始字节是0xxxxxxx11xxxxxx.UTF-8流中没有其他字节匹配这些字节.从这里你可以设计一个功能boolean isStartByte(unsigned char c).从那里开始,剩下的就是使用C++迭代器.玩得开心.