• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_JS_COLLATOR_H
17 #define ECMASCRIPT_JS_COLLATOR_H
18 
19 #include "ecmascript/js_locale.h"
20 
21 #include "unicode/udata.h"
22 
23 namespace panda::ecmascript {
24 enum class UsageOption : uint8_t { SORT = 0x01, SEARCH, EXCEPTION };
25 enum class CaseFirstOption : uint8_t { UPPER = 0x01, LOWER, FALSE_OPTION, UNDEFINED, EXCEPTION };
26 enum class SensitivityOption : uint8_t { BASE = 0x01, ACCENT, CASE, VARIANT, UNDEFINED, EXCEPTION };
27 enum class CompareStringsOption : uint8_t { NONE = 0x01, TRY_FAST_PATH };
28 
29 class JSCollator : public JSObject {
30 public:
31     // NOLINTNEXTLINE (readability-identifier-naming, fuchsia-statically-constructed-objects)
32     static const CString uIcuDataColl;
33 
34     static const std::map<std::string, CaseFirstOption> caseFirstMap;
35 
36     static const std::map<CaseFirstOption, UColAttributeValue> uColAttributeValueMap;
37 
38     CAST_CHECK(JSCollator, IsJSCollator);
39 
40     static constexpr size_t ICU_FIELD_OFFSET = JSObject::SIZE;
41     // icu field.
42     ACCESSORS(IcuField, ICU_FIELD_OFFSET, LOCALE_OFFSET)
43     ACCESSORS(Locale, LOCALE_OFFSET, COLLATION_OFFSET)
44     ACCESSORS(Collation, COLLATION_OFFSET, BOUND_COMPARE_OFFSET)
45     ACCESSORS(BoundCompare, BOUND_COMPARE_OFFSET, BIT_FIELD_OFFSET)
46     ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
47     DEFINE_ALIGN_SIZE(LAST_OFFSET);
48 
49     // define BitField
50     static constexpr size_t USAGE_BITS = 2;
51     static constexpr size_t CASE_FIRST_BITS = 3;
52     static constexpr size_t SENSITIVITY_BITS = 3;
53     static constexpr size_t IGNORE_PUNCTUATION_BITS = 1;
54     static constexpr size_t NUMERIC_BITS = 1;
FIRST_BIT_FIELD(BitField,Usage,UsageOption,USAGE_BITS)55     FIRST_BIT_FIELD(BitField, Usage, UsageOption, USAGE_BITS)
56     NEXT_BIT_FIELD(BitField, CaseFirst, CaseFirstOption, CASE_FIRST_BITS, Usage)
57     NEXT_BIT_FIELD(BitField, Sensitivity, SensitivityOption, SENSITIVITY_BITS, CaseFirst)
58     NEXT_BIT_FIELD(BitField, IgnorePunctuation, bool, IGNORE_PUNCTUATION_BITS, Sensitivity)
59     NEXT_BIT_FIELD(BitField, Numeric, bool, NUMERIC_BITS, IgnorePunctuation)
60 
61     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)
62     DECL_DUMP()
63 
64     icu::Collator *GetIcuCollator() const
65     {
66         ASSERT(GetIcuField().IsJSNativePointer());
67         JSNativePointer *nativePointer = JSNativePointer::Cast(GetIcuField().GetTaggedObject());
68         auto result = nativePointer->GetExternalPointer();
69         return reinterpret_cast<icu::Collator *>(result);
70     }
71 
72     static void FreeIcuCollator([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void *hint = nullptr)
73     {
74         if (pointer == nullptr) {
75             return;
76         }
77         auto icuCollator = reinterpret_cast<icu::Collator *>(pointer);
78         delete icuCollator;
79     }
80 
81     static void SetIcuCollator(JSThread *thread, const JSHandle<JSCollator> &collator,
82         icu::Collator *icuCollator, const NativePointerCallback &callback);
83 
84     // 11.1.1 InitializeCollator ( collator, locales, options )
85     static JSHandle<JSCollator> InitializeCollator(JSThread *thread, const JSHandle<JSCollator> &collator,
86                                                    const JSHandle<JSTaggedValue> &locales,
87                                                    const JSHandle<JSTaggedValue> &options,
88                                                    bool forIcuCache = false,
89                                                    bool enableLocaleCache = false);
90 
91     static icu::Collator *GetCachedIcuCollator(JSThread *thread, const JSHandle<JSTaggedValue> &locales);
92     static icu::Collator *GetCachedIcuCollator(JSThread *thread, const JSTaggedValue &locales);
93 
94     // 11.3.4 Intl.Collator.prototype.resolvedOptions ()
95     static JSHandle<JSObject> ResolvedOptions(JSThread *thread, const JSHandle<JSCollator> &collator);
96 
97     static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread, bool enableLocaleCache = false);
98 
99     static CompareStringsOption CompareStringsOptionFor(JSThread *thread, JSHandle<JSTaggedValue> locales);
100 
101     static CompareStringsOption CompareStringsOptionFor(JSThread *thread, JSHandle<JSTaggedValue> locales,
102                                                         JSHandle<JSTaggedValue> options);
103 
104     static JSTaggedValue CompareStrings(JSThread *thread, const icu::Collator *icuCollator,
105                                         const JSHandle<EcmaString> &string1, const JSHandle<EcmaString> &string2,
106                                         CompareStringsOption csOption = CompareStringsOption::NONE);
107 
108 private:
109     static CaseFirstOption StringToCaseFirstOption(const std::string &str);
110 
111     static UColAttributeValue OptionToUColAttribute(CaseFirstOption caseFirstOption);
112 
113     static std::set<std::string> BuildLocaleSet(const std::vector<std::string> &availableLocales, const char *path,
114                                                 const char *key);
115 
116     static void SetNumericOption(icu::Collator *icuCollator, bool numeric);
117 
118     static void SetCaseFirstOption(icu::Collator *icuCollator, CaseFirstOption caseFirstOption);
119 };
120 }  // namespace panda::ecmascript
121 #endif  // ECMASCRIPT_JS_COLLATOR_H
122