* Simplified UTF8CountBytes() a bit, made it faster, and less error-prone

against malformed UTF-8.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24166 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2008-02-28 10:45:57 +00:00
parent 4f8e472b15
commit 326dce3ae0

View File

@ -1,5 +1,5 @@
/*
* Copyright 2004-2006, Haiku, Inc.
* Copyright 2004-2008, Haiku, Inc.
* Distributed under the terms of the MIT License.
*/
#ifndef _UTF8_FUNCTIONS_H
@ -64,48 +64,22 @@ UTF8PreviousCharLen(const char *text, const char *limit)
static inline uint32
UTF8CountBytes(const char *bytes, int32 numChars)
{
if (!bytes)
if (bytes == NULL)
return 0;
if (numChars < 0)
numChars = INT_MAX;
const char *base = bytes;
while (*bytes && numChars-- > 0) {
if (bytes[0] & 0x80) {
if (bytes[0] & 0x40) {
if (bytes[0] & 0x20) {
if (bytes[0] & 0x10) {
if (bytes[1] == 0 || bytes[2] == 0 || bytes[3] == 0)
return (bytes - base);
bytes += 4;
continue;
}
if (bytes[1] == 0 || bytes[2] == 0)
return (bytes - base);
bytes += 3;
continue;
}
if (bytes[1] == 0)
return (bytes - base);
bytes += 2;
continue;
}
/* Not a startbyte - skip */
bytes += 1;
continue;
while (bytes[0] != '\0') {
if ((bytes[0] & 0xc0) != 0x80) {
if (--numChars <= 0)
break;
}
bytes += 1;
bytes++;
}
return (bytes - base);
return bytes - base;
}