#include "module_plan.h" #include #include namespace bootplan { namespace { bool letter(unsigned char value) { return (value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z'); } bool digit(unsigned char value) { return value >= '0' && value <= '9'; } bool space(unsigned char value) { switch (value) { case ' ': case '\t': case '\r': case '\v': case '\f': return true; default: return false; } } bool valid_text(const std::string &text) { if (text.size() > 65536) { return false; } for (unsigned char value : text) { if (value > 127) { return false; } } return true; } void finish_line(const std::string &line, std::vector> &result) { std::size_t first = 0; while (first < line.size() && space(line[first])) { ++first; } if (first == line.size() || line[first] == '#') { return; } std::istringstream stream(line.substr(first)); stream.imbue(std::locale::classic()); std::vector tokens; std::string token; while (stream >> token) { tokens.push_back(token); } if (!tokens.empty()) { result.emplace_back(std::move(tokens)); } } } bool canonical_module(const std::string &raw, std::string &output) { output.clear(); if (raw.empty() || raw.size() > 64) { return false; } std::string candidate; candidate.reserve(raw.size()); for (unsigned char value : raw) { if (!(letter(value) || digit(value) || value == '_' || value == '-')) { return false; } candidate.push_back(value == '-' ? '_' : static_cast(value)); } output = std::move(candidate); return true; } Error statements(const std::string &text, std::vector> &output) { output.clear(); if (!valid_text(text)) { return Error::invalid; } std::vector> parsed; std::string logical; std::size_t position = 0; bool continued = false; for (;;) { const std::size_t newline = text.find('\n', position); const bool final = newline == std::string::npos; const std::size_t end = final ? text.size() : newline; const std::string physical = text.substr(position, end - position); if (!physical.empty() && physical.back() == '\\') { logical.append(physical, 0, physical.size() - 1); logical.push_back(' '); continued = true; } else { logical += physical; finish_line(logical, parsed); logical.clear(); continued = false; } if (final) { break; } position = end + 1; } if (continued) { return Error::invalid; } output = std::move(parsed); return Error::none; } }