• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_INTLLOCALETAG_H
17 #define PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_INTLLOCALETAG_H
18 
19 #include "unicode/locid.h"
20 
21 #include <string>
22 #include <vector>
23 
24 namespace ark::ets::stdlib::intl {
25 constexpr size_t INTL_INDEX_TWO = 2;
26 constexpr size_t INTL_INDEX_THREE = 3;
27 constexpr size_t INTL_INDEX_FOUR = 4;
28 constexpr size_t INTL_INDEX_FIVE = 5;
29 constexpr size_t INTL_INDEX_EIGHT = 8;
30 constexpr uint32_t FLAG = 0x20;
31 
32 std::string ToStdStringLanguageTag(const icu::Locale &locale);
33 
34 bool IsStructurallyValidLanguageTag(const std::string &tag);
35 
36 bool IsPrivateSubTag(const std::string &result, size_t &len);
37 
38 class LanguageTagListIterator : public icu::Locale::Iterator {
39 public:
LanguageTagListIterator(const std::vector<std::string> & tags)40     explicit LanguageTagListIterator(const std::vector<std::string> &tags) : tags_(tags), it_(tags.cbegin()) {}
41 
hasNext()42     UBool hasNext() const override
43     {
44         return static_cast<UBool>(it_ != tags_.cend());
45     }
46 
next()47     const icu::Locale &next() override
48     {
49         auto status = UErrorCode::U_ZERO_ERROR;
50         locale_ = icu::Locale::forLanguageTag((it_++)->c_str(), status);
51         return locale_;
52     }
53 
54 private:
55     const std::vector<std::string> &tags_;
56     std::vector<std::string>::const_iterator it_;
57     icu::Locale locale_;
58 };
59 
AsciiAlphaToLower(uint32_t c)60 inline constexpr int AsciiAlphaToLower(uint32_t c)
61 {
62     return static_cast<int>(c | FLAG);
63 }
64 
65 }  // namespace ark::ets::stdlib::intl
66 
67 #endif  //  PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_INTLLOCALETAG_H
68