From 6874267be0cfceb9db98c24136216c6ef84b4ec9 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Wed, 21 Apr 2010 23:07:14 +0900 Subject: [PATCH] Optimize latin_tolower(). Marge from master: https://android-git.corp.google.com/g/#change,49214 Change-Id: Ib1d7711aca533c21e5179b557a3ed806ac3fbdc6 --- native/src/char_utils.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/native/src/char_utils.cpp b/native/src/char_utils.cpp index 3cacc9e16..c9204df24 100644 --- a/native/src/char_utils.cpp +++ b/native/src/char_utils.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include +#include namespace latinime { @@ -882,18 +882,25 @@ static const struct LatinCapitalSmallPair SORTED_CHAR_MAP[] = { { 0xFF3A, 0xFF5A } // FULLWIDTH LATIN CAPITAL LETTER Z }; -static int compare_pair_capital(const void *a, const void *b) { - return (int)((struct LatinCapitalSmallPair*)a)->capital - - (int)((struct LatinCapitalSmallPair*)b)->capital; -} +unsigned short latin_tolower(unsigned short c0) { + const struct LatinCapitalSmallPair *p; + const struct LatinCapitalSmallPair *base = SORTED_CHAR_MAP; + int c = c0; + int lim, cmp; + const size_t nmemb = sizeof(SORTED_CHAR_MAP) / sizeof(SORTED_CHAR_MAP[0]); -unsigned short latin_tolower(unsigned short c) { - struct LatinCapitalSmallPair *p = - (struct LatinCapitalSmallPair *)bsearch(&c, SORTED_CHAR_MAP, - sizeof(SORTED_CHAR_MAP) / sizeof(SORTED_CHAR_MAP[0]), - sizeof(SORTED_CHAR_MAP[0]), - compare_pair_capital); - return p ? p->small : c; + // Binary search: Taken from bionic + for (lim = nmemb; lim != 0; lim >>= 1) { + p = base + (lim >> 1); + cmp = c - (int)p->capital; + if (cmp == 0) + return p->small; + if (cmp > 0) { /* key > p: move right */ + base = p + 1; + lim--; + } /* else move left */ + } + return c0; } } // namespace latinime