• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_BASE_LOCALE_HELPER_H
17 #define ECMASCRIPT_BASE_LOCALE_HELPER_H
18 
19 #include "ecmascript/js_handle.h"
20 #include "ecmascript/js_locale.h"
21 
22 namespace panda::ecmascript::intl {
23 constexpr uint8_t INTL_INDEX_TWO = 2;
24 constexpr uint8_t INTL_INDEX_THREE = 3;
25 constexpr uint8_t INTL_INDEX_FOUR = 4;
26 constexpr uint8_t INTL_INDEX_FIVE = 5;
27 constexpr uint8_t INTL_INDEX_EIGHT = 8;
28 class LocaleHelper {
29 public:
30     struct ParsedLocale {
31         std::string base;
32         std::string extension;
33     };
34     static JSHandle<EcmaString> UStringToString(JSThread *thread, const icu::UnicodeString &string);
35     static JSHandle<EcmaString> UStringToString(JSThread *thread, const icu::UnicodeString &string, int32_t begin,
36                                                 int32_t end);
37 
38     // 9.2.1 CanonicalizeLocaleList ( locales )
39     static JSHandle<TaggedArray> CanonicalizeLocaleList(JSThread *thread, const JSHandle<JSTaggedValue> &locales);
40     // 6.2.3 CanonicalizeUnicodeLocaleId ( locale )
41     static JSHandle<EcmaString> CanonicalizeUnicodeLocaleId(JSThread *thread, const JSHandle<EcmaString> &locale);
42     static JSHandle<EcmaString> ToLanguageTag(JSThread *thread, const icu::Locale &locale);
43     static std::vector<std::string> GetAvailableLocales(JSThread *thread, const char *key, const char *path);
44     static bool IsStructurallyValidLanguageTag(const JSHandle<EcmaString> &tag);
45     // 9.2.2 BestAvailableLocale ( availableLocales, locale )
46     static std::string BestAvailableLocale(const std::vector<std::string> &availableLocales,
47                                            const std::string &locale);
48 
49     static JSHandle<EcmaString> DefaultLocale(JSThread *thread);
50     static LocaleHelper::ParsedLocale HandleLocale(const JSHandle<EcmaString> &localeString);
51     static void HandleLocaleExtension(size_t &start, size_t &extensionEnd, const std::string result, size_t len);
52     static std::string ConvertToStdString(const JSHandle<EcmaString> &ecmaStr);
53 private:
54     template<typename T>
55     static JSHandle<TaggedArray> CanonicalizeHelper(JSThread *thread, JSHandle<T> &obj, JSHandle<TaggedArray> &seen);
56     static bool DealwithLanguageTag(const std::vector<std::string> &containers, size_t &address);
57 
AsciiAlphaToLower(uint32_t c)58     static inline constexpr int AsciiAlphaToLower(uint32_t c)
59     {
60         constexpr uint32_t FLAG = 0x20;
61         return static_cast<int>(c | FLAG);
62     }
63 
IsLanguageSubtag(const std::string & value)64     static bool IsLanguageSubtag(const std::string &value)
65     {
66         return IsAlpha(value, INTL_INDEX_TWO, INTL_INDEX_THREE) || IsAlpha(value, INTL_INDEX_FIVE, INTL_INDEX_EIGHT);
67     }
68 
IsScriptSubtag(const std::string & value)69     static bool IsScriptSubtag(const std::string &value)
70     {
71         return IsAlpha(value, INTL_INDEX_FOUR, INTL_INDEX_FOUR);
72     }
73 
IsRegionSubtag(const std::string & value)74     static bool IsRegionSubtag(const std::string &value)
75     {
76         return IsAlpha(value, INTL_INDEX_TWO, INTL_INDEX_TWO) || IsDigit(value, INTL_INDEX_THREE, INTL_INDEX_THREE);
77     }
78 
IsVariantSubtag(const std::string & value)79     static bool IsVariantSubtag(const std::string &value)
80     {
81         return IsThirdDigitAlphanum(value) || IsAlphanum(value, INTL_INDEX_FIVE, INTL_INDEX_EIGHT);
82     }
83 
IsThirdDigitAlphanum(const std::string & value)84     static bool IsThirdDigitAlphanum(const std::string &value)
85     {
86         return InRange(value[0], '0', '9') && value.length() == INTL_INDEX_FOUR &&
87             IsAlphanum(value.substr(1), INTL_INDEX_THREE, INTL_INDEX_THREE);
88     }
89 
IsExtensionSingleton(const std::string & value)90     static bool IsExtensionSingleton(const std::string &value)
91     {
92         return IsAlphanum(value, 1, 1);
93     }
94 
IsPrivateSubTag(std::string result,size_t len)95     static bool IsPrivateSubTag(std::string result, size_t len)
96     {
97         if ((len > 1) && (result[1] == '-')) {
98             ASSERT(result[0] == 'x' || result[0] == 'i');
99             return true;
100         }
101         return false;
102     }
103 
104     template<typename T, typename U>
InRange(T value,U start,U end)105     static bool InRange(T value, U start, U end)
106     {
107         ASSERT(start <= end);
108         ASSERT(sizeof(T) >= sizeof(U));
109         return (value >= static_cast<T>(start)) && (value <= static_cast<T>(end));
110     }
111 
IsAsciiAlpha(char ch)112     static bool IsAsciiAlpha(char ch)
113     {
114         return InRange(ch, 'A', 'Z') || InRange(ch, 'a', 'z');
115     }
116 
IsAlpha(const std::string & str,size_t min,size_t max)117     static bool IsAlpha(const std::string &str, size_t min, size_t max)
118     {
119         if (!InRange(str.length(), min, max)) {
120             return false;
121         }
122         for (char c : str) {
123             if (!IsAsciiAlpha(c)) {
124                 return false;
125             }
126         }
127         return true;
128     }
129 
IsDigit(const std::string & str,size_t min,size_t max)130     static bool IsDigit(const std::string &str, size_t min, size_t max)
131     {
132         if (!InRange(str.length(), min, max)) {
133             return false;
134         }
135         for (char i : str) {
136             if (!InRange(i, '0', '9')) {
137                 return false;
138             }
139         }
140         return true;
141     }
142 
IsAlphanum(const std::string & str,size_t min,size_t max)143     static bool IsAlphanum(const std::string &str, size_t min, size_t max)
144     {
145         if (!InRange(str.length(), min, max)) {
146             return false;
147         }
148         for (char i : str) {
149             if (!IsAsciiAlpha(i) && !InRange(i, '0', '9')) {
150                 return false;
151             }
152         }
153         return true;
154     }
155 
ValidateOtherTags(const icu::Locale & locale,const char * packageName,const char * key,bool & res)156     static bool ValidateOtherTags(const icu::Locale &locale, const char *packageName, const char *key, bool &res)
157     {
158         const char *localeCountry = locale.getCountry();
159         const char *localeScript = locale.getScript();
160         if (localeCountry[0] != '\0' && localeScript[0] != '\0') {
161             std::string removeCountry = locale.getLanguage();
162             removeCountry.append("-");
163             removeCountry.append(localeScript);
164             return CheckLocales(removeCountry.c_str(), key, packageName, res);
165         }
166         if (localeCountry[0] != '\0' || localeScript[0] != '\0') {
167             std::string language = locale.getLanguage();
168             return CheckLocales(language.c_str(), key, packageName, res);
169         }
170         return res;
171     }
172 
CheckLocales(const icu::Locale & locale,const char * key,const char * packageName,bool & res)173     static bool CheckLocales(const icu::Locale &locale, const char *key, const char *packageName, bool &res)
174     {
175         res = false;
176         UErrorCode status = U_ZERO_ERROR;
177         const char *formalLocale = locale.getName();
178         UResourceBundle *localeRes = ures_open(packageName, formalLocale, &status);
179         if (localeRes != nullptr && status == U_ZERO_ERROR) {
180             if (key == nullptr) {
181                 res = true;
182             } else {
183                 UResourceBundle *keyRes = ures_getByKey(localeRes, key, nullptr, &status);
184                 if (keyRes != nullptr && status == U_ZERO_ERROR) {
185                     res = true;
186                 }
187                 ures_close(keyRes);
188             }
189         }
190         ures_close(localeRes);
191         if (res) {
192             return res;
193         } else {
194             ValidateOtherTags(locale, packageName, key, res);
195         }
196         return res;
197     }
198 };
199 }
200 #endif  // ECMASCRIPT_BASE_LOCALE_HELPER_H