2004-01-15 10:15:37 +03:00
|
|
|
#ifndef __MOREUTF8
|
|
|
|
#define __MOREUTF8
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
#include <stdio.h>
|
2004-01-15 10:15:37 +03:00
|
|
|
|
|
|
|
static inline bool
|
|
|
|
IsInsideGlyph(uchar ch)
|
|
|
|
{
|
|
|
|
return (ch & 0xC0) == 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32
|
2005-06-15 21:09:00 +04:00
|
|
|
UTF8NextCharLenUnsafe(const char *text)
|
2004-01-15 10:15:37 +03:00
|
|
|
{
|
|
|
|
const char *ptr = text;
|
2005-06-15 21:09:00 +04:00
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
do {
|
|
|
|
ptr++;
|
|
|
|
} while (IsInsideGlyph(*ptr));
|
|
|
|
|
|
|
|
return ptr - text;
|
|
|
|
}
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
static inline uint32
|
|
|
|
UTF8NextCharLen(const char *text)
|
|
|
|
{
|
|
|
|
if (text == NULL || *text == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return UTF8NextCharLenUnsafe(text);
|
|
|
|
}
|
|
|
|
|
2005-05-21 03:51:33 +04:00
|
|
|
static inline uint32
|
|
|
|
UTF8PreviousCharLen(const char *text, const char *limit)
|
|
|
|
{
|
|
|
|
const char *ptr = text;
|
|
|
|
|
|
|
|
if (ptr == NULL || limit == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (ptr == limit)
|
|
|
|
break;
|
|
|
|
ptr--;
|
|
|
|
} while (IsInsideGlyph(*ptr));
|
|
|
|
|
|
|
|
return text - ptr;
|
|
|
|
}
|
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
// TODO: use this function in other places of this file...
|
2005-05-13 21:30:59 +04:00
|
|
|
static inline uint32
|
2005-06-15 21:09:00 +04:00
|
|
|
count_utf8_bytes(uchar ch)
|
2005-05-13 21:30:59 +04:00
|
|
|
{
|
2005-06-15 21:09:00 +04:00
|
|
|
// the number of high bits set until the first
|
|
|
|
// unset bit determine the count of bytes used for
|
|
|
|
// this glyph from this byte on
|
|
|
|
uchar bit = 1 << 7;
|
|
|
|
uint32 count = 1;
|
|
|
|
if (ch & bit) {
|
|
|
|
bit = bit >> 1;
|
|
|
|
while (ch & bit) {
|
|
|
|
count++;
|
|
|
|
bit = bit >> 1;
|
|
|
|
}
|
2005-05-13 21:30:59 +04:00
|
|
|
}
|
2005-06-15 21:09:00 +04:00
|
|
|
return count;
|
|
|
|
}
|
2005-05-13 21:30:59 +04:00
|
|
|
|
2005-06-15 21:09:00 +04:00
|
|
|
static inline uint32
|
|
|
|
UTF8CountBytes(const char *text, uint32 numChars)
|
|
|
|
{
|
|
|
|
if (text) {
|
|
|
|
// iterate over numChars glyphs incrementing ptr by the
|
|
|
|
// number of bytes for each glyph, which is encoded in
|
|
|
|
// the first byte of any glyph.
|
|
|
|
const char *ptr = text;
|
|
|
|
while (numChars--) {
|
|
|
|
ptr += count_utf8_bytes(*ptr);
|
|
|
|
}
|
|
|
|
return ptr - text;
|
|
|
|
}
|
|
|
|
return 0;
|
2005-05-13 21:30:59 +04:00
|
|
|
}
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
static inline uint32
|
2005-05-21 03:51:33 +04:00
|
|
|
UTF8CountChars(const char *text, int32 numBytes)
|
2004-01-15 10:15:37 +03:00
|
|
|
{
|
2005-05-21 03:51:33 +04:00
|
|
|
const char* ptr = text;
|
|
|
|
const char* last = ptr + numBytes - 1;
|
2004-01-15 10:15:37 +03:00
|
|
|
|
2005-05-21 03:51:33 +04:00
|
|
|
uint32 count = 0;
|
|
|
|
while (ptr <= last) {
|
|
|
|
ptr += UTF8NextCharLen(ptr);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2004-01-15 10:15:37 +03:00
|
|
|
}
|
|
|
|
|
2005-05-21 03:51:33 +04:00
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
#endif
|