...
This commit is contained in:
parent
3821d965cc
commit
2acb97e973
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
@ -49,7 +50,8 @@ size_t AdcSplitCharRangeToTokens(OutR& result, const InR& input, const DelimT& d
|
||||
vv = input | std::views::split(delim);
|
||||
}
|
||||
|
||||
std::ranges::copy(vv, std::back_inserter(result));
|
||||
std::ranges::transform(vv, std::back_inserter(result),
|
||||
[](const auto& el) { return std::string(el.begin(), el.end()); });
|
||||
|
||||
// return number of tokens
|
||||
return std::ranges::distance(vv.begin(), vv.end());
|
||||
@ -218,6 +220,12 @@ class AdcTokenNetMessage : protected AdcNetMessageInterface // (hide setBytes/a
|
||||
public:
|
||||
static constexpr std::string_view tokenDelimiter{TOKEN_DELIM};
|
||||
|
||||
// back to public access
|
||||
using base_t::bytes;
|
||||
using base_t::byteSize;
|
||||
using base_t::messageEmpty;
|
||||
|
||||
|
||||
AdcTokenNetMessage() = default;
|
||||
|
||||
template <typename T, typename... Ts>
|
||||
@ -290,7 +298,7 @@ public:
|
||||
}
|
||||
|
||||
auto max_el = _storageSequence.size() - start;
|
||||
N >= max_el ? max_el * 2 - 1 : N * 2 - 1;
|
||||
N = N >= max_el ? max_el * 2 - 1 : N * 2 - 1;
|
||||
|
||||
std::ranges::copy(_outputSequence | std::views::drop(start * 2) | std::views::take(N) | std::views::join,
|
||||
std::back_inserter(r));
|
||||
@ -353,10 +361,18 @@ template <const char* KEY_PARAM_DELIM = constants::ADC_DEFAULT_KEY_PARAM_DELIMIT
|
||||
const char* PARAM_PARAM_DELIM = constants::ADC_DEFAULT_PARAM_PARAM_DELIMITER>
|
||||
class AdcKeyParamNetMessage : protected AdcTokenNetMessage<PARAM_PARAM_DELIM>
|
||||
{
|
||||
using base_t = AdcTokenNetMessage<PARAM_PARAM_DELIM>;
|
||||
|
||||
public:
|
||||
static constexpr std::string_view keyparamDelimiter{KEY_PARAM_DELIM};
|
||||
static constexpr std::string_view paramparamDelimiter{PARAM_PARAM_DELIM};
|
||||
|
||||
// back to public access
|
||||
using base_t::bytes;
|
||||
using base_t::byteSize;
|
||||
using base_t::messageEmpty;
|
||||
using base_t::storageSeq;
|
||||
|
||||
AdcKeyParamNetMessage() = default;
|
||||
|
||||
template <typename KeyT, typename... ParamTs>
|
||||
@ -414,7 +430,8 @@ public:
|
||||
template <traits::adc_output_char_range R>
|
||||
void paramsBytes(R& r, size_t start = 0, size_t N = std::numeric_limits<size_t>::max()) const
|
||||
{
|
||||
this->tokensBytes(r, start, N);
|
||||
// '+1' since the first 2 elements in _outputSequence are key and key-param delimiter
|
||||
this->tokensBytes(r, start + 1, N);
|
||||
}
|
||||
|
||||
|
||||
@ -429,7 +446,9 @@ public:
|
||||
setKeyParam(sp);
|
||||
} else {
|
||||
auto N = std::ranges::distance(sp.begin(), found.begin());
|
||||
setKeyParam(sp.first(N), sp.last(sp.size() - N - keyparamDelimiter.size()));
|
||||
setKeyParam(sp.first(N));
|
||||
this->_outputSequence.emplace_back(keyparamDelimiter); // add key-param delimiter in output sequence
|
||||
base_t::appendFromBytes(found.begin() + keyparamDelimiter.size(), sp.end());
|
||||
}
|
||||
}
|
||||
|
||||
@ -437,17 +456,6 @@ public:
|
||||
protected:
|
||||
std::string _key;
|
||||
// _outputSequence will store parameters
|
||||
|
||||
void updateState() override
|
||||
{
|
||||
if (this->_outputSequence.size()) {
|
||||
this->_outputSequence.emplace_back(paramparamDelimiter); // add param-param delimiter in output sequence
|
||||
} else {
|
||||
this->_outputSequence.emplace_back(keyparamDelimiter); // add key-param delimiter in output sequence
|
||||
}
|
||||
|
||||
this->_outputSequence.emplace_back(this->_storageSequence.back());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace adc
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include <list>
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include <doctest/doctest.h>
|
||||
#include <iostream>
|
||||
@ -6,21 +7,36 @@
|
||||
|
||||
using namespace adc;
|
||||
|
||||
static constexpr char DD[] = "=";
|
||||
|
||||
TEST_CASE("[ADC NET MESSAGE]")
|
||||
{
|
||||
AdcKeyParamNetMessage<> msg;
|
||||
AdcKeyParamNetMessage<DD> msg;
|
||||
|
||||
std::string_view bytes{"SET POS 1 2 3 4 5"};
|
||||
std::string_view bytes{"SET=POS 1 2 3 4 5"};
|
||||
|
||||
std::cout << "BYTES: [" << bytes << "]\n";
|
||||
std::cout << "INPUT BYTES: [" << bytes << "]\n";
|
||||
|
||||
msg.setFromBytes(bytes.begin(), bytes.end());
|
||||
|
||||
std::string key, pars;
|
||||
std::string key, pars, bb;
|
||||
msg.key(key);
|
||||
std::cout << "KEY: [" << key << "]\n";
|
||||
|
||||
msg.paramsBytes(pars, 1);
|
||||
// msg.paramsBytes(pars);
|
||||
msg.paramsBytes(pars);
|
||||
std::cout << "PARS BYTES: [" << pars << "]\n";
|
||||
pars.clear();
|
||||
msg.paramsBytes(pars, 2, 3);
|
||||
std::cout << "PARS BYTES: [" << pars << "]\n";
|
||||
|
||||
std::list<std::string> ls;
|
||||
msg.params(ls, 0, 3);
|
||||
for (auto& el : ls) {
|
||||
std::cout << "PAR: [" << el << "]\n";
|
||||
}
|
||||
|
||||
msg.bytes(bb);
|
||||
std::cout << "MESSAGE: [" << bb << "]\n";
|
||||
|
||||
REQUIRE_EQ(bb, bytes);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user