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

122 lines
3.9 KiB
C++

#include "ratio.hpp"
#include <limits>
namespace ratio {
bool Parser::separator(unsigned char byte) {
return byte == ' ' || byte == '\t' || byte == '\r';
}
bool Parser::tokenize(const std::string& line, std::vector<std::string>& result) {
if (line.size() > 256) {
return false;
}
std::size_t cursor = 0;
while (cursor < line.size()) {
while (cursor < line.size() && separator(static_cast<unsigned char>(line[cursor]))) {
++cursor;
}
if (cursor == line.size()) {
break;
}
const std::size_t begin = cursor;
while (cursor < line.size() && !separator(static_cast<unsigned char>(line[cursor]))) {
const unsigned char byte = static_cast<unsigned char>(line[cursor]);
if (byte < 33 || byte > 126) {
return false;
}
++cursor;
}
result.push_back(line.substr(begin, cursor - begin));
if (result.size() > 6) {
return false;
}
}
return true;
}
bool Parser::integer(const std::string& token, std::int64_t& value) {
if (token.empty()) {
return false;
}
std::size_t cursor = 0;
bool negative = false;
if (token[cursor] == '+' || token[cursor] == '-') {
negative = token[cursor] == '-';
++cursor;
}
if (cursor == token.size()) {
return false;
}
const std::uint64_t positive = static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());
const std::uint64_t bound = positive + (negative ? 1u : 0u);
std::uint64_t number = 0;
for (; cursor < token.size(); ++cursor) {
const unsigned char byte = static_cast<unsigned char>(token[cursor]);
if (byte < '0' || byte > '9') {
return false;
}
const std::uint64_t digit = byte - '0';
if (number > (bound - digit) / 10) {
return false;
}
number = number * 10 + digit;
}
if (negative && number == positive + 1) {
value = std::numeric_limits<std::int64_t>::min();
} else {
value = static_cast<std::int64_t>(number);
if (negative) {
value = -value;
}
}
return true;
}
bool Parser::slot_number(const std::string& token, int& value) {
std::int64_t wide = 0;
if (!integer(token, wide)) {
return false;
}
if (wide < std::numeric_limits<int>::min() || wide > std::numeric_limits<int>::max()) {
return false;
}
value = static_cast<int>(wide);
return true;
}
Command Parser::parse(const std::string& line) const {
Command response;
std::vector<std::string> tokens;
if (!tokenize(line, tokens)) {
return response;
}
if (tokens.empty()) {
response.verb = Verb::empty;
return response;
}
if (tokens[0] == "TOTAL" && tokens.size() == 1) {
response.verb = Verb::totals;
return response;
}
if (tokens.size() < 2 || !slot_number(tokens[1], response.slot)) {
return response;
}
if (tokens[0] == "CONFIG" && tokens.size() == 6) {
if (!integer(tokens[2], response.profile.raw_low) ||
!integer(tokens[3], response.profile.raw_high) ||
!integer(tokens[4], response.profile.out_low) ||
!integer(tokens[5], response.profile.out_high)) {
return response;
}
response.verb = Verb::configure;
} else if (tokens[0] == "FEED" && tokens.size() == 4) {
if (!integer(tokens[2], response.raw) || !integer(tokens[3], response.weight)) {
return response;
}
response.verb = Verb::feed;
} else if (tokens[0] == "SNAP" && tokens.size() == 2) {
response.verb = Verb::snapshot;
} else if (tokens[0] == "CLEAR" && tokens.size() == 2) {
response.verb = Verb::clear;
} else if (tokens[0] == "DROP" && tokens.size() == 2) {
response.verb = Verb::drop;
}
return response;
}
}