From f9db93e41afc876d83680aae16dbaa365c1c7f91 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 5 Oct 2021 18:16:22 -0500 Subject: [PATCH] Add simple HTTPClient The binance depth stream must be bootstrapped with a depth snapshot, which is retrieved via an HTTP GET request --- src/lib/api/io/URL.proto | 15 +++ src/lib/collections/FlatUnorderedMap.h | 159 +++++++++++++++++++++++++ src/lib/io/QueryParams.cpp | 16 +++ src/lib/io/QueryParams.h | 13 ++ src/lib/io/URL.cpp | 153 ++++++++++++++++++++++++ src/lib/io/URL.h | 96 +++++++++++++++ src/lib/io/http/CurlHandle.cpp | 21 ++++ src/lib/io/http/CurlHandle.h | 21 ++++ src/lib/io/http/HTTPClient.cpp | 55 +++++++++ src/lib/io/http/HTTPClient.h | 35 ++++++ src/lib/io/http/HTTPHeaders.cpp | 46 +++++++ src/lib/io/http/HTTPHeaders.h | 23 ++++ src/lib/io/http/HTTPRequest.cpp | 17 +++ src/lib/io/http/HTTPRequest.h | 26 ++++ src/lib/io/http/HTTPResponse.cpp | 25 ++++ src/lib/io/http/HTTPResponse.h | 33 +++++ src/lib/utils/StringSplitter.h | 81 +++++++++++++ 17 files changed, 835 insertions(+) create mode 100644 src/lib/api/io/URL.proto create mode 100644 src/lib/collections/FlatUnorderedMap.h create mode 100644 src/lib/io/QueryParams.cpp create mode 100644 src/lib/io/QueryParams.h create mode 100644 src/lib/io/URL.cpp create mode 100644 src/lib/io/URL.h create mode 100644 src/lib/io/http/CurlHandle.cpp create mode 100644 src/lib/io/http/CurlHandle.h create mode 100644 src/lib/io/http/HTTPClient.cpp create mode 100644 src/lib/io/http/HTTPClient.h create mode 100644 src/lib/io/http/HTTPHeaders.cpp create mode 100644 src/lib/io/http/HTTPHeaders.h create mode 100644 src/lib/io/http/HTTPRequest.cpp create mode 100644 src/lib/io/http/HTTPRequest.h create mode 100644 src/lib/io/http/HTTPResponse.cpp create mode 100644 src/lib/io/http/HTTPResponse.h create mode 100644 src/lib/utils/StringSplitter.h diff --git a/src/lib/api/io/URL.proto b/src/lib/api/io/URL.proto new file mode 100644 index 000000000..61913fae7 --- /dev/null +++ b/src/lib/api/io/URL.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; +package bts.api.io; + +message QueryParam { + string key = 1; + string value = 2; +} + +message URL { + string protocol = 1; + string host = 2; + uint32 port = 3; + string path = 4; + repeated QueryParam query_params = 5; +} diff --git a/src/lib/collections/FlatUnorderedMap.h b/src/lib/collections/FlatUnorderedMap.h new file mode 100644 index 000000000..a4be47c1a --- /dev/null +++ b/src/lib/collections/FlatUnorderedMap.h @@ -0,0 +1,159 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace bts::collections { + +template +class FlatUnorderedMap { + public: + using Container = std::vector>; + using Iterator = Container::iterator; + using ConstIterator = Container::const_iterator; + using ReverseIterator = Container::reverse_iterator; + using ConstReverseIterator = Container::const_reverse_iterator; + + Value *get(const Key &key); + const Value *get(const Key &key) const; + + template + std::pair try_emplace(const Key &key, Args... args); + + Iterator begin(); + ConstIterator begin() const; + + ReverseIterator rbegin(); + ConstReverseIterator rbegin() const; + + Iterator end(); + ConstIterator end() const; + + ReverseIterator rend(); + ConstReverseIterator rend() const; + + bool empty() const; + + std::size_t size() const; + + Value &operator[](const Key &key); + + private: + std::vector> items_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +Value *FlatUnorderedMap::get(const Key &key) { + for (auto &qp : items_) { + if (qp.first == key) { + return &qp.second; + } + } + return nullptr; +} + +template +const Value *FlatUnorderedMap::get(const Key &key) const { + for (auto &qp : items_) { + if (qp.first == key) { + return &qp.second; + } + } + return nullptr; +} + +// template +// std::pair::Iterator, bool> +// FlatUnorderedMap::try_emplace(const Key &key) { +// for (auto it = items_.begin(); it != items_.end(); it++) { +// if (it->first == key) { +// return {it, false}; +// } +// } +// return items_.emplace_back(key); +// } + +template +template +std::pair::Iterator, bool> +FlatUnorderedMap::try_emplace(const Key &key, Args... args) { + for (auto it = items_.begin(); it != items_.end(); it++) { + if (it->first == key) { + return {it, false}; + } + } + Value v(args...); + items_.emplace_back(key, std::move(v)); + return {items_.end() - 1, true}; +} + +template +typename FlatUnorderedMap::Iterator +FlatUnorderedMap::begin() { + return items_.begin(); +} + +template +typename FlatUnorderedMap::ConstIterator +FlatUnorderedMap::begin() const { + return items_.begin(); +} + +template +typename FlatUnorderedMap::ReverseIterator +FlatUnorderedMap::rbegin() { + return items_.rbegin(); +} + +template +typename FlatUnorderedMap::ConstReverseIterator +FlatUnorderedMap::rbegin() const { + return items_.rbegin(); +} + +template +typename FlatUnorderedMap::Iterator +FlatUnorderedMap::end() { + return items_.end(); +} + +template +typename FlatUnorderedMap::ConstIterator +FlatUnorderedMap::end() const { + return items_.end(); +} + +template +typename FlatUnorderedMap::ReverseIterator +FlatUnorderedMap::rend() { + return items_.rend(); +} + +template +typename FlatUnorderedMap::ConstReverseIterator +FlatUnorderedMap::rend() const { + return items_.rend(); +} + +template +bool FlatUnorderedMap::empty() const { + return items_.empty(); +} + +template +std::size_t FlatUnorderedMap::size() const { + return items_.size(); +} + +template +Value &FlatUnorderedMap::operator[](const Key &key) { + return try_emplace(key).first->second; +} + +} // namespace bts::collections diff --git a/src/lib/io/QueryParams.cpp b/src/lib/io/QueryParams.cpp new file mode 100644 index 000000000..a547c7609 --- /dev/null +++ b/src/lib/io/QueryParams.cpp @@ -0,0 +1,16 @@ +#include "QueryParams.h" +#include +#include + +namespace bts::io { + +std::string QueryParams::to_string() const { + std::string rv; + auto query_param_strs = ranges::transform_view( + *this, [](const auto &qp) { return qp.first + "=" + qp.second; }); + rv.append(fmt::format( + "{}", fmt::join(query_param_strs.begin(), query_param_strs.end(), "&"))); + return rv; +} + +} // namespace bts::io diff --git a/src/lib/io/QueryParams.h b/src/lib/io/QueryParams.h new file mode 100644 index 000000000..78f01310f --- /dev/null +++ b/src/lib/io/QueryParams.h @@ -0,0 +1,13 @@ +#pragma once +#include "lib/collections/FlatUnorderedMap.h" +#include + +namespace bts::io { + +class QueryParams + : public collections::FlatUnorderedMap { + public: + std::string to_string() const; +}; + +} // namespace bts::io diff --git a/src/lib/io/URL.cpp b/src/lib/io/URL.cpp new file mode 100644 index 000000000..6b8660d6a --- /dev/null +++ b/src/lib/io/URL.cpp @@ -0,0 +1,153 @@ +#include "URL.h" +#include "lib/api/io/URL.pb.h" +#include +#include +#include +#include +#include + +namespace bts::io { + +// URL:: URL(const api::io::URL &url) : protocol_(url.protocol()), +// host_(url.host()), port_(url.port()), path_(url.path()) {} +URL::URL(const bts::api::io::URL &url) + : protocol_(url.protocol()), host_(url.host()), port_(url.port()), + path_(url.path()) { + for (const auto &qp : url.query_params()) { + query_params_[qp.key()] = qp.value(); + } +} + +std::vector> URL::well_known_ports_() { + return std::vector>{ + {"http", 80}, {"https", 443}, {"ws", 80}, + {"wss", 443}, {"ssh", 22}, {"sftp", 22}, + }; +} + +std::uint16_t URL::get_known_port_for_protocol(const std::string &protocol) { + auto wkps = well_known_ports_(); + for (const auto &p : wkps) { + if (p.first == protocol) { + return p.second; + } + } + return 0; +} + +std::string URL::get_known_protocol_for_port(std::uint16_t port) { + auto wkps = well_known_ports_(); + for (const auto &p : wkps) { + if (p.second == port) { + return p.first; + } + } + return ""; +} + +std::string &URL::protocol() { return protocol_; } +const std::string &URL::protocol() const { return protocol_; } + +std::string &URL::host() { return host_; } +const std::string &URL::host() const { return host_; } + +std::uint16_t &URL::port() { return port_; } +std::uint16_t URL::port() const { return port_; } + +std::string &URL::path() { return path_; } +const std::string &URL::path() const { return path_; } + +QueryParams &URL::query_params() { return query_params_; } + +const QueryParams &URL::query_params() const { return query_params_; } + +std::string URL::to_string() const { + auto rv = protocol() + "://"; + if (host().empty()) { + rv.append("localhost"); + } else { + rv.append(host()); + } + if (port_ != 0) { + rv.append(":" + std::to_string(port_)); + } + if (!path_.empty()) { + rv.append("/" + path_); + } + if (!query_params_.empty()) { + rv.append("?"); + auto query_param_strs = + ranges::transform_view(query_params_, [](const auto &qp) { + return qp.first + "=" + qp.second; + }); + rv.append(fmt::format( + "{}", + fmt::join(query_param_strs.begin(), query_param_strs.end(), "&"))); + } + return rv; +} + +std::ostream &operator<<(std::ostream &out, const URL &url) noexcept { + out << url.to_string(); + return out; +} + +void URL::fill_protocol(const std::string &protocol) { + if (protocol_.empty()) { + protocol_ = protocol; + } +} + +void URL::fill_host(const std::string &host) { + if (host_.empty()) { + host_ = host; + } +} + +void URL::fill_port(std::uint16_t port) { + if (port_ == 0) { + port_ = port; + } +} + +void URL::fill_path(const std::string &path) { + if (path_.empty()) { + path_ = path; + } +} + +void URL::fill_query_params( + const QueryParams &query_params, bool add_missing_params) { + if (query_params_.empty()) { + query_params_ = query_params; + } else if (add_missing_params) { + for (const auto &[k, v] : query_params) { + query_params_.try_emplace(k, v); + } + } +} + +void URL::fill_with(const URL &url) { + fill_path(url.path()); + fill_host(url.host()); + fill_protocol(url.protocol()); + fill_port(url.port()); + fill_query_params(url.query_params()); +} + +bool operator==(const URL &lhs, const URL &rhs) noexcept { + bool rv = lhs.protocol() == rhs.protocol() && lhs.host() == rhs.host() && + lhs.path() == rhs.path() && lhs.port() == rhs.port(); + if (rv) { + rv = std::equal( + lhs.query_params().begin(), lhs.query_params().end(), + rhs.query_params().begin(), rhs.query_params().end()); + } + return rv; +} + +bool operator!=(const URL &lhs, const URL &rhs) noexcept { + return !(lhs == rhs); +} + +} // namespace bts::io diff --git a/src/lib/io/URL.h b/src/lib/io/URL.h new file mode 100644 index 000000000..56f4f199a --- /dev/null +++ b/src/lib/io/URL.h @@ -0,0 +1,96 @@ +#pragma once +#include "QueryParams.h" +#include "lib/core/HashPhaser.h" +#include +#include +#include +#include +#include +#include + +namespace bts::api::io { +class URL; +} + +namespace bts::io { + +class URL { + public: + URL() = default; + explicit URL(const bts::api::io::URL &url); + + URL(const URL &) = default; + URL(URL &&) noexcept = default; + URL &operator=(const URL &) = default; + URL &operator=(URL &&) noexcept = default; + + std::string &protocol(); + const std::string &protocol() const; + + std::string &host(); + const std::string &host() const; + + std::uint16_t &port(); + std::uint16_t port() const; + + std::string &path(); + const std::string &path() const; + + QueryParams &query_params(); + const QueryParams &query_params() const; + + std::string to_string() const; + + void fill_protocol(const std::string &protocol); + void fill_host(const std::string &host); + void fill_path(const std::string &path); + void fill_port(std::uint16_t port); + void fill_query_params( + const QueryParams &query_params, bool add_missing_params = false); + void fill_with(const URL &url); + + static std::uint16_t get_known_port_for_protocol( + const std::string &protocol); + static std::string get_known_protocol_for_port(std::uint16_t port); + + private: + std::string protocol_; + std::string host_; + std::uint16_t port_; + std::string path_; + QueryParams query_params_; + + static std::vector> + well_known_ports_(); +}; + +bool operator==(const URL &lhs, const URL &rhs) noexcept; +bool operator!=(const URL &lhs, const URL &rhs) noexcept; + +std::ostream &operator<<(std::ostream &out, const URL &url) noexcept; + +} // namespace bts::io + +namespace std { + +template <> +struct hash { + std::size_t operator()(const bts::io::URL &url) const; +}; + +} // namespace std + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +namespace std { + +inline std::size_t hash::operator()( + const bts::io::URL &url) const { + return bts::core::HashPhaser::auto_phase( + url.protocol(), url.host(), url.port(), url.path(), + url.query_params().size()); +} + +} // namespace std diff --git a/src/lib/io/http/CurlHandle.cpp b/src/lib/io/http/CurlHandle.cpp new file mode 100644 index 000000000..79db4d9ec --- /dev/null +++ b/src/lib/io/http/CurlHandle.cpp @@ -0,0 +1,21 @@ +#include "CurlHandle.h" +#include +#include + +namespace bts::http { + +CurlHandle::CurlHandle() { + const auto eCode = curl_global_init(CURL_GLOBAL_ALL); + if (eCode != CURLE_OK) { + throw std::runtime_error{"Error initializing libCURL"}; + } +} + +CurlHandle::~CurlHandle() { curl_global_cleanup(); } + +CurlHandle &CurlHandle::instance() { + static CurlHandle inst{}; + return inst; +} + +} // namespace bts::http diff --git a/src/lib/io/http/CurlHandle.h b/src/lib/io/http/CurlHandle.h new file mode 100644 index 000000000..eaef53d54 --- /dev/null +++ b/src/lib/io/http/CurlHandle.h @@ -0,0 +1,21 @@ +#pragma once + +namespace bts::http { + +class CurlHandle { + public: + static CurlHandle &instance(); + + CurlHandle(CurlHandle const &) = delete; + CurlHandle(CurlHandle &&) = delete; + + CurlHandle &operator=(CurlHandle const &) = delete; + CurlHandle &operator=(CurlHandle &&) = delete; + + ~CurlHandle(); + + private: + CurlHandle(); +}; + +} // namespace bts::http diff --git a/src/lib/io/http/HTTPClient.cpp b/src/lib/io/http/HTTPClient.cpp new file mode 100644 index 000000000..428928cf5 --- /dev/null +++ b/src/lib/io/http/HTTPClient.cpp @@ -0,0 +1,55 @@ +#include "HTTPClient.h" +#include "HTTPRequest.h" +#include "HTTPResponse.h" +#include "lib/io/URL.h" +#include "lib/logging/logging.h" +#include +#include + +namespace bts::io::http { + +static size_t write_data_( + void *ptr, size_t size, size_t nmemb, std::string *data) { + data->append((char *)ptr, size * nmemb); + return size * nmemb; +} + +HTTPResponse HTTPClient::get(const HTTPRequest &request) { + auto *curl = curl_easy_init(); + auto url_str = request.url().to_string(); + // LOG(md, bts::Info, "do GET: {}", url_str); + curl_easy_setopt(curl, CURLOPT_URL, url_str.c_str()); + + struct curl_slist *request_headers = NULL; + for (const auto &[k, v] : request.headers()) { + request_headers = curl_slist_append( + request_headers, fmt::format("{}: {}", k, v).c_str()); + } + + std::string response_string; + std::string header_string; + std::string response_header_string; + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string); + curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string); + + char *url; + long response_code; + double elapsed; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); + curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed); + curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); + // curl_easy_getinfo(curl, CURLINFO_HEADER_OUT, &response_header_string); + + curl_easy_perform(curl); + curl_easy_cleanup(curl); + curl = NULL; + HTTPResponse response; + response.headers() = HTTPHeaders(response_header_string); + response.body() = std::move(response_string); + response.code() = response_code; + // LOG(md, bts::Info, "do GET: {}. Received response:\n{}", url_str, response); + return response; +} + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPClient.h b/src/lib/io/http/HTTPClient.h new file mode 100644 index 000000000..c8eba9272 --- /dev/null +++ b/src/lib/io/http/HTTPClient.h @@ -0,0 +1,35 @@ +#include +#include +#include // std::size_t +#include // snprintf +#include +#include // strerror, strlen, memcpy, strcpy +#include +#include +#include +#include +#include +#include // std::unique_ptr +#include +#include +#include // va_start, etc. +#include +#include +#include +#include +#include +#include +#include + +namespace bts::io::http { + +class HTTPRequest; +class HTTPResponse; + +class HTTPClient { + public: + // HTTPClient(const std::string &url); + HTTPResponse get(const HTTPRequest &request); +}; + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPHeaders.cpp b/src/lib/io/http/HTTPHeaders.cpp new file mode 100644 index 000000000..9bffd8418 --- /dev/null +++ b/src/lib/io/http/HTTPHeaders.cpp @@ -0,0 +1,46 @@ +#include "HTTPHeaders.h" +#include "lib/utils/StringSplitter.h" +#include +#include +#include +#include +#include + +namespace bts::io::http { + +HTTPHeaders::HTTPHeaders(const std::string &str) { + auto item_strs = utils::StringSplitter::split(str, "\n"); + auto items = ranges::transform_view( + item_strs, + [](const std::string &item_str) -> std::pair { + auto kv = utils::StringSplitter::split(item_str, ":"); + if (kv.empty()) { + return std::make_pair("", ""); + } + if (kv.size() == 1) { + return std::make_pair(kv[0], std::string("")); + } + return std::make_pair(kv[0], kv[1]); + }); + for (auto p : items) { + try_emplace(p.first, p.second); + } +} + +std::string HTTPHeaders::to_string() const { + std::string rv; + auto query_param_strs = ranges::transform_view( + *this, [](const auto &qp) { return qp.first + ": " + qp.second; }); + rv.append(fmt::format( + "{}", + fmt::join(query_param_strs.begin(), query_param_strs.end(), "\n"))); + return rv; +} + +std::ostream &operator<<( + std::ostream &out, const HTTPHeaders &headers) noexcept { + out << headers.to_string(); + return out; +} + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPHeaders.h b/src/lib/io/http/HTTPHeaders.h new file mode 100644 index 000000000..3c5dfe7a4 --- /dev/null +++ b/src/lib/io/http/HTTPHeaders.h @@ -0,0 +1,23 @@ +#pragma once +#include "lib/collections/FlatUnorderedMap.h" + +namespace bts::io::http { + +class HTTPHeaders + : public collections::FlatUnorderedMap { + public: + HTTPHeaders() = default; + explicit HTTPHeaders(const std::string &header_str); + + HTTPHeaders(const HTTPHeaders &) noexcept = default; + HTTPHeaders(HTTPHeaders &&) noexcept = default; + HTTPHeaders &operator=(const HTTPHeaders &) noexcept = default; + HTTPHeaders &operator=(HTTPHeaders &&) noexcept = default; + + std::string to_string() const; +}; + +std::ostream &operator<<( + std::ostream &out, const HTTPHeaders &headers) noexcept; + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPRequest.cpp b/src/lib/io/http/HTTPRequest.cpp new file mode 100644 index 000000000..70f953900 --- /dev/null +++ b/src/lib/io/http/HTTPRequest.cpp @@ -0,0 +1,17 @@ +#include "HTTPRequest.h" + +namespace bts::io::http { + +io::URL &HTTPRequest::url() { return url_; } + +const io::URL &HTTPRequest::url() const { return url_; } + +HTTPHeaders &HTTPRequest::headers() { return headers_; } + +const HTTPHeaders &HTTPRequest::headers() const { return headers_; } + +std::string &HTTPRequest::body() { return body_; } + +const std::string &HTTPRequest::body() const { return body_; } + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPRequest.h b/src/lib/io/http/HTTPRequest.h new file mode 100644 index 000000000..bee527a19 --- /dev/null +++ b/src/lib/io/http/HTTPRequest.h @@ -0,0 +1,26 @@ +#pragma once +#include "HTTPHeaders.h" +#include "lib/io/URL.h" +#include +#include + +namespace bts::io::http { + +class HTTPRequest { + public: + io::URL &url(); + const io::URL &url() const; + + HTTPHeaders &headers(); + const HTTPHeaders &headers() const; + + std::string &body(); + const std::string &body() const; + + private: + io::URL url_; + HTTPHeaders headers_; + std::string body_; +}; + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPResponse.cpp b/src/lib/io/http/HTTPResponse.cpp new file mode 100644 index 000000000..cc7c14c91 --- /dev/null +++ b/src/lib/io/http/HTTPResponse.cpp @@ -0,0 +1,25 @@ +#include "HTTPResponse.h" +#include + +namespace bts::io::http { + +HTTPHeaders &HTTPResponse::headers() { return headers_; } + +const HTTPHeaders &HTTPResponse::headers() const { return headers_; } + +std::string &HTTPResponse::body() { return body_; } + +const std::string &HTTPResponse::body() const { return body_; } + +std::int64_t &HTTPResponse::code() { return code_; } + +std::int64_t HTTPResponse::code() const { return code_; } + +std::ostream &operator<<( + std::ostream &out, const HTTPResponse &response) noexcept { + out << response.headers() << "\n" + << response.body() << "\nCode: " << response.code(); + return out; +} + +} // namespace bts::io::http diff --git a/src/lib/io/http/HTTPResponse.h b/src/lib/io/http/HTTPResponse.h new file mode 100644 index 000000000..c22ce2c8f --- /dev/null +++ b/src/lib/io/http/HTTPResponse.h @@ -0,0 +1,33 @@ +#pragma once +#include "HTTPHeaders.h" +#include "lib/io/URL.h" +#include +#include + +namespace bts::io::http { + +class HTTPResponse { + public: + io::URL &url(); + const io::URL &url() const; + + HTTPHeaders &headers(); + const HTTPHeaders &headers() const; + + std::string &body(); + const std::string &body() const; + + std::int64_t &code(); + std::int64_t code() const; + + private: + io::URL url_; + HTTPHeaders headers_; + std::string body_; + std::int64_t code_; +}; + +std::ostream &operator<<( + std::ostream &out, const HTTPResponse &response) noexcept; + +} // namespace bts::io::http diff --git a/src/lib/utils/StringSplitter.h b/src/lib/utils/StringSplitter.h new file mode 100644 index 000000000..bc1750723 --- /dev/null +++ b/src/lib/utils/StringSplitter.h @@ -0,0 +1,81 @@ +#pragma once +#include +#include +#include +#include + +namespace bts::utils { + +class StringSplitter { + public: + template + static std::vector split( + Range range, const std::string &delimiter); + + template + static std::vector split( + Iterator first, Iterator last, const std::string &delimiter); + + template + static void split( + Iterator first, + Iterator last, + const std::string &delimiter, + std::vector &parts); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +std::vector StringSplitter::split( + Range range, const std::string &delimiter) { + std::vector parts; + split(range.begin(), range.end(), delimiter, parts); + return parts; +} + +template +std::vector StringSplitter::split( + Iterator first, Iterator last, const std::string &delimiter) { + std::vector parts; + split(first, last, delimiter, parts); + return parts; +} + +template +void StringSplitter::split( + Iterator first, + Iterator last, + const std::string &delimiter, + std::vector &parts) { + + auto cursor = first; + auto curr_end = first; + auto curr_start = first; + + auto find_delim = [&cursor, &delimiter, &last]() { + for (auto it = cursor; it != last; it++) { + if (*it == delimiter[0]) { + if (std::equal( + it + 1, it + delimiter.size(), delimiter.begin() + 1, + delimiter.end())) { + return it; + } + } + } + return last; + }; + + while ((curr_end = find_delim()) != last) { + parts.push_back(std::string(curr_start, curr_end)); + curr_start = curr_end + delimiter.size(); + cursor = curr_end + delimiter.size(); + } + if (cursor != last) { + parts.push_back(std::string(cursor, last)); + } +} + +} // namespace bts::utils