You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem was very difficult to detect. Because prints were the same. But strings weren't been the same. My string has a lot of white characters inside (Not only on end and begin).
I encountered this issue as well and came up with three potential improvements:
Add char to the runtime error
inlineuint32_tindex(const std::array<int8_t, 256>& rdata, char symbol) {
autoindex = rdata[static_cast<unsignedchar>(symbol)];
if (index <= -1) {
std::stringstream ss;
ss << "Invalid input: '" << symbol << "' (0x" << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(symbol) << ") not within alphabet";
throwstd::runtime_error(ss.str());
}
returnstatic_cast<uint32_t>(index);
}
Add char and offset to runtime error
inlineuint32_tindex(const std::array<int8_t, 256>& rdata, char symbol, size_t offset) {
autoindex = rdata[static_cast<unsignedchar>(symbol)];
if (index <= -1) {
std::stringstream ss;
ss << "Invalid input: '" << symbol << "' (0x" << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(symbol) << ") at position " << std::dec << offset << " not within alphabet";
throwstd::runtime_error(ss.str());
}
returnstatic_cast<uint32_t>(index);
}
Same as 2 but handover base and offset
inlineuint32_tindex(const std::array<int8_t, 256>& rdata, const std::string& base, size_t offset) {
auto symbol = base[offset];
autoindex = rdata[static_cast<unsignedchar>(symbol)];
if (index <= -1) {
std::stringstream ss;
ss << "Invalid input: '" << symbol << "' (0x" << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<int>(symbol) << ") at position " << std::dec << offset << " not within alphabet";
throwstd::runtime_error(ss.str());
}
returnstatic_cast<uint32_t>(index);
}
Personally, I prefer option 3, as it provides the most comprehensive information, which can be extremely helpful for debugging.
I am less inclined towards option 2 because it introduces a function argument solely for error handling, which might feel unnecessary unless it's strictly needed.
Originally posted by @hanusek in #248 (comment)
Ideally the error messages would include a position and element that is invalid. Currently the messages are tok generic
The text was updated successfully, but these errors were encountered: