The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Mathias Agopian | f446ba9 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 17 | #include <utils/AndroidUnicode.h> |
| 18 | #include "CharacterData.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| 20 | #define LOG_TAG "Unicode" |
Mathias Agopian | f446ba9 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | // ICU headers for using macros |
| 24 | #include <unicode/utf16.h> |
| 25 | |
| 26 | #define MIN_RADIX 2 |
| 27 | #define MAX_RADIX 36 |
| 28 | |
| 29 | #define TYPE_SHIFT 0 |
| 30 | #define TYPE_MASK ((1<<5)-1) |
| 31 | |
| 32 | #define DIRECTION_SHIFT (TYPE_SHIFT+5) |
| 33 | #define DIRECTION_MASK ((1<<5)-1) |
| 34 | |
| 35 | #define MIRRORED_SHIFT (DIRECTION_SHIFT+5) |
| 36 | #define MIRRORED_MASK ((1<<1)-1) |
| 37 | |
| 38 | #define TOUPPER_SHIFT (MIRRORED_SHIFT+1) |
| 39 | #define TOUPPER_MASK ((1<<6)-1) |
| 40 | |
| 41 | #define TOLOWER_SHIFT (TOUPPER_SHIFT+6) |
| 42 | #define TOLOWER_MASK ((1<<6)-1) |
| 43 | |
| 44 | #define TOTITLE_SHIFT (TOLOWER_SHIFT+6) |
| 45 | #define TOTITLE_MASK ((1<<2)-1) |
| 46 | |
| 47 | #define MIRROR_SHIFT (TOTITLE_SHIFT+2) |
| 48 | #define MIRROR_MASK ((1<<5)-1) |
| 49 | |
| 50 | #define NUMERIC_SHIFT (TOTITLE_SHIFT+2) |
| 51 | #define NUMERIC_MASK ((1<<7)-1) |
| 52 | |
| 53 | #define DECOMPOSITION_SHIFT (11) |
| 54 | #define DECOMPOSITION_MASK ((1<<5)-1) |
| 55 | |
| 56 | /* |
| 57 | * Returns the value stored in the CharacterData tables that contains |
| 58 | * an index into the packed data table and the decomposition type. |
| 59 | */ |
| 60 | static uint16_t findCharacterValue(UChar32 c) |
| 61 | { |
| 62 | LOG_ASSERT(c >= 0 && c <= 0x10FFFF, "findCharacterValue received an invalid codepoint"); |
| 63 | if (c < 256) |
| 64 | return CharacterData::LATIN1_DATA[c]; |
| 65 | |
| 66 | // Rotate the bits because the tables are separated into even and odd codepoints |
| 67 | c = (c >> 1) | ((c & 1) << 20); |
| 68 | |
| 69 | CharacterData::Range search = CharacterData::FULL_DATA[c >> 16]; |
| 70 | const uint32_t* array = search.array; |
| 71 | |
| 72 | // This trick is so that that compare in the while loop does not |
| 73 | // need to shift the array entry down by 16 |
| 74 | c <<= 16; |
| 75 | c |= 0xFFFF; |
| 76 | |
| 77 | int high = (int)search.length - 1; |
| 78 | int low = 0; |
| 79 | |
| 80 | if (high < 0) |
| 81 | return 0; |
| 82 | |
| 83 | while (low < high - 1) |
| 84 | { |
| 85 | int probe = (high + low) >> 1; |
| 86 | |
| 87 | // The entries contain the codepoint in the high 16 bits and the index |
| 88 | // into PACKED_DATA in the low 16. |
| 89 | if (array[probe] > (unsigned)c) |
| 90 | high = probe; |
| 91 | else |
| 92 | low = probe; |
| 93 | } |
| 94 | |
| 95 | LOG_ASSERT((array[low] <= (unsigned)c), "A suitable range was not found"); |
| 96 | return array[low] & 0xFFFF; |
| 97 | } |
| 98 | |
| 99 | uint32_t android::Unicode::getPackedData(UChar32 c) |
| 100 | { |
| 101 | // findCharacterValue returns a 16-bit value with the top 5 bits containing a decomposition type |
| 102 | // and the remaining bits containing an index. |
| 103 | return CharacterData::PACKED_DATA[findCharacterValue(c) & 0x7FF]; |
| 104 | } |
| 105 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | android::Unicode::Direction android::Unicode::getDirectionality(UChar32 c) |
| 107 | { |
| 108 | uint32_t data = getPackedData(c); |
| 109 | |
| 110 | if (0 == data) |
| 111 | return DIRECTIONALITY_UNDEFINED; |
| 112 | |
| 113 | Direction d = (Direction) ((data >> DIRECTION_SHIFT) & DIRECTION_MASK); |
| 114 | |
| 115 | if (DIRECTION_MASK == d) |
| 116 | return DIRECTIONALITY_UNDEFINED; |
| 117 | |
| 118 | return d; |
| 119 | } |
| 120 | |
| 121 | bool android::Unicode::isMirrored(UChar32 c) |
| 122 | { |
| 123 | return ((getPackedData(c) >> MIRRORED_SHIFT) & MIRRORED_MASK) != 0; |
| 124 | } |
| 125 | |
| 126 | UChar32 android::Unicode::toMirror(UChar32 c) |
| 127 | { |
| 128 | if (!isMirrored(c)) |
| 129 | return c; |
| 130 | |
| 131 | return c + CharacterData::MIRROR_DIFF[(getPackedData(c) >> MIRROR_SHIFT) & MIRROR_MASK]; |
| 132 | } |