Parser to fill a KeyValuesData
structure out of a character buffer.
More...
#include <KeyedCSVparser.h>
Classes | |
struct | InvalidFormat |
struct | MissingValues |
struct | ParserError |
Public Types | |
using | ParsedData_t = icarus::KeyValuesData |
using | Error = icarus::KeyValuesData::Error |
Base of all errors by KeyedCSVparser. More... | |
using | ErrorOnKey = icarus::KeyValuesData::ErrorOnKey |
using | MissingSize = KeyValuesData::MissingSize |
Expected number of values is missing. More... | |
Public Member Functions | |
KeyedCSVparser (char sep= ',') | |
Constructor: specifies the separator character. More... | |
template<typename String > | |
auto | makeBuffer (String const &s) noexcept-> Buffer_t |
template<char... Chars> | |
auto | stripRightChars (SubBuffer_t s) noexcept-> SubBuffer_t |
template<typename BIter , typename EIter > | |
auto | parse (BIter b, EIter e) const -> ParsedData_t |
ParsedData_t | parse (std::string_view const &s) const |
Parses the buffer s and returns a data structure with the content. More... | |
ParsedData_t | parse (std::string const &s) const |
template<typename BIter , typename EIter > | |
ParsedData_t | parse (BIter b, EIter e) const |
ParsedData_t | operator() (std::string_view const &s) const |
ParsedData_t | operator() (std::string const &s) const |
template<typename BIter , typename EIter > | |
ParsedData_t | operator() (BIter b, EIter e) const |
void | parse (std::string_view const &s, ParsedData_t &data) const |
Parses the buffer s and fills data with it. More... | |
Know patterns | |
The parser normally treats as a value everything that does not start with a letter. Known patterns may override this behaviour: if a token matches a known pattern, it is considered a key and it is possible to specify the expected number of values. The number of values can be:
Patterns are considered in the order they were added. | |
KeyedCSVparser & | addPattern (std::regex pattern, unsigned int values) |
Adds a single known pattern. More... | |
KeyedCSVparser & | addPattern (std::string const &pattern, unsigned int values) |
KeyedCSVparser & | addPatterns (std::initializer_list< std::pair< std::regex, unsigned int >> patterns) |
Adds known patterns. More... | |
KeyedCSVparser & | addPatterns (std::initializer_list< std::pair< std::string, unsigned int >> patterns) |
Static Public Attributes | |
static constexpr unsigned int | FixedSize = std::numeric_limits<unsigned int>::max() |
Expected values are missing. More... | |
static constexpr unsigned int | DynamicSize = FixedSize - 1U |
Mnemonic size value used in addPattern() calls. More... | |
Private Types | |
using | Buffer_t = std::string_view |
using | SubBuffer_t = std::string_view |
Private Member Functions | |
std::size_t | findTokenLength (Buffer_t const &buffer) const noexcept |
Returns the length of the next toke, up to the next separator (excluded). More... | |
SubBuffer_t | peekToken (Buffer_t const &buffer) const noexcept |
Returns the value of the next token, stripped. More... | |
SubBuffer_t | extractToken (Buffer_t &buffer) const noexcept |
Extracts the next token from the buffer and returns its value, stripped. More... | |
bool | isKey (SubBuffer_t const &buffer) const noexcept |
Is content of buffer a key (as opposed to a value)? More... | |
Static Private Member Functions | |
template<typename String > | |
static Buffer_t | makeBuffer (String const &s) noexcept |
static Buffer_t & | moveBufferHead (Buffer_t &buffer, std::size_t size) noexcept |
static SubBuffer_t | strip (SubBuffer_t s) noexcept |
static SubBuffer_t | stripLeft (SubBuffer_t s) noexcept |
static SubBuffer_t | stripRight (SubBuffer_t s) noexcept |
static SubBuffer_t | stripRightChar (SubBuffer_t s, char c) noexcept |
template<char... Chars> | |
static SubBuffer_t | stripRightChars (SubBuffer_t s) noexcept |
Private Attributes | |
char const | fSep = ',' |
Character used as token separator. More... | |
std::vector< std::pair < std::regex, unsigned int > > | fPatterns |
List of known patterns for matching keys, and how many values they hold. More... | |
Parser to fill a KeyValuesData
structure out of a character buffer.
It currently supports only single-line buffer.
The parser operates one "line" at a time, returning a KeyValuesData
with the values assigned to each detected key. No data type is implied: all elements are treated as strings, either a key or a value. The parser separates the elements according to a separator, strips them of trailing and heading spaces, then it decides whether each element is a value to be assigned to the last key found, or a new key. Keys are elements that have letters in them, values are anything else. This simple (and arguable) criterion can be broken with specific parser configuration: a pattern can be specified that when matched to an element will make it a key; the pattern can also set the number of values that key will require.
For example:
will return data
with 6 items.
Definition at line 68 of file KeyedCSVparser.h.
|
private |
Definition at line 164 of file KeyedCSVparser.h.
Base of all errors by KeyedCSVparser.
Definition at line 75 of file KeyedCSVparser.h.
Definition at line 76 of file KeyedCSVparser.h.
Expected number of values is missing.
Parsing format is not understood.
Definition at line 80 of file KeyedCSVparser.h.
Definition at line 72 of file KeyedCSVparser.h.
|
private |
Definition at line 165 of file KeyedCSVparser.h.
|
inline |
Constructor: specifies the separator character.
Definition at line 91 of file KeyedCSVparser.h.
|
inline |
Adds a single known pattern.
pattern | the regular expression matching the key for this pattern |
values | the number of values for this pattern |
addPattern()
calls may be chained) Definition at line 141 of file KeyedCSVparser.h.
|
inline |
Definition at line 143 of file KeyedCSVparser.h.
auto icarus::details::KeyedCSVparser::addPatterns | ( | std::initializer_list< std::pair< std::regex, unsigned int >> | patterns | ) |
Adds known patterns.
patterns | sequence of patterns to be added |
addPatterns()
calls may be chained)Each pattern is a pair key regex/number of values, like in addPattern()
.
Definition at line 114 of file KeyedCSVparser.cxx.
auto icarus::details::KeyedCSVparser::addPatterns | ( | std::initializer_list< std::pair< std::string, unsigned int >> | patterns | ) |
Definition at line 124 of file KeyedCSVparser.cxx.
|
privatenoexcept |
Extracts the next token from the buffer
and returns its value, stripped.
Definition at line 165 of file KeyedCSVparser.cxx.
|
privatenoexcept |
Returns the length of the next toke, up to the next separator (excluded).
Definition at line 141 of file KeyedCSVparser.cxx.
|
privatenoexcept |
Is content of buffer
a key (as opposed to a value)?
Definition at line 192 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
|
noexcept |
Definition at line 202 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
Definition at line 210 of file KeyedCSVparser.cxx.
|
inline |
Definition at line 100 of file KeyedCSVparser.h.
|
inline |
Definition at line 101 of file KeyedCSVparser.h.
|
inline |
Definition at line 103 of file KeyedCSVparser.h.
|
inline |
Parses the buffer s
and returns a data structure with the content.
Definition at line 236 of file KeyedCSVparser.h.
auto icarus::details::KeyedCSVparser::parse | ( | std::string const & | s | ) | const |
Definition at line 135 of file KeyedCSVparser.cxx.
ParsedData_t icarus::details::KeyedCSVparser::parse | ( | BIter | b, |
EIter | e | ||
) | const |
void icarus::details::KeyedCSVparser::parse | ( | std::string_view const & | s, |
ParsedData_t & | data | ||
) | const |
Parses the buffer s
and fills data
with it.
Definition at line 22 of file KeyedCSVparser.cxx.
auto icarus::details::KeyedCSVparser::parse | ( | BIter | b, |
EIter | e | ||
) | const -> ParsedData_t |
Definition at line 244 of file KeyedCSVparser.h.
|
privatenoexcept |
Returns the value of the next token, stripped.
Definition at line 157 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
Definition at line 220 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
Definition at line 226 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
Definition at line 240 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
Definition at line 255 of file KeyedCSVparser.cxx.
|
staticprivatenoexcept |
|
noexcept |
Definition at line 270 of file KeyedCSVparser.cxx.
|
static |
Mnemonic size value used in addPattern()
calls.
Definition at line 87 of file KeyedCSVparser.h.
|
static |
Expected values are missing.
Mnemonic size value used in addPattern()
calls.
Definition at line 85 of file KeyedCSVparser.h.
|
private |
List of known patterns for matching keys, and how many values they hold.
Definition at line 170 of file KeyedCSVparser.h.
|
private |
Character used as token separator.
Definition at line 167 of file KeyedCSVparser.h.