blob: 20cf2e8b5296718f15656992c972290b903ac057 [file] [log] [blame]
Ken Wakasa07cab722010-04-20 01:24:57 +09001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef LATINIME_CHAR_UTILS_H
18#define LATINIME_CHAR_UTILS_H
19
Ken Wakasade8a9a82012-08-17 13:06:28 +090020#include <cctype>
Ken Wakasaf2789812012-09-04 12:49:46 +090021#include <stdint.h>
Ken Wakasade8a9a82012-08-17 13:06:28 +090022
Ken Wakasa07cab722010-04-20 01:24:57 +090023namespace latinime {
24
Ken Wakasade8a9a82012-08-17 13:06:28 +090025inline static bool isAsciiUpper(unsigned short c) {
Tom Ouyangedd5b732012-09-25 17:04:35 -070026 // Note: isupper(...) reports false positives for some Cyrillic characters, causing them to
27 // be incorrectly lower-cased using toAsciiLower(...) rather than latin_tolower(...).
28 return (c >= 'A' && c <= 'Z');
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090029}
30
31inline static unsigned short toAsciiLower(unsigned short c) {
32 return c - 'A' + 'a';
33}
34
Ken Wakasade8a9a82012-08-17 13:06:28 +090035inline static bool isAscii(unsigned short c) {
36 return isascii(static_cast<int>(c)) != 0;
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090037}
38
Ken Wakasade8a9a82012-08-17 13:06:28 +090039unsigned short latin_tolower(const unsigned short c);
Ken Wakasa07cab722010-04-20 01:24:57 +090040
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090041/**
42 * Table mapping most combined Latin, Greek, and Cyrillic characters
43 * to their base characters. If c is in range, BASE_CHARS[c] == c
44 * if c is not a combined character, or the base character if it
45 * is combined.
46 */
47
48static const int BASE_CHARS_SIZE = 0x0500;
Ken Wakasaf2789812012-09-04 12:49:46 +090049extern const uint16_t BASE_CHARS[BASE_CHARS_SIZE];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090050
51inline static unsigned short toBaseChar(unsigned short c) {
52 if (c < BASE_CHARS_SIZE) {
53 return BASE_CHARS[c];
54 }
55 return c;
56}
57
Jean Chalarde9a86e22012-06-28 21:01:29 +090058inline static unsigned short toLowerCase(const unsigned short c) {
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090059 if (isAsciiUpper(c)) {
60 return toAsciiLower(c);
61 } else if (isAscii(c)) {
62 return c;
63 }
64 return latin_tolower(c);
65}
66
Jean Chalarde9a86e22012-06-28 21:01:29 +090067inline static unsigned short toBaseLowerCase(const unsigned short c) {
68 return toLowerCase(toBaseChar(c));
69}
Ken Wakasace9e52a2011-06-18 13:09:55 +090070} // namespace latinime
Ken Wakasa07cab722010-04-20 01:24:57 +090071#endif // LATINIME_CHAR_UTILS_H