1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LATINIME_DICT_TOOLKIT_UTF8_UTILS_H
18 #define LATINIME_DICT_TOOLKIT_UTF8_UTILS_H
19 
20 #include <cstdint>
21 #include <string>
22 #include <vector>
23 
24 #include "dict_toolkit_defines.h"
25 #include "utils/int_array_view.h"
26 
27 namespace latinime {
28 namespace dicttoolkit {
29 
30 class Utf8Utils {
31 public:
32     static std::vector<int> getCodePoints(const std::string &utf8Str);
33     static std::string getUtf8String(const CodePointArrayView codePoints);
34 
35 private:
36     DISALLOW_IMPLICIT_CONSTRUCTORS(Utf8Utils);
37 
38     // Values indexed by sequence size.
39     static const size_t MAX_SEQUENCE_SIZE_FOR_A_CODE_POINT;
40     static const uint8_t FIRST_BYTE_MARKER_MASKS[];
41     static const uint8_t FIRST_BYTE_MARKERS[];
42     static const uint8_t FIRST_BYTE_CODE_POINT_BITS_MASKS[];
43     static const int MAX_ENCODED_CODE_POINT_VALUES[];
44 
45     static const uint8_t TRAILING_BYTE_CODE_POINT_BITS_MASK;
46     static const uint8_t TRAILING_BYTE_MARKER;
47     static const size_t CODE_POINT_BIT_COUNT_IN_TRAILING_BYTE;
48 
49     static int getSequenceSizeByCheckingFirstByte(const uint8_t firstByte);
50     static int maskFirstByte(const uint8_t firstByte, const int encodeSize);
51     static int maskTrailingByte(const uint8_t secondOrLaterByte);
52     static int getSequenceSizeToEncodeCodePoint(const int codePoint);
53 };
54 } // namespace dicttoolkit
55 } // namespace latinime
56 #endif // LATINIME_DICT_TOOLKIT_UTF8_UTILS_H
57