-
Notifications
You must be signed in to change notification settings - Fork 0
Unify ZAC_CHAR handling for cross-platform signedness differences #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,29 +31,36 @@ | |
| #include <array> | ||
| #include <limits> | ||
|
|
||
| #if defined(__ANDROID__) || defined(__arm__) || defined(__aarch64__) | ||
| using ZAC_CHAR = unsigned char; | ||
| #else | ||
| using ZAC_CHAR = char; | ||
| #endif | ||
|
|
||
|
|
||
| namespace ZACLib { | ||
| #if __cplusplus >= 201703L | ||
| using ZAC_SV = std::string_view; | ||
| using ZAC_SV = std::basic_string_view<ZAC_CHAR>; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On platforms where To fix this while retaining the benefits of class ZAC_SV : public std::basic_string_view<ZAC_CHAR> {
public:
using std::basic_string_view<ZAC_CHAR>::basic_string_view;
ZAC_SV() = default;
ZAC_SV(const std::string& s) noexcept
: std::basic_string_view<ZAC_CHAR>(reinterpret_cast<const ZAC_CHAR*>(s.data()), s.size())
{}
}; |
||
| #else | ||
| class ZAC_SV { | ||
| const char* m_data; | ||
| const ZAC_CHAR* m_data; | ||
| const size_t m_size; | ||
|
|
||
| public: | ||
| ZAC_SV() : m_data(nullptr), | ||
| m_size(0) {} | ||
|
|
||
| ZAC_SV(const char* d, const size_t s) : m_data(d), | ||
| ZAC_SV(const ZAC_CHAR* d, const size_t s) : m_data(d), | ||
| m_size(s) {} | ||
|
|
||
| ZAC_SV(const std::string& s) : m_data(s.c_str()), | ||
| // ReSharper disable once CppRedundantCastExpression | ||
| ZAC_SV(const std::string& s) : m_data(reinterpret_cast<const ZAC_CHAR*>(s.c_str())), | ||
| m_size(s.size()) {} // 模仿std::string_view,不禁止隐式构造 | ||
|
|
||
| ZAC_SV(const char* d) : m_data(d), | ||
| m_size(d ? std::strlen(d) : 0) {} // 模仿std::string_view,不禁止隐式构造 | ||
| ZAC_SV(const ZAC_CHAR* d) : m_data(d), | ||
| m_size(d ? std::strlen(reinterpret_cast<const char*>(d)) : 0) {} // 模仿std::string_view,不禁止隐式构造 | ||
|
|
||
| const char* data() const noexcept { | ||
| const ZAC_CHAR* data() const noexcept { | ||
| return m_data; | ||
| } | ||
|
|
||
|
|
@@ -65,12 +72,12 @@ namespace ZACLib { | |
| return m_size == 0; | ||
| } | ||
|
|
||
| const char* begin() const { return m_data; } | ||
| const char* end() const { return m_data + m_size; } | ||
| const char* cbegin() const { return m_data; } | ||
| const char* cend() const { return m_data + m_size; } | ||
| const ZAC_CHAR* begin() const { return m_data; } | ||
| const ZAC_CHAR* end() const { return m_data + m_size; } | ||
| const ZAC_CHAR* cbegin() const { return m_data; } | ||
| const ZAC_CHAR* cend() const { return m_data + m_size; } | ||
|
|
||
| const char& operator[](const size_t i) const { return m_data[i]; } | ||
| const ZAC_CHAR& operator[](const size_t i) const { return m_data[i]; } | ||
| }; | ||
| #endif | ||
|
|
||
|
|
@@ -105,7 +112,7 @@ namespace ZACLib { | |
| if (from.empty()) return; | ||
| if (from.size() > max_rule_len) max_rule_len = from.size(); | ||
| int node = 0; | ||
| for (const char i : from) { | ||
| for (const ZAC_CHAR i : from) { | ||
| const auto c = static_cast<unsigned char>(i); | ||
| if (trie[node].next[c] == -1) { | ||
| trie[node].next[c] = trie.size(); // NOLINT(*-narrowing-conversions) | ||
|
|
@@ -116,7 +123,7 @@ namespace ZACLib { | |
| if (from.size() > trie[node].match_len) { | ||
| trie[node].output_id = outputs.size(); // NOLINT(*-narrowing-conversions) | ||
| trie[node].match_len = from.size(); | ||
| outputs.emplace_back(to.data(), to.size()); | ||
| outputs.emplace_back(reinterpret_cast<const char*>(to.data()), to.size()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -158,7 +165,7 @@ namespace ZACLib { | |
|
|
||
| if (input.empty()) return result; | ||
| if (max_rule_len == 0) { | ||
| result.append(input.data(), input.size()); | ||
| result.append(reinterpret_cast<const char*>(input.data()), input.size()); | ||
| return result; | ||
| } | ||
|
|
||
|
|
@@ -206,15 +213,15 @@ namespace ZACLib { | |
| continue; | ||
| } | ||
|
|
||
| result.append(input.data() + last_pos, cursor - last_pos); | ||
| result.append(reinterpret_cast<const char*>(input.data() + last_pos), cursor - last_pos); | ||
| result.append(outputs[best_output]); | ||
| cursor += best_len; | ||
| last_pos = cursor; | ||
| } | ||
| }; | ||
|
|
||
| for (size_t i = 0; i < input.size(); ++i) { | ||
| const unsigned char c = input[i]; | ||
| const auto c = static_cast<unsigned char>(input[i]); | ||
| state = trie[state].next[c]; | ||
|
|
||
| if (trie[state].output_id != invalid_output) { | ||
|
|
@@ -230,7 +237,7 @@ namespace ZACLib { | |
| emit_until(input.size()); | ||
|
|
||
| if (last_pos < input.size()) { | ||
| result.append(input.data() + last_pos, input.size() - last_pos); | ||
| result.append(reinterpret_cast<const char*>(input.data() + last_pos), input.size() - last_pos); | ||
| } | ||
|
|
||
| return result; | ||
|
|
@@ -255,7 +262,7 @@ namespace ZACLib { | |
| void AddRule(const ZAC_SV& from) { | ||
| if (from.empty()) return; | ||
| int node = 0; | ||
| for (const char i : from) { | ||
| for (const ZAC_CHAR i : from) { | ||
| const auto c = static_cast<unsigned char>(i); | ||
| if (trie[node].next[c] == -1) { | ||
| trie[node].next[c] = trie.size(); // NOLINT(*-narrowing-conversions) | ||
|
|
@@ -266,7 +273,7 @@ namespace ZACLib { | |
| if (from.size() > trie[node].match_len) { | ||
| trie[node].output_id = outputs.size(); // NOLINT(*-narrowing-conversions) | ||
| trie[node].match_len = from.size(); | ||
| outputs.emplace_back(from.data(), from.size()); | ||
| outputs.emplace_back(reinterpret_cast<const char*>(from.data()), from.size()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On platforms where
ZAC_CHARisunsigned char(e.g., ARM/Android), aliasingZAC_SVdirectly tostd::basic_string_view<ZAC_CHAR>breaks API compatibility for C++17 and later. Code that passes string literals orstd::stringobjects to functions expecting aZAC_SVwill fail to compile because there's no implicit conversion fromstd::string(which useschar) tostd::basic_string_view<unsigned char>.To fix this while retaining the benefits of
std::string_view,ZAC_SVcan be a wrapper class that inherits fromstd::basic_string_view<ZAC_CHAR>and provides the necessary constructor fromstd::string.