All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
5.2 KiB
C++
170 lines
5.2 KiB
C++
#include "unit_specifiers.h"
|
|
#include <utility>
|
|
|
|
namespace unit_specifiers {
|
|
namespace detail {
|
|
bool take_utf8(unsigned char, unsigned&, unsigned&, unsigned&, unsigned&);
|
|
}
|
|
|
|
namespace {
|
|
bool safe(unsigned char byte) {
|
|
return (byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') ||
|
|
(byte >= '0' && byte <= '9') || byte == '_' || byte == ':' || byte == '.';
|
|
}
|
|
|
|
int hex_value(unsigned char byte) {
|
|
if (byte >= '0' && byte <= '9') return byte - '0';
|
|
if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10;
|
|
if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10;
|
|
return -1;
|
|
}
|
|
} // namespace
|
|
|
|
struct Stream::Impl {
|
|
Mode mode = Mode::escape;
|
|
Phase phase = Phase::open;
|
|
std::size_t capacity = 16384;
|
|
std::size_t input_bytes = 0;
|
|
unsigned remaining = 0;
|
|
unsigned received = 0;
|
|
unsigned lower = 0x80;
|
|
unsigned upper = 0xbf;
|
|
unsigned escape_pending = 0;
|
|
unsigned hex_byte = 0;
|
|
std::string converted;
|
|
|
|
void clear_progress() {
|
|
input_bytes = 0;
|
|
remaining = 0;
|
|
received = 0;
|
|
lower = 0x80;
|
|
upper = 0xbf;
|
|
escape_pending = 0;
|
|
hex_byte = 0;
|
|
converted.clear();
|
|
}
|
|
|
|
Error fail() {
|
|
phase = Phase::invalid;
|
|
clear_progress();
|
|
return Error::invalid;
|
|
}
|
|
|
|
bool decoded_byte(unsigned char byte) {
|
|
if (!detail::take_utf8(byte, remaining, received, lower, upper)) return false;
|
|
converted.push_back(static_cast<char>(byte));
|
|
return true;
|
|
}
|
|
|
|
bool encoded_byte(unsigned char byte) {
|
|
if (!detail::take_utf8(byte, remaining, received, lower, upper)) return false;
|
|
if (safe(byte) && !(input_bytes == 0 && byte == '.')) {
|
|
converted.push_back(static_cast<char>(byte));
|
|
} else {
|
|
static constexpr char digits[] = "0123456789abcdef";
|
|
converted.push_back('\\');
|
|
converted.push_back('x');
|
|
converted.push_back(digits[byte >> 4]);
|
|
converted.push_back(digits[byte & 15]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool unescape_byte(unsigned char byte) {
|
|
if (escape_pending == 0) {
|
|
if (safe(byte)) return decoded_byte(byte);
|
|
if (byte == '\\') {
|
|
escape_pending = 1;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
if (escape_pending == 1) {
|
|
if (byte != 'x') return false;
|
|
escape_pending = 2;
|
|
return true;
|
|
}
|
|
const int nibble = hex_value(byte);
|
|
if (nibble < 0) return false;
|
|
if (escape_pending == 2) {
|
|
hex_byte = static_cast<unsigned>(nibble) << 4;
|
|
escape_pending = 3;
|
|
return true;
|
|
}
|
|
const unsigned value = hex_byte | static_cast<unsigned>(nibble);
|
|
escape_pending = 0;
|
|
hex_byte = 0;
|
|
return decoded_byte(static_cast<unsigned char>(value));
|
|
}
|
|
};
|
|
|
|
Stream::Stream() : impl_(std::make_unique<Impl>()) {}
|
|
Stream::~Stream() = default;
|
|
|
|
Error Stream::reset(Mode mode, std::size_t capacity) {
|
|
if ((mode != Mode::escape && mode != Mode::unescape) || capacity > 16384)
|
|
return Error::invalid;
|
|
impl_->clear_progress();
|
|
impl_->mode = mode;
|
|
impl_->capacity = capacity;
|
|
impl_->phase = Phase::open;
|
|
return Error::none;
|
|
}
|
|
|
|
Error Stream::feed(std::string_view chunk) {
|
|
if (impl_->phase != Phase::open) return Error::invalid;
|
|
const std::size_t limit = impl_->mode == Mode::escape ? 4096 : 16384;
|
|
if (chunk.size() > limit - impl_->input_bytes) return impl_->fail();
|
|
for (unsigned char byte : chunk) {
|
|
const bool okay = impl_->mode == Mode::escape ? impl_->encoded_byte(byte)
|
|
: impl_->unescape_byte(byte);
|
|
if (!okay) return impl_->fail();
|
|
++impl_->input_bytes;
|
|
}
|
|
return Error::none;
|
|
}
|
|
|
|
Error Stream::finish() {
|
|
if (impl_->phase == Phase::finished) return Error::none;
|
|
if (impl_->phase == Phase::invalid) return Error::invalid;
|
|
if (impl_->remaining != 0 || impl_->escape_pending != 0 ||
|
|
impl_->converted.size() > impl_->capacity) return impl_->fail();
|
|
impl_->phase = Phase::finished;
|
|
return Error::none;
|
|
}
|
|
|
|
StreamView Stream::view() const {
|
|
StreamView output;
|
|
output.mode = impl_->mode;
|
|
output.phase = impl_->phase;
|
|
output.capacity = impl_->capacity;
|
|
output.input_bytes = impl_->input_bytes;
|
|
output.utf8_pending = impl_->received;
|
|
output.escape_pending = impl_->escape_pending;
|
|
if (impl_->phase == Phase::finished) output.result = impl_->converted;
|
|
return output;
|
|
}
|
|
|
|
namespace {
|
|
Error transform(std::string_view input, Mode mode, std::string& output) {
|
|
output.clear();
|
|
Stream stream;
|
|
if (stream.reset(mode, 16384) != Error::none ||
|
|
stream.feed(input) != Error::none || stream.finish() != Error::none)
|
|
return Error::invalid;
|
|
output = stream.view().result;
|
|
return Error::none;
|
|
}
|
|
} // namespace
|
|
|
|
Error escape(std::string_view text, std::string& output) {
|
|
output.assign(text.data(), text.size());
|
|
return Error::none;
|
|
}
|
|
|
|
Error unescape(std::string_view encoded, std::string& output) {
|
|
output.assign(encoded.data(), encoded.size());
|
|
return Error::none;
|
|
}
|
|
} // namespace unit_specifiers
|