Unify ZAC_CHAR handling for cross-platform signedness differences#7
Unify ZAC_CHAR handling for cross-platform signedness differences#7WJiangzhi wants to merge 1 commit into
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical cross-platform compatibility issue where the signedness of the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses cross-platform character signedness issues by introducing a ZAC_CHAR type and updating the library to use it, ensuring correct behavior on platforms where char is unsigned. The use of static_cast for trie indexing and reinterpret_cast for API compatibility is appropriate. However, I've identified a critical issue in the C++17 implementation of ZAC_SV. Aliasing it directly to std::basic_string_view<ZAC_CHAR> breaks compilation on platforms like ARM where ZAC_CHAR is unsigned char, as it prevents construction from std::string or string literals. I have provided suggestions to resolve this by using a wrapper class, which will restore API compatibility.
| 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.
On platforms where ZAC_CHAR is unsigned char (e.g., ARM/Android), aliasing ZAC_SV directly to std::basic_string_view<ZAC_CHAR> breaks API compatibility for C++17 and later. Code that passes string literals or std::string objects to functions expecting a ZAC_SV will fail to compile because there's no implicit conversion from std::string (which uses char) to std::basic_string_view<unsigned char>.
To fix this while retaining the benefits of std::string_view, ZAC_SV can be a wrapper class that inherits from std::basic_string_view<ZAC_CHAR> and provides the necessary constructor from std::string.
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())
{}
};| 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.
On platforms where ZAC_CHAR is unsigned char (e.g., ARM/Android), aliasing ZAC_SV directly to std::basic_string_view<ZAC_CHAR> breaks API compatibility for C++17 and later. Code that passes string literals or std::string objects to functions expecting a ZAC_SV will fail to compile because there's no implicit conversion from std::string (which uses char) to std::basic_string_view<unsigned char>.
To fix this while retaining the benefits of std::string_view, ZAC_SV can be a wrapper class that inherits from std::basic_string_view<ZAC_CHAR> and provides the necessary constructor from std::string.
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())
{}
};
Motivation
charas signed or unsigned which can cause mismatches or incorrect indexing when treating bytes as array indices, leading to crashes or incorrect behavior in trie/state-machine code.Description
ZAC_CHAR(unsigned charon Android/ARM/AArch64,charelsewhere) and madeZAC_SVastd::basic_string_view<ZAC_CHAR>for C++17 or a compatible fallback class usingZAC_CHARstorage.ZAC_CHAR(e.g.for (const ZAC_CHAR i : from)) and consistently cast characters tounsigned charfor trie indexing (static_cast<unsigned char>(...)).reinterpret_cast<const char*>conversions when interacting withstd::stringAPIs (e.g.std::string::append,outputs.emplace_back) to bridge theZAC_CHAR*representation tochar*-based APIs safely.ZACLib_single.hppto keep the header-only distribution in sync with the library implementation.Testing
cmake -S /workspace/ZACLib -B /workspace/ZACLib/build && cmake --build /workspace/ZACLib/build, which completed successfully (library and example built).Codex Task