#include "module_plan.h" #include namespace bootplan { namespace { bool module_byte(unsigned char byte) { return (byte >= 'A' && byte <= 'Z') || (byte >= 'a' && byte <= 'z') || (byte >= '0' && byte <= '9') || byte == '_' || byte == '-'; } bool space_byte(unsigned char byte) { return byte == ' ' || byte == '\t' || byte == '\r' || byte == '\v' || byte == '\f'; } bool finish_line(const std::string &line, std::vector> &rows) { std::size_t at = 0; while (at < line.size() && space_byte(static_cast(line[at]))) ++at; if (at == line.size() || line[at] == '#') return true; std::vector row; while (at < line.size()) { std::size_t start = at; while (at < line.size() && !space_byte(static_cast(line[at]))) { ++at; } row.emplace_back(line.substr(start, at - start)); while (at < line.size() && space_byte(static_cast(line[at]))) ++at; } rows.push_back(std::move(row)); return true; } } bool canonical_module(const std::string &raw, std::string &output) { // Build before publishing, including when the caller aliases input and output. std::string next; if (raw.empty() || raw.size() > 64) { output.clear(); return false; } next.reserve(raw.size()); for (unsigned char byte : raw) { if (!module_byte(byte)) { output.clear(); return false; } next.push_back(byte == '-' ? '_' : static_cast(byte)); } output = std::move(next); return true; } Error statements(const std::string &text, std::vector> &output) { output.clear(); if (text.size() > 65536) return Error::invalid; for (unsigned char byte : text) if (byte > 127) return Error::invalid; std::vector> next; std::string logical; std::size_t start = 0; for (;;) { const std::size_t lf = text.find('\n', start); const bool has_next = lf != std::string::npos; const std::size_t end = has_next ? lf : text.size(); const bool continued = end > start && text[end - 1] == '\\'; logical.append(text, start, end - start - (continued ? 1 : 0)); if (continued) { if (!has_next) return Error::invalid; logical.push_back(' '); } else { if (!finish_line(logical, next)) return Error::invalid; logical.clear(); } if (!has_next) break; start = lf + 1; } output = std::move(next); return Error::none; } }