...
This commit is contained in:
@@ -27,6 +27,65 @@ static constexpr char ADC_DEFAULT_NETPROTO_STARTMARK[] = "\n\t\r\v\r\t\n";
|
||||
} // namespace constants
|
||||
|
||||
|
||||
template <const char* STOPSEQ = constants::ADC_DEFAULT_NETPROTO_STOPSEQ>
|
||||
struct AdcStopSeqSessionProto {
|
||||
static constexpr std::string_view STOP_SEQ{STOPSEQ};
|
||||
static constexpr size_t STOP_SEQ_SIZE = STOP_SEQ.size();
|
||||
|
||||
static_assert(STOP_SEQ_SIZE, "STOP BYTE SEQUENCE MUST NOT BE AN EMPTY ONE!!!");
|
||||
|
||||
template <traits::adc_input_char_range R>
|
||||
auto search(const R& r)
|
||||
{
|
||||
std::tuple<std::ranges::iterator_t<R>, std::ranges::iterator_t<R>, bool> res{r.begin(), r.end(), false};
|
||||
|
||||
std::get<1>(res) = std::search(r.begin(), r.end(), STOP_SEQ.begin(), STOP_SEQ.end());
|
||||
if (std::get<1>(res) != r.end()) { // move iterator to the one-past-the-end position
|
||||
std::get<1>(res) = std::advance(std::get<1>(res), STOP_SEQ_SIZE);
|
||||
std::get<2>(res) = true;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
template <traits::adc_input_char_range R>
|
||||
auto toProto(const R& r)
|
||||
{
|
||||
// return 2-element array with the first element as a view of the input range and
|
||||
// the second one - a view of the stop sequence
|
||||
|
||||
if constexpr (std::ranges::viewable_range<R>) {
|
||||
auto res = std::array{std::ranges::subrange(r.begin(), r.end()),
|
||||
std::ranges::subrange(STOP_SEQ.begin(), STOP_SEQ.end())};
|
||||
|
||||
return res;
|
||||
} else { // return a copy of input range with appended stop sequence
|
||||
auto res = r;
|
||||
std::ranges::copy(STOP_SEQ, std::back_inserter(res));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
template <traits::adc_input_char_range R>
|
||||
auto fromProto(const R& r)
|
||||
{
|
||||
auto N = std::distance(r.begin(), r.end());
|
||||
|
||||
if (N < STOP_SEQ_SIZE) { // one must ensure for input range size correctness
|
||||
return std::span<std::ranges::range_value_t<R>>();
|
||||
}
|
||||
|
||||
if constexpr (std::ranges::viewable_range<R>) {
|
||||
return std::ranges::subrange(r.begin(), r.end() - STOP_SEQ_SIZE);
|
||||
} else {
|
||||
R res;
|
||||
std::ranges::copy(r | std::views::take(N - STOP_SEQ_SIZE), std::back_inserter(res));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename InetT, const char* STOPSEQ = constants::ADC_DEFAULT_NETPROTO_STOPSEQ>
|
||||
struct AdcNetProtoStopSeq : InetT {
|
||||
static constexpr std::string_view stopSeq{STOPSEQ};
|
||||
|
||||
Reference in New Issue
Block a user