Optimize latin_tolower(). Marge from master: https://android-git.corp.google.com/g/#change,49214
Change-Id: Ib1d7711aca533c21e5179b557a3ed806ac3fbdc6main
parent
07cab72ce4
commit
6874267be0
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace latinime {
|
namespace latinime {
|
||||||
|
|
||||||
|
@ -882,18 +882,25 @@ static const struct LatinCapitalSmallPair SORTED_CHAR_MAP[] = {
|
||||||
{ 0xFF3A, 0xFF5A } // FULLWIDTH LATIN CAPITAL LETTER Z
|
{ 0xFF3A, 0xFF5A } // FULLWIDTH LATIN CAPITAL LETTER Z
|
||||||
};
|
};
|
||||||
|
|
||||||
static int compare_pair_capital(const void *a, const void *b) {
|
unsigned short latin_tolower(unsigned short c0) {
|
||||||
return (int)((struct LatinCapitalSmallPair*)a)->capital
|
const struct LatinCapitalSmallPair *p;
|
||||||
- (int)((struct LatinCapitalSmallPair*)b)->capital;
|
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) {
|
// Binary search: Taken from bionic
|
||||||
struct LatinCapitalSmallPair *p =
|
for (lim = nmemb; lim != 0; lim >>= 1) {
|
||||||
(struct LatinCapitalSmallPair *)bsearch(&c, SORTED_CHAR_MAP,
|
p = base + (lim >> 1);
|
||||||
sizeof(SORTED_CHAR_MAP) / sizeof(SORTED_CHAR_MAP[0]),
|
cmp = c - (int)p->capital;
|
||||||
sizeof(SORTED_CHAR_MAP[0]),
|
if (cmp == 0)
|
||||||
compare_pair_capital);
|
return p->small;
|
||||||
return p ? p->small : c;
|
if (cmp > 0) { /* key > p: move right */
|
||||||
|
base = p + 1;
|
||||||
|
lim--;
|
||||||
|
} /* else move left */
|
||||||
|
}
|
||||||
|
return c0;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace latinime
|
} // namespace latinime
|
||||||
|
|
Loading…
Reference in New Issue