#include "unit_specifiers.h" #include #include #include #include #include #include #include #include using namespace unit_specifiers; namespace { std::string word(std::istringstream& row) { std::string value; if (!(row >> value)) throw std::runtime_error("missing field"); return value; } void end(std::istringstream& row) { std::string value; if (row >> value) throw std::runtime_error("extra field"); } unsigned nibble(char c) { if (c >= '0' && c <= '9') return static_cast(c - '0'); if (c >= 'a' && c <= 'f') return static_cast(c - 'a' + 10); if (c >= 'A' && c <= 'F') return static_cast(c - 'A' + 10); throw std::runtime_error("hex digit"); } std::string bytes(std::istringstream& row) { const std::string text = word(row); if (text == "-") return {}; if (text.size() % 2 != 0) throw std::runtime_error("hex length"); std::string value; value.reserve(text.size() / 2); for (std::size_t i = 0; i < text.size(); i += 2) { const unsigned byte = nibble(text[i]) * 16 + nibble(text[i + 1]); value.push_back(static_cast(byte)); } return value; } std::string hex(std::string_view value) { static constexpr char digits[] = "0123456789abcdef"; std::string result; result.reserve(value.size() * 2); for (unsigned char byte : value) { result.push_back(digits[byte >> 4]); result.push_back(digits[byte & 15]); } return result; } int integer(std::istringstream& row) { const std::string text = word(row); int value = 0; const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value); if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size()) throw std::runtime_error("int32"); return value; } std::size_t size(std::istringstream& row) { const std::string text = word(row); std::uint64_t value = 0; const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value); if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size() || value > std::numeric_limits::max()) throw std::runtime_error("size_t"); return static_cast(value); } unsigned slot(std::istringstream& row) { const std::size_t value = size(row); if (value > 1) throw std::runtime_error("slot"); return static_cast(value); } void string_value(std::string_view value) { std::cout << '"' << hex(value) << '"'; } void boolean(bool value) { std::cout << (value ? "true" : "false"); } void view_value(const StreamView& value) { std::cout << '[' << static_cast(value.mode) << ',' << static_cast(value.phase) << ',' << value.capacity << ',' << value.input_bytes << ',' << value.utf8_pending << ',' << value.escape_pending << ','; string_value(value.result); std::cout << ']'; } void service_value(const ServiceName& value) { std::cout << '['; string_value(value.unit); std::cout << ','; string_value(value.prefix); std::cout << ','; string_value(value.instance); std::cout << ','; string_value(value.decoded_prefix); std::cout << ','; string_value(value.decoded_instance); std::cout << ','; boolean(value.instantiated); std::cout << ']'; } void program_value(const Program& value) { std::cout << '['; bool separator = false; for (const Piece& piece : value.pieces) { if (separator) std::cout << ','; separator = true; std::cout << '[' << static_cast(piece.kind) << ','; string_value(piece.text); std::cout << ']'; } std::cout << ']'; } bool same(const Program& left, const Program& right) { if (left.pieces.size() != right.pieces.size()) return false; for (std::size_t i = 0; i < left.pieces.size(); ++i) { if (left.pieces[i].kind != right.pieces[i].kind || left.pieces[i].text != right.pieces[i].text) return false; } return true; } ServiceName dirty_service(const std::string& nonce) { return {nonce, nonce, nonce, nonce, nonce, true}; } void text_result(Error error, const std::string& output, bool unchanged) { std::cout << '[' << static_cast(error) << ','; string_value(output); std::cout << ','; boolean(unchanged); std::cout << ']'; } } // namespace int main() { Stream streams[2]; StreamView held_view; ServiceName held_service; Program held_program; std::string held_result; std::string line; try { while (std::getline(std::cin, line)) { std::istringstream row(line); std::string command; if (!(row >> command) || command.front() == '#') continue; if (command == "V") { const std::string text = bytes(row); end(row); const std::string before = text; const bool result = valid_utf8(text); std::cout << '['; boolean(result); std::cout << ','; boolean(text == before); std::cout << ']'; } else if (command == "E" || command == "U") { const std::string text = bytes(row); std::string output = bytes(row); end(row); const std::string before = text; const Error error = command == "E" ? escape(text, output) : unescape(text, output); held_result = output; text_result(error, output, text == before); } else if (command == "N") { const unsigned index = slot(row); const Mode mode = static_cast(integer(row)); const std::size_t capacity = size(row); end(row); const Error error = streams[index].reset(mode, capacity); std::cout << '[' << static_cast(error) << ','; view_value(streams[index].view()); std::cout << ']'; } else if (command == "F") { const unsigned index = slot(row); const std::string chunk = bytes(row); end(row); const std::string before = chunk; const Error error = streams[index].feed(chunk); std::cout << '[' << static_cast(error) << ','; view_value(streams[index].view()); std::cout << ','; boolean(chunk == before); std::cout << ']'; } else if (command == "D") { const unsigned index = slot(row); end(row); const Error error = streams[index].finish(); std::cout << '[' << static_cast(error) << ','; view_value(streams[index].view()); std::cout << ']'; } else if (command == "Q") { const unsigned index = slot(row); end(row); held_view = streams[index].view(); view_value(held_view); } else if (command == "Y") { const unsigned index = slot(row); const std::string nonce = bytes(row); end(row); StreamView copied = streams[index].view(); copied.mode = static_cast(17); copied.phase = static_cast(18); copied.capacity = 19; copied.input_bytes = 20; copied.utf8_pending = 21; copied.escape_pending = 22; copied.result = nonce; std::cout << '['; view_value(copied); std::cout << ','; view_value(streams[index].view()); std::cout << ']'; } else if (command == "P") { const std::string unit = bytes(row); const std::string nonce = bytes(row); end(row); const std::string before = unit; ServiceName output = dirty_service(nonce); const Error error = parse_unit(unit, output); held_service = output; std::cout << '[' << static_cast(error) << ','; service_value(output); std::cout << ','; boolean(unit == before); std::cout << ']'; } else if (command == "C") { const std::string text = bytes(row); const std::string nonce = bytes(row); end(row); const std::string before = text; Program output; output.pieces.push_back({PieceKind::decoded_instance, nonce}); const Error error = compile_template(text, output); held_program = output; std::cout << '[' << static_cast(error) << ','; program_value(output); std::cout << ','; boolean(text == before); std::cout << ']'; } else if (command == "R") { const std::size_t count = size(row); if (count > 8192) throw std::runtime_error("wire count"); Program program; for (std::size_t i = 0; i < count; ++i) { const PieceKind kind = static_cast(integer(row)); program.pieces.push_back({kind, bytes(row)}); } const std::string unit = bytes(row); const std::size_t capacity = size(row); std::string output = bytes(row); end(row); const Program before = program; const std::string unit_before = unit; const Error error = render(program, unit, capacity, output); held_result = output; text_result(error, output, unit == unit_before && same(program, before)); } else if (command == "T") { const std::string unit = bytes(row); const std::size_t capacity = size(row); std::string output = bytes(row); end(row); const Program before = held_program; const std::string unit_before = unit; const Error error = render(held_program, unit, capacity, output); held_result = output; text_result(error, output, unit == unit_before && same(held_program, before)); } else if (command == "X") { const std::string text = bytes(row); const std::string unit = bytes(row); const std::size_t capacity = size(row); std::string output = bytes(row); end(row); const std::string text_before = text; const std::string unit_before = unit; const Error error = expand(text, unit, capacity, output); held_result = output; text_result(error, output, text == text_before && unit == unit_before); } else if (command == "H") { end(row); std::cout << '['; view_value(held_view); std::cout << ','; service_value(held_service); std::cout << ','; program_value(held_program); std::cout << ','; string_value(held_result); std::cout << ']'; } else { throw std::runtime_error("command"); } std::cout << '\n'; } } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 2; } return std::cin.bad() || !std::cout ? 2 : 0; }