Merge branch 'master' into spirit-x3
This commit is contained in:
commit
04755f1a9b
2 changed files with 33 additions and 19 deletions
|
@ -35,34 +35,48 @@ using csv_value = std::string;
|
||||||
using csv_line = std::vector<csv_value>;
|
using csv_line = std::vector<csv_value>;
|
||||||
using csv_data = std::vector<csv_line>;
|
using csv_data = std::vector<csv_line>;
|
||||||
|
|
||||||
template <typename Iterator>
|
struct csv_white_space_skipper : qi::primitive_parser<csv_white_space_skipper>
|
||||||
struct csv_white_space_skipper : qi::grammar<Iterator>
|
|
||||||
{
|
{
|
||||||
csv_white_space_skipper()
|
template <typename Context, typename Iterator>
|
||||||
: csv_white_space_skipper::base_type(skip)
|
struct attribute
|
||||||
{
|
{
|
||||||
using namespace qi;
|
typedef qi::unused_type type;
|
||||||
qi::lit_type lit;
|
|
||||||
skip = +lit(' ')
|
|
||||||
;
|
|
||||||
}
|
|
||||||
qi::rule<Iterator> skip;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Iterator, typename Skipper = csv_white_space_skipper<Iterator> >
|
template <typename Iterator, typename Context
|
||||||
|
, typename Skipper, typename Attribute>
|
||||||
|
bool parse(Iterator& first, Iterator const& last
|
||||||
|
, Context& /*context*/, Skipper const& skipper
|
||||||
|
, Attribute& /*attr*/) const
|
||||||
|
{
|
||||||
|
qi::skip_over(first, last, skipper);
|
||||||
|
if (first != last && *first == ' ')
|
||||||
|
{
|
||||||
|
while (++first != last && *first == ' ')
|
||||||
|
;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Context>
|
||||||
|
qi::info what(Context& /*context*/) const
|
||||||
|
{
|
||||||
|
return qi::info("csv_white_space_skipper");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename Iterator, typename Skipper = csv_white_space_skipper>
|
||||||
struct csv_line_grammar : qi::grammar<Iterator, csv_line(char, char), Skipper>
|
struct csv_line_grammar : qi::grammar<Iterator, csv_line(char, char), Skipper>
|
||||||
{
|
{
|
||||||
csv_line_grammar()
|
csv_line_grammar()
|
||||||
: csv_line_grammar::base_type(line)
|
: csv_line_grammar::base_type(line)
|
||||||
{
|
{
|
||||||
using namespace qi;
|
|
||||||
qi::_a_type _a;
|
|
||||||
qi::_r1_type _r1;
|
qi::_r1_type _r1;
|
||||||
qi::_r2_type _r2;
|
qi::_r2_type _r2;
|
||||||
qi::lit_type lit;
|
qi::lit_type lit;
|
||||||
qi::_1_type _1;
|
|
||||||
qi::char_type char_;
|
qi::char_type char_;
|
||||||
qi::omit_type omit;
|
|
||||||
unesc_char.add
|
unesc_char.add
|
||||||
("\\a", '\a')
|
("\\a", '\a')
|
||||||
("\\b", '\b')
|
("\\b", '\b')
|
||||||
|
@ -76,11 +90,11 @@ struct csv_line_grammar : qi::grammar<Iterator, csv_line(char, char), Skipper>
|
||||||
("\\\"", '\"')
|
("\\\"", '\"')
|
||||||
("\"\"", '\"') // double quote
|
("\"\"", '\"') // double quote
|
||||||
;
|
;
|
||||||
line = -omit[char_("\n\r")] >> column(_r1, _r2) % lit(_r1)
|
line = -lit("\n\r") >> column(_r1, _r2) % lit(_r1)
|
||||||
;
|
;
|
||||||
column = quoted(_r2) | *(char_ - (lit(_r1)))
|
column = quoted(_r2) | *(char_ - lit(_r1))
|
||||||
;
|
;
|
||||||
quoted = omit[char_(_r1)[_a = _1]] > text(_a) > lit(_a) // support unmatched quotes or not (??)
|
quoted = lit(_r1) > text(_r1) > lit(_r1) // support unmatched quotes or not (??)
|
||||||
;
|
;
|
||||||
text = *(unesc_char | (char_ - lit(_r1)))
|
text = *(unesc_char | (char_ - lit(_r1)))
|
||||||
;
|
;
|
||||||
|
@ -90,7 +104,7 @@ private:
|
||||||
qi::rule<Iterator, csv_line(char, char), Skipper> line;
|
qi::rule<Iterator, csv_line(char, char), Skipper> line;
|
||||||
qi::rule<Iterator, csv_value(char, char)> column; // no-skip
|
qi::rule<Iterator, csv_value(char, char)> column; // no-skip
|
||||||
qi::rule<Iterator, csv_value(char)> text; // no-skip
|
qi::rule<Iterator, csv_value(char)> text; // no-skip
|
||||||
qi::rule<Iterator, qi::locals<char>, csv_value(char)> quoted; //no-skip
|
qi::rule<Iterator, csv_value(char)> quoted; // no-skip
|
||||||
qi::symbols<char const, char const> unesc_char;
|
qi::symbols<char const, char const> unesc_char;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ namespace csv_utils
|
||||||
{
|
{
|
||||||
|
|
||||||
static const mapnik::csv_line_grammar<char const*> line_g;
|
static const mapnik::csv_line_grammar<char const*> line_g;
|
||||||
static const mapnik::csv_white_space_skipper<char const*> skipper;
|
static const mapnik::csv_white_space_skipper skipper{};
|
||||||
|
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
static mapnik::csv_line parse_line(Iterator start, Iterator end, char separator, char quote, std::size_t num_columns)
|
static mapnik::csv_line parse_line(Iterator start, Iterator end, char separator, char quote, std::size_t num_columns)
|
||||||
|
|
Loading…
Reference in a new issue