#include "alternative_registry.h" #include #include namespace alternatives { namespace { bool letter(unsigned char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } bool digit(unsigned char c) { return c >= '0' && c <= '9'; } bool path_character(unsigned char c) { return letter(c) || digit(c) || c == '_' || c == '.' || c == '+' || c == '-'; } } bool valid_id(std::string_view value) { if (value.empty() || value.size() > 32) { return false; } for (unsigned char c : value) { if (!letter(c) && !digit(c) && c != '_' && c != '-') { return false; } } return true; } bool valid_path(std::string_view value) { if (value.size() < 2 || value.size() > 128 || value.front() != '/') { return false; } std::size_t begin = 1; while (begin < value.size()) { std::size_t end = value.find('/', begin); if (end == std::string_view::npos) { end = value.size(); } if (end == begin) { return false; } const auto component = value.substr(begin, end - begin); if (component == "." || component == "..") { return false; } for (unsigned char c : component) { if (!path_character(c)) { return false; } } if (end == value.size()) { return true; } begin = end + 1; } return false; } Error parse_priority(std::string_view text, std::int32_t& output) { output = 0; if (text.empty() || text.size() > 11) { return Error::invalid; } std::size_t at = 0; bool negative = false; if (text.front() == '+' || text.front() == '-') { negative = text.front() == '-'; ++at; } if (at == text.size()) { return Error::invalid; } const std::uint64_t maximum = negative ? 2147483648ULL : 2147483647ULL; std::uint64_t magnitude = 0; for (; at < text.size(); ++at) { const unsigned char c = static_cast(text[at]); if (!digit(c)) { return Error::invalid; } const unsigned value = static_cast(c - '0'); if (magnitude > (maximum - value) / 10) { return Error::invalid; } magnitude = magnitude * 10 + value; } if (negative && magnitude == 2147483648ULL) { output = std::numeric_limits::min(); } else { output = negative ? -static_cast(magnitude) : static_cast(magnitude); } return Error::none; } } // namespace alternatives