-
Notifications
You must be signed in to change notification settings - Fork 0
fix/wrong-type-at-arm-char-unsigned #10
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| cmake-build*/ | ||
| **/cmake-build*/ | ||
| /.idea | ||
| !/.idea/dictionaries | ||
| !/.idea/dictionaries | ||
|
|
||
| **/localtest*/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # ZACLib/Android.mk | ||
| LOCAL_PATH := $(call my-dir) | ||
|
|
||
| include $(CLEAR_VARS) | ||
|
|
||
| LOCAL_MODULE := ZACLib | ||
| LOCAL_SRC_FILES := ZACLib.cpp | ||
| LOCAL_C_INCLUDES := $(LOCAL_PATH) | ||
|
|
||
| LOCAL_CPPFLAGS := -std=c++11 # or higher | ||
|
|
||
| include $(BUILD_STATIC_LIBRARY) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
|
|
||
| set(CMAKE_CXX_STANDARD 11) # or higher | ||
|
|
||
| # ZACLib/CMakeLists.txt | ||
| add_library(ZACLib STATIC "ZACLib.cpp") # if use source | ||
| target_include_directories(ZACLib PUBLIC ".") | ||
| set_target_properties(ZACLib PROPERTIES | ||
| CXX_STANDARD 11 | ||
| CXX_STANDARD 11 # or higner | ||
| CXX_STANDARD_REQUIRED YES | ||
| CXX_EXTENSIONS NO | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,38 +22,72 @@ | |
| #define ZACLIB_TYPES_HPP | ||
|
|
||
| #if __cplusplus >= 201703L | ||
| #include <string_view> | ||
| #include <string_view> | ||
| #else | ||
| #include <cstring> | ||
| #include <cstring> | ||
| #endif | ||
|
|
||
| #include <string> | ||
| #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; | ||
| class ZAC_SV : public std::basic_string_view<ZAC_CHAR> { | ||
| public: | ||
| using std::basic_string_view<ZAC_CHAR>::basic_string_view; | ||
| ZAC_SV() = default; | ||
|
|
||
| // ReSharper disable once CppRedundantCastExpression | ||
| ZAC_SV(const std::string& s) noexcept | ||
| : std::basic_string_view<ZAC_CHAR>(reinterpret_cast<const ZAC_CHAR*>(s.data()), s.size()) {} | ||
|
|
||
| #if defined(__ANDROID__) || defined(__arm__) || defined(__aarch64__) | ||
| ZAC_SV(const char* s, const size_t n) | ||
| : std::basic_string_view<ZAC_CHAR>(reinterpret_cast<const ZAC_CHAR*>(s), n) {} | ||
|
|
||
| ZAC_SV(const char* s) | ||
| : std::basic_string_view<ZAC_CHAR>( | ||
| reinterpret_cast<const ZAC_CHAR*>(s), | ||
| s ? std::char_traits<char>::length(s) : 0) {} | ||
| #endif | ||
| }; | ||
| #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) {} | ||
|
|
||
| #if defined(__ANDROID__) || defined(__arm__) || defined(__aarch64__) | ||
| ZAC_SV(const char* d, const size_t s) : m_data(reinterpret_cast<const ZAC_CHAR*>(d)), | ||
| m_size(s) {} | ||
| #endif | ||
|
|
||
| ZAC_SV(const std::string& s) : m_data(s.c_str()), | ||
| // ReSharper disable once CppRedundantCastExpression | ||
| ZAC_SV(const std::string& s) noexcept : m_data(reinterpret_cast<const ZAC_CHAR*>(s.data())), | ||
| 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 { | ||
| #if defined(__ANDROID__) || defined(__arm__) || defined(__aarch64__) | ||
| ZAC_SV(const char* d) : m_data(reinterpret_cast<const ZAC_CHAR*>(d)), | ||
| m_size(d ? std::strlen(d) : 0) {} | ||
| #endif | ||
|
|
||
| const ZAC_CHAR* data() const noexcept { | ||
| return m_data; | ||
| } | ||
|
|
||
|
|
@@ -65,12 +99,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 | ||
|
|
||
|
|
@@ -89,7 +123,6 @@ namespace ZACLib { | |
| } | ||
|
|
||
| #endif //ZACLIB_TYPES_HPP | ||
|
|
||
| // ZACLib_Types.hpp end | ||
|
|
||
| namespace ZACLib { | ||
|
|
@@ -105,7 +138,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 +149,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()); | ||
|
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. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -158,7 +191,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 +239,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 +263,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 +288,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 +299,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()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # ZACLib/Makefile | ||
| CXX := g++ | ||
| CXXFLAGS := -I./ \ | ||
| -std=c++11 # or higher | ||
|
|
||
| SRC := ZACLib.cpp | ||
| OBJ := $(SRC:.cpp=.o) | ||
| TARGET := ZACLib | ||
|
|
||
| all: $(TARGET) | ||
|
|
||
| $(TARGET): $(OBJ) | ||
| $(CXX) $(OBJ) -o $(TARGET) | ||
|
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. |
||
|
|
||
| %.o: %.cpp | ||
| $(CXX) $(CXXFLAGS) -c $< -o $@ | ||
|
|
||
| clean: | ||
| rm -f $(OBJ) $(TARGET) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| // Created by wanjiangzhi on 2026/2/24. | ||
| // | ||
|
|
||
| // ReSharper disable CppClassNeverUsed | ||
| #include "ZACLib.hpp" | ||
| #include <array> | ||
| #include <queue> | ||
|
|
@@ -13,26 +12,26 @@ namespace ZACLib { | |
| } | ||
|
|
||
| void Replace::AddRule(const ZAC_SV& from) { | ||
| AddRule(from, ZAC_SV("", 0)); | ||
| AddRule(from, ZAC_SV("")); | ||
| } | ||
|
|
||
| void Replace::AddRule(const ZAC_SV& from, const ZAC_SV& to) { | ||
| 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) | ||
| trie[node].next[c] = static_cast<Node::value_type>(trie.size()); | ||
|
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. A narrowing conversion occurs here when casting if (trie.size() >= static_cast<size_t>(std::numeric_limits<Node::value_type>::max())) {
// Handle error: Trie too large
return;
}
trie[node].next[c] = static_cast<Node::value_type>(trie.size()); |
||
| trie.emplace_back(); | ||
| } | ||
| node = trie[node].next[c]; | ||
| } | ||
|
|
||
| if (from.size() > trie[node].match_len) { | ||
| trie[node].output_id = outputs.size(); // NOLINT(*-narrowing-conversions) | ||
| trie[node].output_id = static_cast<Node::value_type>(outputs.size()); | ||
|
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. This line involves a critical narrowing conversion. Casting if (outputs.size() >= static_cast<size_t>(std::numeric_limits<Node::value_type>::max())) {
// Handle error: Too many patterns
return;
}
trie[node].output_id = static_cast<Node::value_type>(outputs.size()); |
||
| trie[node].match_len = from.size(); | ||
| outputs.emplace_back(to.data(), to.size()); | ||
| outputs.emplace_back(ArmCastChar(to.data()), to.size()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -62,7 +61,7 @@ namespace ZACLib { | |
| if (v != -1) { | ||
| trie[v].fail = trie[trie[u].fail].next[c]; | ||
|
|
||
| int f = trie[v].fail; | ||
| const int f = trie[v].fail; | ||
| if (trie[f].match_len > trie[v].match_len) { | ||
| trie[v].match_len = trie[f].match_len; | ||
| trie[v].output_id = trie[f].output_id; | ||
|
|
@@ -82,11 +81,11 @@ namespace ZACLib { | |
|
|
||
| if (input.empty()) return result; | ||
| if (max_rule_len == 0) { | ||
| result.append(input.data(), input.size()); | ||
| result.append(ArmCastChar(input.data()), input.size()); | ||
| return result; | ||
| } | ||
|
|
||
| const auto invalid_output = Node::kInvalidOutput; | ||
| constexpr auto invalid_output = Node::kInvalidOutput; | ||
| const size_t ring_size = max_rule_len + 1; | ||
| std::vector<size_t> pending_start(ring_size, invalid_output); | ||
| std::vector<size_t> pending_len(ring_size, 0); | ||
|
|
@@ -121,24 +120,23 @@ namespace ZACLib { | |
| size_t cursor = 0; | ||
|
|
||
| auto emit_until = [&](const size_t upper_bound) { | ||
| while (cursor < upper_bound) { | ||
| while (cursor < upper_bound) { // NOLINT | ||
| size_t best_len = 0; | ||
| size_t best_output = invalid_output; | ||
| get_best(cursor, best_len, best_output); | ||
| if (best_len == 0) { | ||
| ++cursor; | ||
| continue; | ||
| } | ||
|
|
||
| result.append(input.data() + last_pos, cursor - last_pos); | ||
| result.append(ArmCastChar(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) { | ||
|
|
@@ -154,7 +152,7 @@ namespace ZACLib { | |
| emit_until(input.size()); | ||
|
|
||
| if (last_pos < input.size()) { | ||
| result.append(input.data() + last_pos, input.size() - last_pos); | ||
| result.append(ArmCastChar(input.data() + last_pos), input.size() - last_pos); | ||
| } | ||
|
|
||
| return result; | ||
|
|
@@ -168,19 +166,20 @@ namespace ZACLib { | |
| void Search::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) | ||
| trie[node].next[c] = static_cast<Node::value_type>(trie.size()); | ||
|
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. Similar to the issue in if (trie.size() >= static_cast<size_t>(std::numeric_limits<Node::value_type>::max())) {
// Handle error
return;
}
trie[node].next[c] = static_cast<Node::value_type>(trie.size()); |
||
| trie.emplace_back(); | ||
| } | ||
| node = trie[node].next[c]; | ||
| } | ||
|
|
||
| if (from.size() > trie[node].match_len) { | ||
| trie[node].output_id = outputs.size(); // NOLINT(*-narrowing-conversions) | ||
|
|
||
| trie[node].output_id = outputs.size(); | ||
| trie[node].match_len = from.size(); | ||
| outputs.emplace_back(from.data(), from.size()); | ||
| outputs.emplace_back(ArmCastChar(from.data()), from.size()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -253,7 +252,7 @@ namespace ZACLib { | |
| int node = 0; | ||
| for (const unsigned char c : from) { | ||
| if (trie[node].next[c] == -1) { | ||
| trie[node].next[c] = trie.size(); // NOLINT(*-narrowing-conversions) | ||
| trie[node].next[c] = static_cast<Node::value_type>(trie.size()); | ||
|
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. This narrowing conversion from if (trie.size() >= static_cast<size_t>(std::numeric_limits<Node::value_type>::max())) {
// Handle error
return;
}
trie[node].next[c] = static_cast<Node::value_type>(trie.size()); |
||
| trie.emplace_back(); | ||
| } | ||
| node = trie[node].next[c]; | ||
|
|
||
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.
这里有一个拼写错误,“higner” 应该是 “higher”。