{ "unit_specifiers.h": "#ifndef UNIT_SPECIFIERS_H\n#define UNIT_SPECIFIERS_H\n#include \n#include \n#include \n#include \n#include \n\nnamespace unit_specifiers {\nenum class Error { none = 0, invalid = 1 };\nenum class Mode { escape = 0, unescape = 1 };\nenum class Phase { open = 0, invalid = 1, finished = 2 };\nenum class PieceKind {\n literal = 0,\n entire_unit = 1,\n prefix = 2,\n instance = 3,\n decoded_instance = 4,\n percent = 5\n};\n\nstruct ServiceName {\n std::string unit;\n std::string prefix;\n std::string instance;\n std::string decoded_prefix;\n std::string decoded_instance;\n bool instantiated = false;\n};\n\nstruct Piece {\n PieceKind kind = PieceKind::literal;\n std::string text;\n};\n\nstruct Program {\n std::vector pieces;\n};\n\nstruct StreamView {\n Mode mode = Mode::escape;\n Phase phase = Phase::open;\n std::size_t capacity = 16384;\n std::size_t input_bytes = 0;\n std::size_t utf8_pending = 0;\n std::size_t escape_pending = 0;\n std::string result;\n};\n\n// UTF-8 validity has no implicit locale or file-system dependency.\nbool valid_utf8(std::string_view bytes);\nError escape(std::string_view text, std::string& output);\nError unescape(std::string_view encoded, std::string& output);\nError parse_unit(std::string_view unit, ServiceName& output);\nError compile_template(std::string_view text, Program& output);\nError render(const Program& program, std::string_view unit,\n std::size_t capacity, std::string& output);\nError expand(std::string_view text, std::string_view unit,\n std::size_t capacity, std::string& output);\n\nclass Stream {\npublic:\n Stream();\n ~Stream();\n Stream(const Stream&) = delete;\n Stream& operator=(const Stream&) = delete;\n Stream(Stream&&) = delete;\n Stream& operator=(Stream&&) = delete;\n Error reset(Mode mode, std::size_t capacity);\n Error feed(std::string_view chunk);\n Error finish();\n StreamView view() const;\nprivate:\n struct Impl;\n std::unique_ptr impl_;\n};\n} // namespace unit_specifiers\n#endif\n", "unit_observe.cpp": "#include \"unit_specifiers.h\"\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\nusing namespace unit_specifiers;\n\nnamespace {\nstd::string word(std::istringstream& row) {\n std::string value;\n if (!(row >> value)) throw std::runtime_error(\"missing field\");\n return value;\n}\n\nvoid end(std::istringstream& row) {\n std::string value;\n if (row >> value) throw std::runtime_error(\"extra field\");\n}\n\nunsigned nibble(char c) {\n if (c >= '0' && c <= '9') return static_cast(c - '0');\n if (c >= 'a' && c <= 'f') return static_cast(c - 'a' + 10);\n if (c >= 'A' && c <= 'F') return static_cast(c - 'A' + 10);\n throw std::runtime_error(\"hex digit\");\n}\n\nstd::string bytes(std::istringstream& row) {\n const std::string text = word(row);\n if (text == \"-\") return {};\n if (text.size() % 2 != 0) throw std::runtime_error(\"hex length\");\n std::string value;\n value.reserve(text.size() / 2);\n for (std::size_t i = 0; i < text.size(); i += 2) {\n const unsigned byte = nibble(text[i]) * 16 + nibble(text[i + 1]);\n value.push_back(static_cast(byte));\n }\n return value;\n}\n\nstd::string hex(std::string_view value) {\n static constexpr char digits[] = \"0123456789abcdef\";\n std::string result;\n result.reserve(value.size() * 2);\n for (unsigned char byte : value) {\n result.push_back(digits[byte >> 4]);\n result.push_back(digits[byte & 15]);\n }\n return result;\n}\n\nint integer(std::istringstream& row) {\n const std::string text = word(row);\n int value = 0;\n const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);\n if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size())\n throw std::runtime_error(\"int32\");\n return value;\n}\n\nstd::size_t size(std::istringstream& row) {\n const std::string text = word(row);\n std::uint64_t value = 0;\n const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);\n if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size() ||\n value > std::numeric_limits::max())\n throw std::runtime_error(\"size_t\");\n return static_cast(value);\n}\n\nunsigned slot(std::istringstream& row) {\n const std::size_t value = size(row);\n if (value > 1) throw std::runtime_error(\"slot\");\n return static_cast(value);\n}\n\nvoid string_value(std::string_view value) {\n std::cout << '\"' << hex(value) << '\"';\n}\n\nvoid boolean(bool value) {\n std::cout << (value ? \"true\" : \"false\");\n}\n\nvoid view_value(const StreamView& value) {\n std::cout << '[' << static_cast(value.mode) << ','\n << static_cast(value.phase) << ',' << value.capacity << ','\n << value.input_bytes << ',' << value.utf8_pending << ','\n << value.escape_pending << ',';\n string_value(value.result);\n std::cout << ']';\n}\n\nvoid service_value(const ServiceName& value) {\n std::cout << '[';\n string_value(value.unit);\n std::cout << ',';\n string_value(value.prefix);\n std::cout << ',';\n string_value(value.instance);\n std::cout << ',';\n string_value(value.decoded_prefix);\n std::cout << ',';\n string_value(value.decoded_instance);\n std::cout << ',';\n boolean(value.instantiated);\n std::cout << ']';\n}\n\nvoid program_value(const Program& value) {\n std::cout << '[';\n bool separator = false;\n for (const Piece& piece : value.pieces) {\n if (separator) std::cout << ',';\n separator = true;\n std::cout << '[' << static_cast(piece.kind) << ',';\n string_value(piece.text);\n std::cout << ']';\n }\n std::cout << ']';\n}\n\nbool same(const Program& left, const Program& right) {\n if (left.pieces.size() != right.pieces.size()) return false;\n for (std::size_t i = 0; i < left.pieces.size(); ++i) {\n if (left.pieces[i].kind != right.pieces[i].kind ||\n left.pieces[i].text != right.pieces[i].text) return false;\n }\n return true;\n}\n\nServiceName dirty_service(const std::string& nonce) {\n return {nonce, nonce, nonce, nonce, nonce, true};\n}\n\nvoid text_result(Error error, const std::string& output, bool unchanged) {\n std::cout << '[' << static_cast(error) << ',';\n string_value(output);\n std::cout << ',';\n boolean(unchanged);\n std::cout << ']';\n}\n} // namespace\n\nint main() {\n Stream streams[2];\n StreamView held_view;\n ServiceName held_service;\n Program held_program;\n std::string held_result;\n std::string line;\n try {\n while (std::getline(std::cin, line)) {\n std::istringstream row(line);\n std::string command;\n if (!(row >> command) || command.front() == '#') continue;\n if (command == \"V\") {\n const std::string text = bytes(row);\n end(row);\n const std::string before = text;\n const bool result = valid_utf8(text);\n std::cout << '[';\n boolean(result);\n std::cout << ',';\n boolean(text == before);\n std::cout << ']';\n } else if (command == \"E\" || command == \"U\") {\n const std::string text = bytes(row);\n std::string output = bytes(row);\n end(row);\n const std::string before = text;\n const Error error = command == \"E\" ? escape(text, output)\n : unescape(text, output);\n held_result = output;\n text_result(error, output, text == before);\n } else if (command == \"N\") {\n const unsigned index = slot(row);\n const Mode mode = static_cast(integer(row));\n const std::size_t capacity = size(row);\n end(row);\n const Error error = streams[index].reset(mode, capacity);\n std::cout << '[' << static_cast(error) << ',';\n view_value(streams[index].view());\n std::cout << ']';\n } else if (command == \"F\") {\n const unsigned index = slot(row);\n const std::string chunk = bytes(row);\n end(row);\n const std::string before = chunk;\n const Error error = streams[index].feed(chunk);\n std::cout << '[' << static_cast(error) << ',';\n view_value(streams[index].view());\n std::cout << ',';\n boolean(chunk == before);\n std::cout << ']';\n } else if (command == \"D\") {\n const unsigned index = slot(row);\n end(row);\n const Error error = streams[index].finish();\n std::cout << '[' << static_cast(error) << ',';\n view_value(streams[index].view());\n std::cout << ']';\n } else if (command == \"Q\") {\n const unsigned index = slot(row);\n end(row);\n held_view = streams[index].view();\n view_value(held_view);\n } else if (command == \"Y\") {\n const unsigned index = slot(row);\n const std::string nonce = bytes(row);\n end(row);\n StreamView copied = streams[index].view();\n copied.mode = static_cast(17);\n copied.phase = static_cast(18);\n copied.capacity = 19;\n copied.input_bytes = 20;\n copied.utf8_pending = 21;\n copied.escape_pending = 22;\n copied.result = nonce;\n std::cout << '[';\n view_value(copied);\n std::cout << ',';\n view_value(streams[index].view());\n std::cout << ']';\n } else if (command == \"P\") {\n const std::string unit = bytes(row);\n const std::string nonce = bytes(row);\n end(row);\n const std::string before = unit;\n ServiceName output = dirty_service(nonce);\n const Error error = parse_unit(unit, output);\n held_service = output;\n std::cout << '[' << static_cast(error) << ',';\n service_value(output);\n std::cout << ',';\n boolean(unit == before);\n std::cout << ']';\n } else if (command == \"C\") {\n const std::string text = bytes(row);\n const std::string nonce = bytes(row);\n end(row);\n const std::string before = text;\n Program output;\n output.pieces.push_back({PieceKind::decoded_instance, nonce});\n const Error error = compile_template(text, output);\n held_program = output;\n std::cout << '[' << static_cast(error) << ',';\n program_value(output);\n std::cout << ',';\n boolean(text == before);\n std::cout << ']';\n } else if (command == \"R\") {\n const std::size_t count = size(row);\n if (count > 8192) throw std::runtime_error(\"wire count\");\n Program program;\n for (std::size_t i = 0; i < count; ++i) {\n const PieceKind kind = static_cast(integer(row));\n program.pieces.push_back({kind, bytes(row)});\n }\n const std::string unit = bytes(row);\n const std::size_t capacity = size(row);\n std::string output = bytes(row);\n end(row);\n const Program before = program;\n const std::string unit_before = unit;\n const Error error = render(program, unit, capacity, output);\n held_result = output;\n text_result(error, output, unit == unit_before && same(program, before));\n } else if (command == \"T\") {\n const std::string unit = bytes(row);\n const std::size_t capacity = size(row);\n std::string output = bytes(row);\n end(row);\n const Program before = held_program;\n const std::string unit_before = unit;\n const Error error = render(held_program, unit, capacity, output);\n held_result = output;\n text_result(error, output,\n unit == unit_before && same(held_program, before));\n } else if (command == \"X\") {\n const std::string text = bytes(row);\n const std::string unit = bytes(row);\n const std::size_t capacity = size(row);\n std::string output = bytes(row);\n end(row);\n const std::string text_before = text;\n const std::string unit_before = unit;\n const Error error = expand(text, unit, capacity, output);\n held_result = output;\n text_result(error, output, text == text_before && unit == unit_before);\n } else if (command == \"H\") {\n end(row);\n std::cout << '[';\n view_value(held_view);\n std::cout << ',';\n service_value(held_service);\n std::cout << ',';\n program_value(held_program);\n std::cout << ',';\n string_value(held_result);\n std::cout << ']';\n } else {\n throw std::runtime_error(\"command\");\n }\n std::cout << '\\n';\n }\n } catch (const std::exception& error) {\n std::cerr << error.what() << '\\n';\n return 2;\n }\n return std::cin.bad() || !std::cout ? 2 : 0;\n}\n" }