Skip to content

Commit 80aeb49

Browse files
conversion of uint8array between u16string are added
1 parent 86df60e commit 80aeb49

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/lz_string.cpp

+35-17
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ auto get_base_value(const std::u16string &alphabet,
6363
return base_reverse_dict[alphabet][character];
6464
}
6565

66-
auto convert_from_uint8_array(const std::vector<uint8_t> &data)
67-
-> std::u16string {
68-
size_t length = std::floor(data.size() / 2);
69-
std::vector<std::u16string> result(length);
70-
71-
for (int i = 0; i < length; ++i) {
72-
result.push_back(from_char_code(data[i * 2] * 256 + data[i * 2 + 1]));
73-
}
74-
75-
if (data.size() & 1u) {
76-
result.push_back(from_char_code(data[data.size() - 1] * 256));
77-
}
78-
79-
return join_array(result);
80-
}
81-
8266
/*
8367
* --------------------------------------------------------------------------------------------------
8468
* -- Public function declarations
@@ -94,6 +78,40 @@ auto to_utf16(std::string_view value) -> std::u16string {
9478
return {value.begin(), value.end()};
9579
}
9680

81+
auto to_uint8array(std::u16string_view value) -> std::vector<uint8_t> {
82+
bool is_odd = static_cast<uint16_t>(value[value.length() - 1]) % 256 == 0;
83+
84+
std::vector<uint8_t> buffer;
85+
buffer.reserve(value.length() * 2 - (is_odd ? 1 : 0));
86+
87+
for (int i = 0; i < value.length(); ++i) {
88+
auto current_value = static_cast<uint16_t>(value[i]);
89+
90+
buffer[i * 2] = current_value >> 8u;
91+
92+
if (!is_odd || i < value.length() - 1) {
93+
buffer[i * 2 + 1] = current_value % 256;
94+
}
95+
}
96+
97+
return buffer;
98+
}
99+
100+
auto from_uint8array(const std::vector<uint8_t> &value) -> std::u16string {
101+
size_t length = std::floor(value.size() / 2);
102+
std::vector<std::u16string> result(length);
103+
104+
for (int i = 0; i < length; ++i) {
105+
result.push_back(from_char_code(value[i * 2] * 256 + value[i * 2 + 1]));
106+
}
107+
108+
if (value.size() & 1u) {
109+
result.push_back(from_char_code(value[value.size() - 1] * 256));
110+
}
111+
112+
return join_array(result);
113+
}
114+
97115
auto compress(std::u16string_view input) -> std::u16string {
98116
if (input.empty()) {
99117
return {u""};
@@ -221,7 +239,7 @@ auto decompressUint8Array(const std::vector<uint8_t> &input) -> std::u16string {
221239
return u"";
222240
}
223241

224-
return decompress(convert_from_uint8_array(input));
242+
return decompress(from_uint8array(input));
225243
}
226244

227245
} // namespace pxd::lz_string

src/lz_string.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace pxd::lz_string {
1010

1111
auto to_utf8(std::u16string_view value) -> std::string;
1212
auto to_utf16(std::string_view value) -> std::u16string;
13+
auto to_uint8array(std::u16string_view value) -> std::vector<uint8_t>;
14+
auto from_uint8array(const std::vector<uint8_t> &value) -> std::u16string;
1315

1416
auto compress(std::u16string_view input) -> std::u16string;
1517
auto decompress(std::u16string_view input) -> std::u16string;

0 commit comments

Comments
 (0)