• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #pragma once
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 
23 namespace android {
24 
25 constexpr size_t SCRIPT_LENGTH = 4;
26 
packLocale(const char * language,const char * region)27 constexpr inline uint32_t packLocale(const char* language, const char* region) {
28     const unsigned char* lang = reinterpret_cast<const unsigned char*>(language);
29     const unsigned char* reg = reinterpret_cast<const unsigned char*>(region);
30     return (static_cast<uint32_t>(lang[0]) << 24u) |
31             (static_cast<uint32_t>(lang[1]) << 16u) |
32             (static_cast<uint32_t>(reg[0]) << 8u) |
33             static_cast<uint32_t>(reg[1]);
34 }
35 
dropRegion(uint32_t packed_locale)36 constexpr inline uint32_t dropRegion(uint32_t packed_locale) {
37     return packed_locale & 0xFFFF0000LU;
38 }
39 
hasRegion(uint32_t packed_locale)40 constexpr inline bool hasRegion(uint32_t packed_locale) {
41     return (packed_locale & 0x0000FFFFLU) != 0;
42 }
43 
packScript(const char * script)44 constexpr inline uint32_t packScript(const char* script) {
45     const unsigned char* s = reinterpret_cast<const unsigned char*>(script);
46     return ((static_cast<uint32_t>(s[0]) << 24u) |
47             (static_cast<uint32_t>(s[1]) << 16u) |
48             (static_cast<uint32_t>(s[2]) <<  8u) |
49             static_cast<uint32_t>(s[3]));
50 }
51 
52 /**
53  * Return nullptr if the key isn't found. The input packed_lang_region can be computed
54  * by android::packLocale.
55  * Note that the returned char* is either nullptr or 4-byte char seqeuence, but isn't
56  * a null-terminated string.
57  */
58 const char* lookupLikelyScript(uint32_t packed_lang_region);
59 /**
60  * Return false if the key isn't representative. The input lookup key can be computed
61  * by android::packLocale.
62  */
63 bool isLocaleRepresentative(uint32_t language_and_region, const char* script);
64 
65 /**
66  * Return a parent packed key for a given script and child packed key. Return 0 if
67  * no parent is found.
68  */
69 uint32_t findParentLocalePackedKey(const char* script, uint32_t packed_lang_region);
70 
71 uint32_t getMaxAncestorTreeDepth();
72 
73 } // namespace android
74