blob: b30677fa7d43a2f2dfe04bfd7329fb86efa60d50 [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>
21
Ken Wakasa07cab722010-04-20 01:24:57 +090022namespace latinime {
23
Ken Wakasade8a9a82012-08-17 13:06:28 +090024inline static bool isAsciiUpper(unsigned short c) {
25 return isupper(static_cast<int>(c)) != 0;
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090026}
27
28inline static unsigned short toAsciiLower(unsigned short c) {
29 return c - 'A' + 'a';
30}
31
Ken Wakasade8a9a82012-08-17 13:06:28 +090032inline static bool isAscii(unsigned short c) {
33 return isascii(static_cast<int>(c)) != 0;
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090034}
35
Ken Wakasade8a9a82012-08-17 13:06:28 +090036unsigned short latin_tolower(const unsigned short c);
Ken Wakasa07cab722010-04-20 01:24:57 +090037
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090038/**
39 * Table mapping most combined Latin, Greek, and Cyrillic characters
40 * to their base characters. If c is in range, BASE_CHARS[c] == c
41 * if c is not a combined character, or the base character if it
42 * is combined.
43 */
44
45static const int BASE_CHARS_SIZE = 0x0500;
46extern const unsigned short BASE_CHARS[BASE_CHARS_SIZE];
47
48inline static unsigned short toBaseChar(unsigned short c) {
49 if (c < BASE_CHARS_SIZE) {
50 return BASE_CHARS[c];
51 }
52 return c;
53}
54
Jean Chalarde9a86e22012-06-28 21:01:29 +090055inline static unsigned short toLowerCase(const unsigned short c) {
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +090056 if (isAsciiUpper(c)) {
57 return toAsciiLower(c);
58 } else if (isAscii(c)) {
59 return c;
60 }
61 return latin_tolower(c);
62}
63
Jean Chalarde9a86e22012-06-28 21:01:29 +090064inline static unsigned short toBaseLowerCase(const unsigned short c) {
65 return toLowerCase(toBaseChar(c));
66}
Ken Wakasace9e52a2011-06-18 13:09:55 +090067} // namespace latinime
Ken Wakasa07cab722010-04-20 01:24:57 +090068#endif // LATINIME_CHAR_UTILS_H