开发者

Function to go back 1 UTF-8 character?

开发者 https://www.devze.com 2023-02-09 17:00 出处:网络
I have a function which goes forward 1 utf-8 character and returns the number of bytes it took to get there:

I have a function which goes forward 1 utf-8 character and returns the number of bytes it took to get there:

// Moves the iterator to next unicode character in the string,
//retur开发者_如何学Gons 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;
}

How could I modify this so that I can go back a unicode character from an arbitrary character?

Thanks


UTF-8 start bytes are either 0xxxxxxx or 11xxxxxx. No other bytes in a UTF-8 stream match those. From this you can design a function boolean isStartByte(unsigned char c). From there the rest is boring work with C++ iterators. Have fun.


Just decrement the iterator instead of incrementing it.

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

    return res;
}


In UTF-8, there are three kinds of byte...

0xxxxxxx : ASCII
10xxxxxx : 2nd, 3rd or 4th byte of code
11xxxxxx : 1st byte of multibyte code

So step back until you find an 0xxxxxxx or 11xxxxxx byte.

0

精彩评论

暂无评论...
验证码 换一张
取 消