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 #ifndef OHOS_ABILITY_BASE_ZCHAR_WRAPPER_H
16 #define OHOS_ABILITY_BASE_ZCHAR_WRAPPER_H
17 
18 #include "base_obj.h"
19 #include "refbase.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 class Char final : public Object, public IChar {
24 public:
Char(zchar value)25     inline Char(zchar value) : value_(value)
26     {}
27 
~Char()28     inline ~Char()
29     {}
30 
31     IINTERFACE_DECL();
32 
33     ErrCode GetValue(zchar &value) override; /* [out] */
34 
35     bool Equals(IObject &other) override; /* [in] */
36 
37     std::string ToString() override;
38 
39     static sptr<IChar> Box(zchar value); /* [in] */
40 
41     static zchar Unbox(IChar *object); /* [in] */
42 
43     static sptr<IChar> Parse(const std::string &str); /* [in] */
44 
45     static int GetByteSize(zchar c); /* [in] */
46 
47     static void WriteUTF8Bytes(char *dst, /* [in] */
48         zchar c,                          /* [in] */
49         int size);                        /* [in] */
50 
51     static zchar GetChar(const std::string &str, /* [in] */
52         int index);                              /* [in] */
53 
54     static constexpr char SIGNATURE = 'C';
55 
56     static constexpr zchar INVALID_CHAR = 0x110000;
57 
58 private:
59     static zchar GetCharInternal(const unsigned char *cur, /* [in] */
60         int &size);                                        /* [in] */
61 
62     static constexpr int MIN_CODE_POINT = 0x000000;
63     static constexpr int MAX_CODE_POINT = 0x10FFFF;
64     static constexpr int MIN_HIGH_SURROGATE = 0xD800;
65     static constexpr int MAX_HIGH_SURROGATE = 0xDBFF;
66     static constexpr int MIN_LOW_SURROGATE = 0xDC00;
67     static constexpr int MAX_LOW_SURROGATE = 0xDFFF;
68 
69     static constexpr int BYTE_COUNT_1 = 1;
70     static constexpr int BYTE_COUNT_2 = 2;
71     static constexpr int BYTE_COUNT_3 = 3;
72     static constexpr int BYTE_COUNT_4 = 4;
73 
74     static constexpr zchar BYTE_MASK = 0x000000BF;
75     static constexpr zchar BYTE_MARK = 0x00000080;
76     static constexpr int BYTE_SHIFT = 6;
77 
78     // (00-7f) 7bit -> 0xxxxxxx, bit mask is 0x00000000
79     // (c0-df)(80-bf) 11bit -> 110yyyyx 10xxxxxx, bit mask is 0x000000C0
80     // (e0-ef)(80-bf)(80-bf) 16bit -> 1110yyyy 10yxxxxx 10xxxxxx, bit mask is 0x000000E0
81     // (f0-f7)(80-bf)(80-bf)(80-bf) 21bit -> 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx, bit mask is 0x000000F0
82     static constexpr zchar FIRST_BYTE_MARK[] = {0x00000000, 0x00000000, 0x000000C0, 0x000000E0, 0x000000F0};
83 
84     zchar value_;
85 };
86 }  // namespace AAFwk
87 }  // namespace OHOS
88 
89 #endif
90