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.
精彩评论