From d273d008229294ea3ce11517a74d3c525d847365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 28 Feb 2008 19:17:33 +0000 Subject: [PATCH] * Fixed previous version of UTF8CountBytes() and put it in again: it was breaking out of the loop too early, and would therefore miss the last character. * Replaced UTF8CountChars() with a short and safe version as well. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24173 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/interface/utf8_functions.h | 87 ++++------------------ 1 file changed, 15 insertions(+), 72 deletions(-) diff --git a/headers/private/interface/utf8_functions.h b/headers/private/interface/utf8_functions.h index accb2ae238..ec1592066d 100644 --- a/headers/private/interface/utf8_functions.h +++ b/headers/private/interface/utf8_functions.h @@ -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; } @@ -116,50 +90,19 @@ UTF8CountBytes(const char *bytes, int32 numChars) static inline uint32 UTF8CountChars(const char *bytes, int32 numBytes) { - if (!bytes) + if (bytes == NULL) return 0; uint32 length = 0; - const char *last = bytes + numBytes - 1; + const char *last; if (numBytes < 0) last = (const char *)UINT_MAX; + else + last = bytes + numBytes - 1; - while (*bytes && bytes <= last) { - 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 length; - - bytes += 4; - length++; - continue; - } - - if (bytes[1] == 0 || bytes[2] == 0) - return length; - - bytes += 3; - length++; - continue; - } - - if (bytes[1] == 0) - return length; - - bytes += 2; - length++; - continue; - } - - /* Not a startbyte - skip */ - bytes += 1; - continue; - } - - bytes += 1; - length++; + while (bytes[0] && bytes <= last) { + if ((bytes++[0] & 0xc0) != 0x80) + length++; } return length;