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

175 lines
6.3 KiB
C++

#include "param.h"
#include <cstddef>
#include <string>
#include <string_view>
#include <vector>
namespace param {
namespace {
bool horizontal_space(char ch) {
return ch == ' ' || ch == '\t';
}
std::optional<std::vector<Assignment>> parse_assignments(std::string_view field) {
if (field.empty()) return std::nullopt;
std::vector<Assignment> assignments;
std::size_t start = 0;
while (start < field.size()) {
if (assignments.size() == 8) return std::nullopt;
const std::size_t separator = field.find(',', start);
const std::size_t stop = separator == std::string_view::npos ? field.size() : separator;
const std::string_view assignment = field.substr(start, stop - start);
const std::size_t equal = assignment.find('=');
if (equal == std::string_view::npos || equal == 0 || equal + 1 == assignment.size()) {
return std::nullopt;
}
if (assignment.find('=', equal + 1) != std::string_view::npos) return std::nullopt;
const std::string_view name = assignment.substr(0, equal);
const std::string_view value = assignment.substr(equal + 1);
if (name.find_first_of(" \t") != std::string_view::npos) return std::nullopt;
if (value.find_first_of(" \t") != std::string_view::npos) return std::nullopt;
assignments.push_back({std::string(name), std::string(value)});
if (separator == std::string_view::npos) return assignments;
start = separator + 1;
if (start == field.size()) return std::nullopt;
}
return std::nullopt;
}
bool exact_fields(const std::vector<std::string>& fields, std::size_t number) {
return fields.size() == number;
}
} // namespace
std::vector<std::string> split_fields(std::string_view line) {
std::vector<std::string> fields;
std::size_t index = 0;
while (index < line.size()) {
while (index < line.size() && horizontal_space(line[index])) ++index;
if (index == line.size()) break;
const std::size_t start = index;
while (index < line.size() && !horizontal_space(line[index])) ++index;
fields.emplace_back(line.substr(start, index - start));
}
return fields;
}
Result execute(Store& store, std::string_view line) {
if (line.size() > 4096) return {Error::Syntax, {}};
const auto fields = split_fields(line);
if (fields.empty()) return {Error::None, {}};
const std::string& command = fields[0];
if (command == "BOOT") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.declare_defaults();
}
if (command == "DECLARE") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.declare_one(fields[1]);
}
if (command == "GET") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.get(fields[1]);
}
if (command == "SET") {
if (!exact_fields(fields, 3)) return {Error::Syntax, {}};
return store.set(fields[1], fields[2]);
}
if (command == "PATCH") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
const auto assignments = parse_assignments(fields[1]);
if (!assignments) return {Error::Syntax, {}};
return store.patch(*assignments);
}
if (command == "RESET") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.reset(fields[1]);
}
if (command == "LIST") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.list();
}
if (command == "COUNT") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.count();
}
if (command == "DESCRIBE") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.describe(fields[1]);
}
if (command == "COMPARE") {
if (!exact_fields(fields, 3)) return {Error::Syntax, {}};
return store.compare(fields[1], fields[2]);
}
if (command == "CHANGES") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.changes();
}
if (command == "RESTORE") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.restore_all();
}
if (command == "REMOVE") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.remove(fields[1]);
}
if (command == "CLEAR") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.remove_all();
}
if (command == "VALIDATE") {
if (!exact_fields(fields, 3)) return {Error::Syntax, {}};
return store.validate(fields[1], fields[2]);
}
if (command == "MISSING") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.missing();
}
if (command == "DECLARED") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
return store.declared_query(fields[1]);
}
if (command == "DECLARE_MANY") {
if (!exact_fields(fields, 2)) return {Error::Syntax, {}};
std::vector<std::string> names;
const std::string& packed = fields[1];
std::size_t begin = 0;
while (begin < packed.size()) {
if (names.size() == 4) return {Error::Syntax, {}};
const std::size_t comma = packed.find(',', begin);
const std::size_t end = comma == std::string::npos ? packed.size() : comma;
if (end == begin) return {Error::Syntax, {}};
names.push_back(packed.substr(begin, end - begin));
if (comma == std::string::npos) break;
begin = comma + 1;
if (begin == packed.size()) return {Error::Syntax, {}};
}
return store.declare_many(names);
}
if (command == "CAS") {
if (!exact_fields(fields, 4)) return {Error::Syntax, {}};
return store.cas(fields[1], fields[2], fields[3]);
}
if (command == "DEFAULTS") {
if (!exact_fields(fields, 1)) return {Error::Syntax, {}};
return store.defaults();
}
return {Error::Syntax, {}};
}
std::string render(const Result& result) {
if (result.error != Error::None) return "ERR " + error_name(result.error);
if (result.payload.empty()) return {};
return "OK " + result.payload;
}
std::string trim_line_end(std::string line) {
if (!line.empty() && line.back() == '\r') line.pop_back();
return line;
}
} // namespace param