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>
93 lines
3.4 KiB
C++
93 lines
3.4 KiB
C++
#include "unit_specifiers.h"
|
|
#include <utility>
|
|
|
|
namespace unit_specifiers {
|
|
namespace {
|
|
bool instruction(char code, PieceKind& kind) {
|
|
switch (code) {
|
|
case 'n': kind = PieceKind::entire_unit; return true;
|
|
case 'p': kind = PieceKind::prefix; return true;
|
|
case 'i': kind = PieceKind::instance; return true;
|
|
case 'I': kind = PieceKind::decoded_instance; return true;
|
|
case '%': kind = PieceKind::percent; return true;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
bool valid_program(const Program& program) {
|
|
if (program.pieces.size() > 4096) return false;
|
|
std::size_t literal_bytes = 0;
|
|
for (const Piece& piece : program.pieces) {
|
|
const int kind = static_cast<int>(piece.kind);
|
|
if (kind < 0 || kind > 5) return false;
|
|
if (piece.kind == PieceKind::literal) {
|
|
if (piece.text.size() > 4096 - literal_bytes || !valid_utf8(piece.text))
|
|
return false;
|
|
literal_bytes += piece.text.size();
|
|
} else if (!piece.text.empty()) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string_view replacement(PieceKind kind, const ServiceName& name) {
|
|
switch (kind) {
|
|
case PieceKind::entire_unit: return name.unit;
|
|
case PieceKind::prefix: return name.prefix;
|
|
case PieceKind::instance: return name.instance;
|
|
case PieceKind::decoded_instance: return name.decoded_instance;
|
|
case PieceKind::percent: return "%";
|
|
default: return {};
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
Error compile_template(std::string_view text, Program& output) {
|
|
output.pieces.clear();
|
|
if (text.size() > 4096 || !valid_utf8(text)) return Error::invalid;
|
|
Program compiled;
|
|
std::size_t position = 0;
|
|
while (position < text.size()) {
|
|
const std::size_t percent = text.find('%', position);
|
|
const std::size_t stop = percent == std::string_view::npos ? text.size() : percent;
|
|
if (stop > position)
|
|
compiled.pieces.push_back({PieceKind::literal, std::string(text.substr(position, stop - position))});
|
|
if (percent == std::string_view::npos) break;
|
|
if (percent + 1 == text.size()) return Error::invalid;
|
|
PieceKind kind;
|
|
if (!instruction(text[percent + 1], kind)) return Error::invalid;
|
|
compiled.pieces.push_back({kind, {}});
|
|
position = percent + 2;
|
|
}
|
|
output = std::move(compiled);
|
|
return Error::none;
|
|
}
|
|
|
|
Error render(const Program& program, std::string_view unit,
|
|
std::size_t capacity, std::string& output) {
|
|
output.clear();
|
|
if (capacity > 65536 || !valid_program(program)) return Error::invalid;
|
|
ServiceName name;
|
|
if (parse_unit(unit, name) != Error::none) return Error::invalid;
|
|
std::string result;
|
|
for (const Piece& piece : program.pieces) {
|
|
const std::string_view value = piece.kind == PieceKind::literal
|
|
? std::string_view(piece.text)
|
|
: replacement(piece.kind, name);
|
|
if (value.size() > capacity - result.size()) return Error::invalid;
|
|
if (!value.empty()) result.append(value.data(), value.size());
|
|
}
|
|
output = std::move(result);
|
|
return Error::none;
|
|
}
|
|
|
|
Error expand(std::string_view text, std::string_view unit,
|
|
std::size_t capacity, std::string& output) {
|
|
output.clear();
|
|
Program program;
|
|
if (compile_template(text, program) != Error::none) return Error::invalid;
|
|
return render(program, unit, capacity, output);
|
|
}
|
|
} // namespace unit_specifiers
|