#include "decimal.hpp" #include #include #include #include #include namespace decimal { namespace { bool separator(unsigned char byte) { return byte == ' ' || byte == '\t' || byte == '\r'; } std::vector words(const std::string& line) { std::vector result; std::size_t position = 0; while (position < line.size()) { while (position < line.size() && separator(line[position])) { ++position; } if (position == line.size()) { break; } std::size_t begin = position; while (position < line.size() && !separator(line[position])) { ++position; } result.push_back(line.substr(begin, position - begin)); } return result; } bool integer(const std::string& token, int& value) { if (token.empty()) { return false; } std::size_t position = 0; bool negative = token[0] == '-'; if (negative) { ++position; } if (position == token.size()) { return false; } unsigned long long magnitude = 0; unsigned long long bound = negative ? static_cast(INT_MAX) + 1 : static_cast(INT_MAX); for (; position < token.size(); ++position) { unsigned char byte = token[position]; if (byte < '0' || byte > '9') { return false; } unsigned digit = byte - '0'; if (magnitude > (bound - digit) / 10) { return false; } magnitude = magnitude * 10 + digit; } if (negative && magnitude == static_cast(INT_MAX) + 1) { value = INT_MIN; } else { value = negative ? -static_cast(magnitude) : static_cast(magnitude); } return true; } std::string unsigned_text(std::uint64_t value) { if (value == 0) { return "0"; } std::string result; while (value != 0) { char byte = static_cast('0' + value % 10); result.insert(result.begin(), byte); value /= 10; } return result; } std::string signed_text(std::int64_t value) { if (value < 0) { std::uint64_t magnitude = static_cast(-(value + 1)) + 1; return "-" + unsigned_text(magnitude); } return unsigned_text(static_cast(value)); } std::string prefix_text(const Prefix& result) { if (result.code != Code::OK) { return code_name(result.code); } std::string text = "OK "; text += signed_text(result.numerator); text += " "; text += unsigned_text(result.denominator); text += " "; text += signed_text(result.begin); text += " "; text += signed_text(result.end); text += " "; text += hex_encode(result.rest); return text; } std::string view_text(const View& view) { if (view.code != Code::OK) { return code_name(view.code); } std::string text = "OK "; text += hex_encode(view.source); text += view.scanned ? " 1 " : " 0 "; text += prefix_text(view.last); return text; } void line(std::ostream& output, const std::string& text) { output.write(text.data(), static_cast(text.size())); output.put('\n'); } std::string dispatch(Session& session, const std::vector& tokens) { if (tokens.empty()) { return "SYNTAX"; } const std::string& verb = tokens[0]; std::size_t wanted = 0; if (verb == "PUT" || verb == "SCAN") { wanted = 3; } else if (verb == "CLEAR" || verb == "SHOW") { wanted = 2; } else { return "SYNTAX"; } if (tokens.size() != wanted) { return "SYNTAX"; } int slot = 0; if (!integer(tokens[1], slot)) { return "SYNTAX"; } if (verb == "PUT") { std::string bytes; if (!hex_decode(tokens[2], bytes)) { return "SYNTAX"; } return code_name(session.put(slot, bytes)); } if (verb == "SCAN") { int begin = 0; if (!integer(tokens[2], begin)) { return "SYNTAX"; } return prefix_text(session.scan(slot, begin)); } if (verb == "CLEAR") { Code result = session.clear(slot); return code_name(result); } return view_text(session.view(slot)); } } void run_cli(Session& session, std::istream& input, std::ostream& output) { std::string text; unsigned count = 0; while (std::getline(input, text)) { if (count == 32) { line(output, "LIMIT"); return; } ++count; line(output, dispatch(session, words(text))); } } }