amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

260 lines
7.7 KiB
C++

#include "route.hpp"
#include <array>
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
namespace route {
namespace {
bool horizontal(char ch) { return ch == ' ' || ch == '\t'; }
bool utf8_scalar_sequence(const std::string& text) {
// The description is a text file, not an opaque byte stream. Reject
// overlong encodings, surrogate scalars, and values above U+10ffff.
std::size_t offset = 0;
while (offset < text.size()) {
const unsigned char first = static_cast<unsigned char>(text[offset]);
if (first < 0x80) {
++offset;
continue;
}
unsigned length = 0;
unsigned scalar = 0;
unsigned minimum = 0;
if (first >= 0xc2 && first <= 0xdf) {
length = 2;
scalar = first & 0x1f;
minimum = 0x80;
} else if (first >= 0xe0 && first <= 0xef) {
length = 3;
scalar = first & 0x0f;
minimum = 0x800;
} else if (first >= 0xf0 && first <= 0xf4) {
length = 4;
scalar = first & 0x07;
minimum = 0x10000;
} else {
return false;
}
if (offset + length > text.size()) return false;
for (unsigned i = 1; i < length; ++i) {
const unsigned char byte = static_cast<unsigned char>(text[offset + i]);
if (byte < 0x80 || byte > 0xbf) return false;
scalar = (scalar << 6) | (byte & 0x3f);
}
if (scalar < minimum || scalar > 0x10ffff ||
(scalar >= 0xd800 && scalar <= 0xdfff)) return false;
offset += length;
}
return true;
}
class LineReader {
public:
explicit LineReader(const std::string& line) : line_(line) {}
void spaces() {
while (position_ < line_.size() && horizontal(line_[position_])) {
++position_;
}
}
bool word(std::string& result) {
spaces();
const std::size_t first = position_;
while (position_ < line_.size()) {
const char ch = line_[position_];
if (!((ch >= 'a' && ch <= 'z') || ch == '_')) break;
++position_;
}
if (position_ == first) return false;
result.assign(line_, first, position_ - first);
return true;
}
bool equals() {
spaces();
if (position_ == line_.size() || line_[position_] != '=') return false;
++position_;
return true;
}
bool boolean(bool& value) {
spaces();
const std::size_t first = position_;
while (position_ < line_.size() &&
line_[position_] >= 'a' && line_[position_] <= 'z') {
++position_;
}
const std::string token = line_.substr(first, position_ - first);
if (token == "true") value = true;
else if (token == "false") value = false;
else return false;
return true;
}
bool quoted(std::string& value) {
spaces();
if (position_ >= line_.size() || line_[position_] != '"') return false;
++position_;
std::string parsed;
while (position_ < line_.size()) {
char ch = line_[position_++];
if (ch == '"') {
if (parsed.empty()) return false;
value = std::move(parsed);
return true;
}
if (ch == '\\') {
if (position_ >= line_.size()) return false;
ch = line_[position_++];
if (ch != '\\' && ch != '"') return false;
}
if (ch == '\n' || ch == '\r' || ch == '\0') return false;
parsed.push_back(ch);
}
return false;
}
bool quoted_list(std::vector<std::string>& values) {
spaces();
while (position_ < line_.size() && line_[position_] == '"') {
std::string value;
if (!quoted(value)) return false;
values.push_back(std::move(value));
if (position_ < line_.size() && !horizontal(line_[position_]) &&
line_[position_] != '#') return false;
spaces();
}
return true;
}
bool end() {
spaces();
return position_ == line_.size() || line_[position_] == '#';
}
private:
const std::string& line_;
std::size_t position_ = 0;
};
int key_index(const std::string& key) {
static const std::array<std::string, 5> names = {
"cross", "host_machine", "target_machine", "exe_wrapper", "runtime_root"};
for (std::size_t i = 0; i < names.size(); ++i) {
if (key == names[i]) return static_cast<int>(i);
}
return -1;
}
bool parse_line(const std::string& line, std::array<bool, 5>& seen,
Config& pending, std::string& error) {
LineReader reader(line);
if (reader.end()) return true;
std::string key;
if (!reader.word(key) || !reader.equals()) {
error = "assignment";
return false;
}
const int index = key_index(key);
if (index < 0) {
error = "unknown key";
return false;
}
if (seen[static_cast<std::size_t>(index)]) {
error = "duplicate key";
return false;
}
if (index == 0) {
if (!reader.boolean(pending.cross)) {
error = "boolean";
return false;
}
} else {
std::vector<std::string> values;
if (!reader.quoted_list(values)) {
error = "quoted value";
return false;
}
if (index == 3) pending.exe_wrapper = std::move(values);
else {
if (values.size() != 1) {
error = "one value required";
return false;
}
if (index == 1) pending.host_machine = std::move(values.front());
if (index == 2) pending.target_machine = std::move(values.front());
if (index == 4) pending.runtime_root = std::move(values.front());
}
}
if (!reader.end()) {
error = "trailing text";
return false;
}
seen[static_cast<std::size_t>(index)] = true;
return true;
}
bool validate(const Config& config, std::string& error) {
if (config.host_machine.empty() || config.target_machine.empty()) {
error = "machine";
return false;
}
if (config.cross) {
if (config.host_machine == config.target_machine) {
error = "cross machine";
return false;
}
if (config.exe_wrapper.empty() || config.runtime_root.empty()) {
error = "wrapper or runtime root";
return false;
}
} else if (config.host_machine != config.target_machine) {
error = "native machine";
return false;
}
return true;
}
} // namespace
bool parse_config(const std::string& text, Config& output, std::string& error) {
if (!utf8_scalar_sequence(text)) {
error = "utf8";
return false;
}
Config pending;
std::array<bool, 5> seen{};
std::size_t start = 0;
std::size_t line_number = 0;
do {
const std::size_t end = text.find('\n', start);
std::string line = text.substr(start, end == std::string::npos
? std::string::npos : end - start);
if (!line.empty() && line.back() == '\r') line.pop_back();
++line_number;
if (!parse_line(line, seen, pending, error)) {
error = "line " + std::to_string(line_number) + ": " + error;
return false;
}
if (end == std::string::npos) break;
start = end + 1;
} while (true);
for (bool present : seen) {
if (!present) {
error = "missing key";
return false;
}
}
if (!validate(pending, error)) return false;
output = std::move(pending);
error.clear();
return true;
}
} // namespace route