void client::raw_cmd(const std::experimental::string_view &content) { if (content.empty()) throw std::invalid_argument("content is empty."); con_.write(content.data()); }
bool util::string_contains(const std::experimental::string_view &str, const char c) { assert(!str.empty()); assert(c); return str.find(c) != std::experimental::string_view::npos; }
bool util::is_integer(const std::experimental::string_view &s) { assert(!s.empty()); for (char c: s) { if (!std::isdigit(c)) return false; } return true; }
/* * Validates an ipv4ipv6 address and return the result. * Thanks, beej! */ bool util::valid_ipv46_addr(const std::experimental::string_view &addr) { assert(!addr.empty()); /* Unused, but required by inet_pton(). */ unsigned char buf[sizeof(struct in6_addr)]; if (ip_version(addr) == ipv4) return inet_pton(AF_INET, addr.data(), buf); else return inet_pton(AF_INET6, addr.data(), buf); }
int util::ip_version(const std::experimental::string_view &addr) { assert(!addr.empty()); /* * Feels hackish. * * TODO: Look into and confirm/bust above statement. */ if (string_contains(addr, period)) return ipv4; return ipv6; }
static OutputContainer splitted(const std::experimental::string_view &collection, const Element &element) { OutputContainer output; if(!collection.empty()) { unsigned int start = 0, i = 0; for(; i != collection.size();) { if(collection[i] == element) { output.push_back(collection.substr(start, i-start)); ++i; start = i; } else ++i; } output.push_back(collection.substr(start, i)); } return output; }
/* * Split a string into multiple strings when a character is met. * Returns all tokens in an array. */ std::vector<std::string> util::split_string(const std::experimental::string_view &str, const char c) { assert(!str.empty()); assert(c); using std::experimental::string_view; std::vector<std::string> result; for (string_view::const_iterator len = str.begin(); len <= str.end(); len++) { string_view::const_iterator token_start = len; while (*len != c && *len) len++; result.emplace_back(token_start, len); } return result; }
static bool isOption(const std::experimental::string_view& str) { assert(!str.empty()); return str.front() == '-'; }