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