1 /*
2 * Copyright (c) 2023 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 #include "text_converter.h"
17
18 #include <unicode/utf8.h>
19
20 namespace OHOS {
21 namespace Rosen {
22 namespace TextEngine {
ToUTF8(const UTF16String & utf16Text)23 UTF8String TextConverter::ToUTF8(const UTF16String &utf16Text)
24 {
25 size_t utf16Index = 0;
26 size_t codePoint = 0;
27 UTF8String utf8Text;
28 while (utf16Index < utf16Text.size()) {
29 U16_NEXT(utf16Text.data(), utf16Index, utf16Text.size(), codePoint);
30 size_t utf8Index = 0;
31 int error = 0;
32 const int size = 8;
33 uint8_t utf8Str[size] = {};
34 U8_APPEND(utf8Str, utf8Index, sizeof(utf8Str), codePoint, error);
35 if (utf8Index >= size) {
36 return utf8Text;
37 }
38
39 for (int i = 0; i < static_cast<int>(utf8Index); i++) {
40 utf8Text.push_back(utf8Str[i]);
41 }
42 }
43 utf8Text.push_back(0);
44 return utf8Text;
45 }
46
ToStr(const UTF16String & utf16Text)47 std::string TextConverter::ToStr(const UTF16String &utf16Text)
48 {
49 return reinterpret_cast<char *>(ToUTF8(utf16Text).data());
50 }
51
ToUTF16(const std::string & utf8Text)52 UTF16String TextConverter::ToUTF16(const std::string &utf8Text)
53 {
54 size_t utf8Index = 0;
55 size_t codePoint = 0;
56 UTF16String utf16Text;
57 while (utf8Index < utf8Text.size()) {
58 U8_NEXT(utf8Text.c_str(), utf8Index, utf8Text.size(), codePoint);
59 if (U16_LENGTH(codePoint) == 1) {
60 utf16Text.push_back(codePoint);
61 } else {
62 utf16Text.push_back(U16_LEAD(codePoint));
63 utf16Text.push_back(U16_TRAIL(codePoint));
64 }
65 }
66 return utf16Text;
67 }
68
ToUTF16(const UTF8String & utf8Text)69 UTF16String TextConverter::ToUTF16(const UTF8String &utf8Text)
70 {
71 size_t utf8Index = 0;
72 size_t codePoint = 0;
73 UTF16String utf16Text;
74 while (utf8Index < utf8Text.size()) {
75 U8_NEXT(utf8Text.data(), utf8Index, utf8Text.size(), codePoint);
76 if (U16_LENGTH(codePoint) == 1) {
77 utf16Text.push_back(codePoint);
78 } else {
79 utf16Text.push_back(U16_LEAD(codePoint));
80 utf16Text.push_back(U16_TRAIL(codePoint));
81 }
82 }
83 return utf16Text;
84 }
85
ToUTF16(const UTF32String & utf32Text)86 UTF16String TextConverter::ToUTF16(const UTF32String &utf32Text)
87 {
88 size_t utf32Index = 0;
89 size_t codePoint = 0;
90 int error = 0;
91 UTF16String utf16Text;
92 while (utf32Index < utf32Text.size()) {
93 UTF32_NEXT_CHAR_SAFE(utf32Text.data(), utf32Index, utf32Text.size(), codePoint, error);
94 if (U16_LENGTH(codePoint) == 1) {
95 utf16Text.push_back(codePoint);
96 } else {
97 utf16Text.push_back(U16_LEAD(codePoint));
98 utf16Text.push_back(U16_TRAIL(codePoint));
99 }
100 }
101 return utf16Text;
102 }
103
ToUTF32(const UTF16String & utf16Text)104 UTF32String TextConverter::ToUTF32(const UTF16String &utf16Text)
105 {
106 size_t utf16Index = 0;
107 size_t codePoint = 0;
108 UTF32String utf32Text;
109 while (utf16Index < utf16Text.size()) {
110 U16_NEXT(utf16Text.data(), utf16Index, utf16Text.size(), codePoint);
111 utf32Text.push_back(codePoint);
112 }
113 return utf32Text;
114 }
115 } // namespace TextEngine
116 } // namespace Rosen
117 } // namespace OHOS
118