#include "latch.hpp" #include #include namespace release { namespace { bool decimal(const std::string &text) { if (text.empty()) { return false; } for (unsigned char byte : text) { if (byte < '0' || byte > '9') { return false; } } return true; } int parse_component(const std::string &text, Id &out) { Count value = 0; int result = parse_number(text, std::numeric_limits::max(), value); if (result != 0) { return result; } out = static_cast(value); return 0; } int add_participants(Latch &latch, const std::vector &ids) { for (Id id : ids) { int result = latch.join(id); if (result < 0) { return result; } } return 0; } int add_waiters(Latch &latch, const std::vector &ids) { for (Id id : ids) { int result = latch.await(id); if (result != 0) { return result; } } return 0; } } int parse_number(const std::string &text, Count limit, Count &out) { if (!decimal(text)) { return -22; } Count value = 0; for (unsigned char byte : text) { Count digit = byte - '0'; if (value > limit / 10) { return -34; } if (value == limit / 10 && digit > limit % 10) { return -34; } value = value * 10 + digit; } out = value; return 0; } int parse_ids(const std::string &text, std::vector &out) { std::vector result; std::set seen; if (text == "-") { out = result; return 0; } std::size_t start = 0; while (true) { std::size_t end = text.find(',', start); std::string token = end == std::string::npos ? text.substr(start) : text.substr(start, end - start); Id id = 0; int status = parse_component(token, id); if (status != 0) { return status; } if (!seen.insert(id).second) { return -17; } if (result.size() == entry_limit) { return -28; } result.push_back(id); if (end == std::string::npos) { break; } start = end + 1; } out = result; return 0; } int initialize(Latch &latch, const std::string &target, const std::string &participants, const std::string &waiters) { Count value = 0; int status = parse_number(target, count_limit, value); if (status != 0) { return status; } std::vector participant_ids; status = parse_ids(participants, participant_ids); if (status != 0) { return status; } std::vector waiter_ids; status = parse_ids(waiters, waiter_ids); if (status != 0) { return status; } Latch proposed; status = proposed.reset(value); if (status != 0) { return status; } status = add_participants(proposed, participant_ids); if (status != 0) { return status; } status = add_waiters(proposed, waiter_ids); if (status != 0) { return status; } latch = proposed; return 0; } int parse_waiter(const std::string &text, WaitInfo &out) { std::size_t separator = text.find('|'); if (separator == std::string::npos) { return -22; } if (text.find('|', separator + 1) != std::string::npos) { return -22; } std::string state_text = text.substr(0, separator); std::string ticket_text = text.substr(separator + 1); Count state = 0; int status = parse_number(state_text, 2, state); if (status != 0) { return status; } Count ticket = 0; status = parse_number(ticket_text, std::numeric_limits::max(), ticket); if (status != 0) { return status; } WaitInfo result; result.state = static_cast(state); result.ticket = ticket; out = result; return 0; } }