Implemented ArrayParser and unit tests to verify its working.

This commit is contained in:
eelke 2017-12-16 10:31:51 +01:00
parent b5d800c87e
commit ec930218cd
4 changed files with 254 additions and 3 deletions

53
pgsql/ArrayParser.h Normal file
View file

@ -0,0 +1,53 @@
#ifndef ARRAYPARSER_H
#define ARRAYPARSER_H
#include <optional>
#include <tuple>
#include <string>
#include <string_view>
namespace Pgsql {
/** Class for parsing array values coming from postgresql
*
* exceptions are used to report serious errors
* in production these kind of errors should rarely happen as
* they are either a bug in this parser or postgres changed its format.
*/
class ArrayParser {
public:
/**
* \param data The string that needs parsing (warning just the pointer is stored, the string is not copied)
*/
explicit ArrayParser(const char *array_string);
class NextElemResult {
public:
bool ok;
std::optional<std::string_view> value;
};
/**
*
* Usage:
* auto [ok, val] = parser.GetNextElem();
*
* \return the bool signals if there was a next element when it is true or the end of the array in which
* case it is false. The optional is not set when the next element IS NULL. Otherwise it refers to the
* string value of the element. If the element was quoted it has been stripped of quotes and escapes.
*/
NextElemResult GetNextElem();
private:
const char *data;
const char *end;
std::string temp; // internal buffer for when a value needs escaping
const char *pos;
void parseQuotedValue();
void initializeParse();
/** Moves pos forward to the first non whitespace character.
*/
void skipWhitespace();
};
} // end namespace Pgsql
#endif // ARRAYPARSER_H