Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to go back 1 UTF-8 character?

Tags:

c++

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,
//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;
}

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

Thanks

like image 884
jmasterx Avatar asked Oct 27 '25 09:10

jmasterx


2 Answers

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.

like image 131
rlibby Avatar answered Oct 30 '25 00:10

rlibby


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;
}
like image 45
Maz Avatar answered Oct 29 '25 23:10

Maz