summaryrefslogtreecommitdiff
path: root/libs/androidfw/include
diff options
context:
space:
mode:
author Victor Chang <vichang@google.com> 2025-01-02 16:06:03 -0800
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2025-01-02 16:06:03 -0800
commit554cd36610b210955d5e4e02a6c7d1c01ffd2930 (patch)
tree644d9b7735f81c1ad184a37bf9ada174a96faa90 /libs/androidfw/include
parentc3882af28c3163c0750905bcda465a8134afde71 (diff)
parent60786ca61d07db62316178419bd6afea399c0f3d (diff)
Merge "Extract implementation of script and locale matching into LocaleDataLookup.h" into main am: b4966de272 am: 60786ca61d
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/3432461 Change-Id: Ic9e1b2401f0432ed0f1777e216e171e2a17527e4 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'libs/androidfw/include')
-rw-r--r--libs/androidfw/include/androidfw/LocaleDataLookup.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/libs/androidfw/include/androidfw/LocaleDataLookup.h b/libs/androidfw/include/androidfw/LocaleDataLookup.h
new file mode 100644
index 000000000000..7fde7123ed0b
--- /dev/null
+++ b/libs/androidfw/include/androidfw/LocaleDataLookup.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+
+namespace android {
+
+namespace hidden {
+ bool isRepresentative(uint64_t packed_locale);
+}
+
+constexpr size_t SCRIPT_LENGTH = 4;
+
+constexpr inline uint32_t packLocale(const char* language, const char* region) {
+ const unsigned char* lang = reinterpret_cast<const unsigned char*>(language);
+ const unsigned char* reg = reinterpret_cast<const unsigned char*>(region);
+ return (static_cast<uint32_t>(lang[0]) << 24u) |
+ (static_cast<uint32_t>(lang[1]) << 16u) |
+ (static_cast<uint32_t>(reg[0]) << 8u) |
+ static_cast<uint32_t>(reg[1]);
+}
+
+constexpr inline uint32_t dropRegion(uint32_t packed_locale) {
+ return packed_locale & 0xFFFF0000LU;
+}
+
+constexpr inline bool hasRegion(uint32_t packed_locale) {
+ return (packed_locale & 0x0000FFFFLU) != 0;
+}
+
+/**
+ * Return nullptr if the key isn't found. The input packed_lang_region can be computed
+ * by android::packLocale.
+ * Note that the returned char* is either nullptr or 4-byte char seqeuence, but isn't
+ * a null-terminated string.
+ */
+const char* lookupLikelyScript(uint32_t packed_lang_region);
+/**
+ * Return false if the key isn't representative. The input lookup key can be computed
+ * by android::packLocale.
+ */
+bool inline isLocaleRepresentative(uint32_t language_and_region, const char* script) {
+ const unsigned char* s = reinterpret_cast<const unsigned char*>(script);
+ const uint64_t packed_locale = (
+ ((static_cast<uint64_t>(language_and_region)) << 32u) |
+ (static_cast<uint64_t>(s[0]) << 24u) |
+ (static_cast<uint64_t>(s[1]) << 16u) |
+ (static_cast<uint64_t>(s[2]) << 8u) |
+ static_cast<uint64_t>(s[3]));
+
+ return hidden::isRepresentative(packed_locale);
+}
+
+/**
+ * Return a parent packed key for a given script and child packed key. Return 0 if
+ * no parent is found.
+ */
+uint32_t findParentLocalePackedKey(const char* script, uint32_t packed_lang_region);
+
+uint32_t getMaxAncestorTreeDepth();
+
+} // namespace android