blob: 60da203b949c50a84bf7b117f3dcf352804c3845 [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 Wakasa1e614932012-10-29 18:06:22 +090021
22#include "defines.h"
Ken Wakasade8a9a82012-08-17 13:06:28 +090023
Ken Wakasa07cab722010-04-20 01:24:57 +090024namespace latinime {
25
Ken Wakasa1e614932012-10-29 18:06:22 +090026inline static bool isAsciiUpper(int c) {
Tom Ouyangedd5b732012-09-25 17:04:35 -070027 // Note: isupper(...) reports false positives for some Cyrillic characters, causing them to
28 // be incorrectly lower-cased using toAsciiLower(...) rather than latin_tolower(...).
29 return (c >= 'A' && c <= 'Z');
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090030}
31
Ken Wakasa1e614932012-10-29 18:06:22 +090032inline static int toAsciiLower(int c) {
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090033 return c - 'A' + 'a';
34}
35
Ken Wakasa1e614932012-10-29 18:06:22 +090036inline static bool isAscii(int c) {
37 return isascii(c) != 0;
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090038}
39
Ken Wakasade8a9a82012-08-17 13:06:28 +090040unsigned short latin_tolower(const unsigned short c);
Ken Wakasa07cab722010-04-20 01:24:57 +090041
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090042/**
43 * Table mapping most combined Latin, Greek, and Cyrillic characters
44 * to their base characters. If c is in range, BASE_CHARS[c] == c
45 * if c is not a combined character, or the base character if it
46 * is combined.
47 */
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090048static const int BASE_CHARS_SIZE = 0x0500;
Ken Wakasa1e614932012-10-29 18:06:22 +090049extern const unsigned short BASE_CHARS[BASE_CHARS_SIZE];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090050
Ken Wakasa1e614932012-10-29 18:06:22 +090051inline static int toBaseCodePoint(int c) {
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090052 if (c < BASE_CHARS_SIZE) {
Ken Wakasa1e614932012-10-29 18:06:22 +090053 return static_cast<int>(BASE_CHARS[c]);
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090054 }
55 return c;
56}
57
Ken Wakasa19d844c2012-11-02 23:57:13 +090058AK_FORCE_INLINE static int toLowerCase(const int 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 }
Ken Wakasa1e614932012-10-29 18:06:22 +090064 return static_cast<int>(latin_tolower(static_cast<unsigned short>(c)));
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090065}
66
Ken Wakasa19d844c2012-11-02 23:57:13 +090067AK_FORCE_INLINE static int toBaseLowerCase(const int c) {
Ken Wakasa1e614932012-10-29 18:06:22 +090068 return toLowerCase(toBaseCodePoint(c));
Jean Chalarde9a86e22012-06-28 21:01:29 +090069}
Ken Wakasa5150e152012-09-27 19:21:25 +090070
Ken Wakasa1e614932012-10-29 18:06:22 +090071inline static bool isSkippableCodePoint(const int codePoint) {
Ken Wakasa5150e152012-09-27 19:21:25 +090072 // TODO: Do not hardcode here
Ken Wakasa1e614932012-10-29 18:06:22 +090073 return codePoint == KEYCODE_SINGLE_QUOTE || codePoint == KEYCODE_HYPHEN_MINUS;
Ken Wakasa5150e152012-09-27 19:21:25 +090074}
Ken Wakasace9e52a2011-06-18 13:09:55 +090075} // namespace latinime
Ken Wakasa07cab722010-04-20 01:24:57 +090076#endif // LATINIME_CHAR_UTILS_H