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