From ccf6ad1d8be8ef6460257c95d577829add8a56d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Thu, 28 Feb 2008 15:56:31 +0000 Subject: [PATCH] Reverted to the previous version. Axel, your change completely broke all kinds of stuff. I think the trouble is that you keep decrementing charCount while still inside the same UTF8 glyph. I cannot really tell, I am hoping you don't mind me reverting to the old version, since the new one is only three lines, so it should be easy to correct and re-commit. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24172 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/interface/utf8_functions.h | 42 +++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/headers/private/interface/utf8_functions.h b/headers/private/interface/utf8_functions.h index fdb4985ba0..accb2ae238 100644 --- a/headers/private/interface/utf8_functions.h +++ b/headers/private/interface/utf8_functions.h @@ -1,5 +1,5 @@ /* - * Copyright 2004-2008, Haiku, Inc. + * Copyright 2004-2006, Haiku, Inc. * Distributed under the terms of the MIT License. */ #ifndef _UTF8_FUNCTIONS_H @@ -64,22 +64,48 @@ UTF8PreviousCharLen(const char *text, const char *limit) static inline uint32 UTF8CountBytes(const char *bytes, int32 numChars) { - if (bytes == NULL) + if (!bytes) return 0; if (numChars < 0) numChars = INT_MAX; const char *base = bytes; - while (bytes[0] != '\0') { - if ((bytes[0] & 0xc0) != 0x80) { - if (--numChars <= 0) - break; + 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; } - bytes++; + + bytes += 1; } - return bytes - base; + return (bytes - base); }