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

262 lines
8.6 KiB
C++

#include "param.h"
#include <algorithm>
#include <array>
#include <cerrno>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <limits>
#include <sstream>
#include <utility>
namespace param {
namespace {
bool first_name_byte(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
bool later_name_byte(char ch) {
return first_name_byte(ch) || (ch >= '0' && ch <= '9');
}
Value decimal_default(std::int64_t micros) {
Value result;
result.kind = Kind::Decimal;
result.decimal_micros = micros;
return result;
}
Value integer_default(int number) {
Value result;
result.kind = Kind::Integer;
result.integer = number;
return result;
}
Value boolean_default(bool flag) {
Value result;
result.kind = Kind::Boolean;
result.boolean = flag;
return result;
}
Value text_default(std::string text) {
Value result;
result.kind = Kind::Text;
result.text = std::move(text);
return result;
}
bool decimal_lexeme(std::string_view token) {
if (token.empty()) return false;
std::size_t index = 0;
if (token[index] == '+' || token[index] == '-') ++index;
bool digit_before = false;
while (index < token.size() && token[index] >= '0' && token[index] <= '9') {
digit_before = true;
++index;
}
bool digit_after = false;
if (index < token.size() && token[index] == '.') {
++index;
while (index < token.size() && token[index] >= '0' && token[index] <= '9') {
digit_after = true;
++index;
}
}
if (!digit_before && !digit_after) return false;
if (index < token.size() && (token[index] == 'e' || token[index] == 'E')) {
++index;
if (index < token.size() && (token[index] == '+' || token[index] == '-')) ++index;
const std::size_t exponent_start = index;
while (index < token.size() && token[index] >= '0' && token[index] <= '9') ++index;
if (exponent_start == index) return false;
}
return index == token.size();
}
std::optional<std::int64_t> decimal_to_micros(std::string_view token) {
if (!decimal_lexeme(token)) return std::nullopt;
std::size_t index = 0;
const bool negative = token[index] == '-';
if (token[index] == '-' || token[index] == '+') ++index;
if (negative) return std::nullopt;
std::string digits;
int fractional_places = 0;
bool after_point = false;
while (index < token.size() && token[index] != 'e' && token[index] != 'E') {
const char ch = token[index++];
if (ch == '.') {
after_point = true;
continue;
}
digits.push_back(ch);
if (after_point) ++fractional_places;
}
int exponent = 0;
bool exponent_negative = false;
if (index < token.size()) {
++index;
if (token[index] == '+' || token[index] == '-') {
exponent_negative = token[index] == '-';
++index;
}
while (index < token.size()) {
const int number = token[index++] - '0';
exponent = exponent > 10000 ? 10001 : exponent * 10 + number;
}
if (exponent_negative) exponent = -exponent;
}
const std::size_t first_nonzero = digits.find_first_not_of('0');
if (first_nonzero == std::string::npos) return std::nullopt;
digits.erase(0, first_nonzero);
const long long shift = static_cast<long long>(exponent) - fractional_places + 6;
if (shift >= 0) {
if (digits.size() + static_cast<std::size_t>(shift) > 9) return std::nullopt;
digits.append(static_cast<std::size_t>(shift), '0');
} else {
const auto cut = static_cast<std::size_t>(-shift);
if (cut >= digits.size()) return std::nullopt;
for (std::size_t cursor = digits.size() - cut; cursor < digits.size(); ++cursor) {
if (digits[cursor] != '0') return std::nullopt;
}
digits.resize(digits.size() - cut);
}
if (digits.size() < 7 || digits.size() > 9) return std::nullopt;
std::int64_t micros = 0;
for (char digit : digits) micros = micros * 10 + (digit - '0');
if (micros < 1000000 || micros > 240000000) return std::nullopt;
return micros;
}
std::optional<int> parse_retries(std::string_view token) {
if (token.empty()) return std::nullopt;
std::size_t index = 0;
if (token[0] == '+' || token[0] == '-') ++index;
if (index == token.size()) return std::nullopt;
int result = 0;
bool negative = token[0] == '-';
for (; index < token.size(); ++index) {
const char ch = token[index];
if (ch < '0' || ch > '9') return std::nullopt;
const int digit = ch - '0';
if (result > 8) return std::nullopt;
result = result * 10 + digit;
if (result > 8) return std::nullopt;
}
if (negative && result != 0) return std::nullopt;
return result;
}
std::string format_decimal(std::int64_t micros) {
const std::int64_t integer = micros / 1000000;
const std::int64_t fraction = micros % 1000000;
if (fraction == 0) return std::to_string(integer);
std::ostringstream stream;
stream << integer << '.' << std::setfill('0') << std::setw(6) << fraction;
std::string result = stream.str();
while (!result.empty() && result.back() == '0') result.pop_back();
return result;
}
} // namespace
bool valid_name(std::string_view name) {
if (name.empty() || name.size() > 32 || !first_name_byte(name[0])) return false;
for (std::size_t index = 1; index < name.size(); ++index) {
if (!later_name_byte(name[index])) return false;
}
return true;
}
const std::array<std::string_view, 4>& schema_order() {
static const std::array<std::string_view, 4> order = {"rate", "retries", "enabled", "mode"};
return order;
}
std::optional<Schema> find_schema(std::string_view name) {
if (!valid_name(name)) return std::nullopt;
if (name == "rate") return Schema{"rate", Kind::Decimal, decimal_default(30000000)};
if (name == "retries") return Schema{"retries", Kind::Integer, integer_default(3)};
if (name == "enabled") return Schema{"enabled", Kind::Boolean, boolean_default(true)};
if (name == "mode") return Schema{"mode", Kind::Text, text_default("auto")};
return std::nullopt;
}
std::optional<Value> parse_value(const Schema& schema, std::string_view token) {
Value result;
result.kind = schema.kind;
switch (schema.kind) {
case Kind::Decimal: {
const auto micros = decimal_to_micros(token);
if (!micros) return std::nullopt;
result.decimal_micros = *micros;
return result;
}
case Kind::Integer: {
const auto number = parse_retries(token);
if (!number) return std::nullopt;
result.integer = *number;
return result;
}
case Kind::Boolean:
if (token == "true") result.boolean = true;
else if (token == "false") result.boolean = false;
else return std::nullopt;
return result;
case Kind::Text:
if (token != "auto" && token != "manual" && token != "safe") return std::nullopt;
result.text = std::string(token);
return result;
}
return std::nullopt;
}
std::string format_value(const Value& value) {
switch (value.kind) {
case Kind::Decimal: return format_decimal(value.decimal_micros);
case Kind::Integer: return std::to_string(value.integer);
case Kind::Boolean: return value.boolean ? "true" : "false";
case Kind::Text: return value.text;
}
return {};
}
std::string kind_name(Kind kind) {
switch (kind) {
case Kind::Decimal: return "decimal";
case Kind::Integer: return "integer";
case Kind::Boolean: return "boolean";
case Kind::Text: return "text";
}
return {};
}
std::string error_name(Error error) {
switch (error) {
case Error::None: return {};
case Error::Unknown: return "unknown";
case Error::Undeclared: return "undeclared";
case Error::AlreadyDeclared: return "already_declared";
case Error::Value: return "value";
case Error::Syntax: return "syntax";
case Error::Mismatch: return "mismatch";
}
return "syntax";
}
bool equal_value(const Value& left, const Value& right) {
if (left.kind != right.kind) return false;
switch (left.kind) {
case Kind::Decimal: return left.decimal_micros == right.decimal_micros;
case Kind::Integer: return left.integer == right.integer;
case Kind::Boolean: return left.boolean == right.boolean;
case Kind::Text: return left.text == right.text;
}
return false;
}
} // namespace param