#include "protocol.hpp" #include #include #include #include namespace resource { namespace { bool separator(char ch) { return ch == ' ' || ch == '\t' || ch == '\r'; } bool known_verb(const std::string& verb) { return verb == "NEW" || verb == "RETAIN" || verb == "DROP" || verb == "MOVE" || verb == "SWAP" || verb == "SET" || verb == "ADD" || verb == "READ" || verb == "COUNT" || verb == "CLONE" || verb == "REPLACE" || verb == "CLEAR" || verb == "SNAP"; } std::size_t argument_count(const std::string& verb) { if (verb == "CLEAR" || verb == "SNAP") { return 0; } if (verb == "DROP" || verb == "READ" || verb == "COUNT") { return 1; } return 2; } bool all_digits(std::string_view word) { if (word.empty()) { return false; } for (char ch : word) { if (ch < '0' || ch > '9') { return false; } } return true; } std::string_view trim_zeros(std::string_view word) { while (word.size() > 1 && word.front() == '0') { word.remove_prefix(1); } return word; } bool read_magnitude(std::string_view digits, unsigned long long bound, unsigned long long& result) { digits = trim_zeros(digits); if (digits.size() > 10) { return false; } unsigned long long value = 0; for (char ch : digits) { unsigned digit = static_cast(ch - '0'); if (value > (bound - digit) / 10) { return false; } value = value * 10 + digit; } result = value; return true; } } std::vector split_words(const std::string& line) { std::vector words; std::size_t position = 0; while (position < line.size()) { while (position < line.size() && separator(line[position])) { ++position; } if (position == line.size()) { break; } const std::size_t start = position; while (position < line.size() && !separator(line[position])) { ++position; } words.emplace_back(line.substr(start, position - start)); } return words; } bool decimal_number(const std::string& word, int& result) { std::string_view text(word); bool negative = false; if (!text.empty() && (text.front() == '+' || text.front() == '-')) { negative = text.front() == '-'; text.remove_prefix(1); } if (!all_digits(text)) { return false; } unsigned long long magnitude = 0; const unsigned long long bound = negative ? static_cast(INT_MAX) + 1ULL : static_cast(INT_MAX); if (!read_magnitude(text, bound, magnitude)) { return false; } if (negative && magnitude == static_cast(INT_MAX) + 1ULL) { result = INT_MIN; } else { result = negative ? -static_cast(magnitude) : static_cast(magnitude); } return true; } ParsedCommand parse_command(const std::string& line) { ParsedCommand result{}; result.parse_error = Code::ok; const auto words = split_words(line); if (words.empty()) { result.parse_error = Code::bad_command; return result; } result.verb = words.front(); if (!known_verb(result.verb)) { result.parse_error = Code::bad_command; return result; } if (words.size() != argument_count(result.verb) + 1) { result.parse_error = Code::bad_argument; return result; } for (std::size_t index = 1; index < words.size(); ++index) { int number = 0; if (!decimal_number(words[index], number)) { result.parse_error = Code::bad_argument; result.arguments.clear(); return result; } result.arguments.push_back(number); } return result; } Result dispatch(Counter& counter, const ParsedCommand& command) { if (command.parse_error != Code::ok) { return {command.parse_error, false, 0}; } const auto& values = command.arguments; const std::string& verb = command.verb; if (verb == "NEW") { return counter.create(static_cast(values[0]), values[1]); } if (verb == "RETAIN") { return counter.retain(static_cast(values[0]), static_cast(values[1])); } if (verb == "DROP") { return counter.release(static_cast(values[0])); } if (verb == "MOVE") { return counter.move(static_cast(values[0]), static_cast(values[1])); } if (verb == "SWAP") { return counter.swap(static_cast(values[0]), static_cast(values[1])); } if (verb == "SET") { return counter.set(static_cast(values[0]), values[1]); } if (verb == "ADD") { return counter.add(static_cast(values[0]), values[1]); } if (verb == "READ") { return counter.read(static_cast(values[0])); } if (verb == "COUNT") { return counter.count(static_cast(values[0])); } if (verb == "CLONE") { return counter.clone(static_cast(values[0]), static_cast(values[1])); } if (verb == "REPLACE") { return counter.replace(static_cast(values[0]), values[1]); } if (verb == "CLEAR") { return counter.clear(); } return counter.snapshot_result(); } std::string code_name(Code code) { switch (code) { case Code::ok: return "OK"; case Code::bad_command: return "BAD_COMMAND"; case Code::bad_argument: return "BAD_ARGUMENT"; case Code::occupied: return "OCCUPIED"; case Code::empty: return "EMPTY"; case Code::range: return "RANGE"; case Code::limit: return "LIMIT"; } return "BAD_COMMAND"; } std::string encode_snapshot(const Snapshot& state) { std::ostringstream output; for (std::size_t index = 0; index < slot_count; ++index) { if (index) { output << ','; } if (!state.present[index]) { output << '.'; } else { const View& view = state.slots[index]; output << view.id << ':' << view.value << ':' << view.owners; } } output << '|' << state.active << '|'; if (state.finalized.empty()) { output << '-'; } else { for (std::size_t index = 0; index < state.finalized.size(); ++index) { if (index) { output << ','; } output << state.finalized[index]; } } return output.str(); } std::string encode_result(const Result& result, const Snapshot& state) { std::ostringstream output; output << code_name(result.code) << '|'; if (result.has_payload) { output << result.payload; } else { output << '-'; } output << '|' << encode_snapshot(state); return output.str(); } std::string execute(Counter& counter, const std::string& line) { const ParsedCommand command = parse_command(line); const Result result = dispatch(counter, command); return encode_result(result, counter.snapshot()); } }