30 int forcedValues = -1;
34 auto const token = extractToken(
stream);
36 std::string tokenStr {
cbegin(token),
cend(token) };
43 if (forcedValues >= 0) {
44 bKey = (forcedValues == 0);
56 std::string
const& key = tokenStr;
63 auto const sizeToken = peekToken(
stream);
68 char const *b =
begin(sizeToken), *
e =
end(sizeToken);
69 if (std::from_chars(b,
e, forcedValues).ptr !=
e)
92 if (bKey) currentItem = &(data.
makeItem(std::move(tokenStr)));
96 "values started without a key ('" + tokenStr +
"' is not a valid key)."
99 currentItem->addValue(std::move(tokenStr));
104 if (forcedValues > 0) {
114 (std::initializer_list<std::pair<std::regex, unsigned int>> patterns)
117 for (
auto&
pattern: patterns) fPatterns.emplace_back(std::move(
pattern));
124 (std::initializer_list<std::pair<std::string, unsigned int>> patterns)
128 fPatterns.emplace_back(std::regex{
pattern.first },
pattern.second);
136 {
return parse(std::string_view{ s.data(), s.size() }); }
144 auto const start =
cbegin(buffer), bend =
cend(buffer);
146 while (finish != bend) {
147 if (*finish == fSep)
break;
159 return strip({
cbegin(buffer), findTokenLength(buffer) });
168 auto const start =
cbegin(buffer), bend =
cend(buffer);
169 std::size_t
const length = findTokenLength(buffer);
170 moveBufferHead(buffer, length + ((start + length == bend)? 0: 1));
171 return strip({ start, length });
174 auto const start =
cbegin(buffer), bend =
cend(buffer);
176 while (finish != bend) {
177 if (*finish == fSep)
break;
182 std::size_t
const tokenLength =
std::distance(start, finish);
183 moveBufferHead(buffer, tokenLength + ((finish == bend)? 0: 1));
185 return strip({ start, tokenLength });
195 return !buffer.empty() && std::isalpha(buffer.front());
201 template <
typename String>
204 {
return { data(s),
size(s) }; }
213 size = std::min(size, buffer.size());
214 return buffer = { buffer.data() +
size, buffer.size() - size };
222 {
return stripRight(stripLeft(stripRightChars<'\n', '\r', '\0'>(s))); }
231 if (!std::isspace(s.front()))
break;
245 if (!std::isspace(s.back()))
break;
259 if (s.back() != c)
break;
268 template <
char... Chars>
274 for (
char c: { Chars... }) ns = stripRightChar(ns, c);
275 if (ns == s)
return ns;
Parser to fill a KeyValuesData structure out of a character buffer.
bool isKey(SubBuffer_t const &buffer) const noexcept
Is content of buffer a key (as opposed to a value)?
static SubBuffer_t stripRightChars(SubBuffer_t s) noexcept
std::size_t size(FixedBins< T, C > const &) noexcept
auto cbegin(FixedBins< T, C > const &) noexcept
static SubBuffer_t stripRight(SubBuffer_t s) noexcept
Simple parser for comma-separated text.
auto cend(FixedBins< T, C > const &) noexcept
Representation of a single item of data: a key and several values.
ParsedData_t parse(std::string_view const &s) const
Parses the buffer s and returns a data structure with the content.
static SubBuffer_t stripRightChar(SubBuffer_t s, char c) noexcept
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
static Buffer_t & moveBufferHead(Buffer_t &buffer, std::size_t size) noexcept
Collection of items with key/values structure.
auto end(FixedBins< T, C > const &) noexcept
std::size_t findTokenLength(Buffer_t const &buffer) const noexcept
Returns the length of the next toke, up to the next separator (excluded).
KeyedCSVparser & addPatterns(std::initializer_list< std::pair< std::regex, unsigned int >> patterns)
Adds known patterns.
auto begin(FixedBins< T, C > const &) noexcept
static SubBuffer_t stripLeft(SubBuffer_t s) noexcept
SubBuffer_t extractToken(Buffer_t &buffer) const noexcept
Extracts the next token from the buffer and returns its value, stripped.
then echo File list $list not found else cat $list while read file do echo $file sed s
static SubBuffer_t strip(SubBuffer_t s) noexcept
std::string_view Buffer_t
Item & makeItem(std::string key)
Creates and registers a new item with the specified key.
SubBuffer_t peekToken(Buffer_t const &buffer) const noexcept
Returns the value of the next token, stripped.
std::string_view SubBuffer_t
static Buffer_t makeBuffer(String const &s) noexcept
bool empty(FixedBins< T, C > const &) noexcept