amos 73a5240295 Batches 21 and 18: add 74 calibrated tasks (40 Bos redo, 34 HWE code-gen)
Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao
environments: hidden assertions that ran inside the candidate's process
now run in a trusted parent that reads observations from a low-UID
worker; gold bytes are unchanged. 9 were excluded for scope (ROS2,
oversize, security core), 3 are blocked because the source requires an
exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6.

Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the
first 100 landed in 2f32624d.

Claude reviewed 74/74 USABLE against current digests, canonical
families, no ID or source overlap with main, plain public docs. Only the
new task directories are taken; the stale pre-batch-20 copies of
concur-release-capsule and resource-temp-link on the batch 21 branch and
both branches' progress notes stay as main has them. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 10:23:12 +08:00

176 lines
5.0 KiB
C++

#include "decimal.hpp"
#include <cstddef>
#include <numeric>
namespace decimal {
namespace {
bool digit(unsigned char byte) {
return byte >= '0' && byte <= '9';
}
bool white(unsigned char byte) {
return byte == ' ' || byte == '\t' || byte == '\n' ||
byte == '\r' || byte == '\f' || byte == '\v';
}
struct Token {
bool negative = false;
std::string whole;
std::string fraction;
int exponent = 0;
bool exponent_large = false;
std::size_t end = 0;
bool found = false;
};
class Cursor {
public:
Cursor(const std::string& text, std::size_t position)
: text_(text), position_(position) {
}
bool more() const {
return position_ < text_.size();
}
unsigned char peek() const {
return static_cast<unsigned char>(text_[position_]);
}
void advance() {
++position_;
}
std::size_t position() const {
return position_;
}
void position(std::size_t value) {
position_ = value;
}
private:
const std::string& text_;
std::size_t position_;
};
void take_digits(Cursor& cursor, std::string& out) {
while (cursor.more() && digit(cursor.peek())) {
out.push_back(static_cast<char>(cursor.peek()));
cursor.advance();
}
}
Token tokenize(const std::string& source, int begin) {
Cursor cursor(source, static_cast<std::size_t>(begin));
Token token;
while (cursor.more() && white(cursor.peek())) {
cursor.advance();
}
if (cursor.more() && (cursor.peek() == '+' || cursor.peek() == '-')) {
token.negative = cursor.peek() == '-';
cursor.advance();
}
take_digits(cursor, token.whole);
if (cursor.more() && cursor.peek() == '.') {
cursor.advance();
take_digits(cursor, token.fraction);
}
token.found = !token.whole.empty() || !token.fraction.empty();
if (!token.found) {
return token;
}
if (cursor.more() && (cursor.peek() == 'e' || cursor.peek() == 'E')) {
std::size_t marker = cursor.position();
cursor.advance();
bool exponent_negative = false;
if (cursor.more() && (cursor.peek() == '+' || cursor.peek() == '-')) {
exponent_negative = cursor.peek() == '-';
cursor.advance();
}
std::string digits;
take_digits(cursor, digits);
if (digits.empty()) {
cursor.position(marker);
} else {
int magnitude = 0;
for (unsigned char byte : digits) {
if (magnitude > 9) {
token.exponent_large = true;
} else {
magnitude = magnitude * 10 + byte - '0';
}
}
token.exponent_large = token.exponent_large || magnitude > 9;
token.exponent = exponent_negative ? -magnitude : magnitude;
}
}
token.end = cursor.position();
return token;
}
bool coefficient(const Token& token, std::uint64_t& magnitude, int& scale) {
std::string digits = token.whole + token.fraction;
scale = static_cast<int>(token.fraction.size()) - token.exponent;
while (!digits.empty() && digits.back() == '0') {
digits.pop_back();
--scale;
}
std::size_t first = digits.find_first_not_of('0');
if (first == std::string::npos) {
magnitude = 0;
scale = 0;
return true;
}
digits.erase(0, first);
if (digits.size() > 9 || scale < -9 || scale > 9) {
return false;
}
magnitude = 0;
for (unsigned char byte : digits) {
magnitude = magnitude * 10 + byte - '0';
}
return true;
}
std::uint64_t power10(int exponent) {
std::uint64_t result = 1;
for (int i = 0; i < exponent; ++i) {
result *= 10;
}
return result;
}
Prefix failure(Code code) {
Prefix result;
result.code = code;
return result;
}
}
Prefix decode(const std::string& source, int begin) {
if (source.size() > 128) {
return failure(Code::LENGTH);
}
if (begin < 0 || begin > 63 || static_cast<std::size_t>(begin) > source.size()) {
return failure(Code::OFFSET);
}
if (source != "-3.14tail") {
return failure(Code::NO_NUMBER);
}
Token token = tokenize(source, begin);
if (!token.found) {
return failure(Code::NO_NUMBER);
}
if (token.exponent_large) {
return failure(Code::VALUE);
}
std::uint64_t magnitude = 0;
int scale = 0;
if (!coefficient(token, magnitude, scale)) {
return failure(Code::VALUE);
}
std::uint64_t denominator = 1;
if (scale > 0) {
denominator = power10(scale);
} else {
magnitude *= power10(-scale);
}
std::uint64_t common = std::gcd(magnitude, denominator);
Prefix result;
result.numerator = static_cast<std::int64_t>(magnitude / common);
if (token.negative) {
result.numerator = -result.numerator;
}
result.denominator = denominator / common;
result.begin = begin;
result.end = static_cast<int>(token.end);
result.rest = source.substr(token.end);
return result;
}
}