From 8f1a4e7f895edfed9bca8b83b288cb0f362caea5 Mon Sep 17 00:00:00 2001 From: ejakowatz Date: Thu, 14 Nov 2002 08:06:18 +0000 Subject: [PATCH] Implemented BGA's UTF-8 char len suggestion in UTF8.h and used it in BString::CountChars(). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1932 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/support/UTF8.h | 9 +++++++++ src/kits/support/String.cpp | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/headers/os/support/UTF8.h b/headers/os/support/UTF8.h index d6d5a7067f..6260762c1c 100644 --- a/headers/os/support/UTF8.h +++ b/headers/os/support/UTF8.h @@ -61,6 +61,15 @@ _IMPEXP_TEXTENCODING status_t convert_from_utf8(uint32 dstEncoding, int32 *state, char substitute = B_SUBSTITUTE); +/*-------------------------------------------------------------*/ +/*------- Utility Functions -----------------------------------*/ + +// Suggested by BGA +inline uint32 utf8_char_len(uchar c) +{ + return (((0xE5000000 >> (((c) >> 3) & 0x1E)) & 3) + 1); +} + /*-------------------------------------------------------------*/ /*-------------------------------------------------------------*/ diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 92eecd35b7..5a300f0430 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -36,6 +36,7 @@ #define DEBUG 1 #include #include +#include // Temporary Includes #include "string_helper.h" @@ -92,7 +93,9 @@ BString::CountChars() const count++; // Jump to next UTF8 character - for (; (*ptr & 0xc0) == 0x80; ptr++); + // for (; (*ptr & 0xc0) == 0x80; ptr++); + // ejaesler: BGA's nifty function + ptr += utf8_char_len(*ptr); } return count;