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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With