gpt4 book ai didi

c++ - 使用 Spirit 引号和正常解析内存映射文件

转载 作者:行者123 更新时间:2023-11-30 05:37:55 25 4
gpt4 key购买 nike

根据the answer from sehe我想从内存映射文件中解析引用内容和正常内容,尽可能快

实际的解析器看起来像:

namespace qi = boost::spirit::qi;

using MatrixType = std::vector<std::vector<boost::string_ref>>;
template<typename It>
struct parser : qi::grammar<It, MatrixType(), qi::blank_type, qi::locals<char> >
{
parser()
: parser::base_type( table, "parser" )
{
using namespace boost::phoenix;
using namespace qi;

delimiter = ',';
quoted =
omit [ char_("'\"") [_a = _1] ]
>> raw [ *(char_ - char_(_a)) ] [ _val = construct<boost::string_ref>(begin(_1), size(_1)) ]
>> lit(_a);
unquoted = raw[ *(char_ - (eol | delimiter) ) ] [ _val = construct<boost::string_ref>(begin(_1), size(_1))]; //raw [ *(char_ - char_("\"',")) ] [ _val = construct<boost::string_ref>(begin(_1), size(_1)) ];

any_string = quoted | unquoted;
line = any_string % delimiter;
table = line % eol;

}

qi::rule<It, boost::string_ref() ,qi::locals<char> , qi::blank_type> any_string;
qi::rule<It, boost::string_ref() ,qi::locals<char> , qi::blank_type> quoted;
qi::rule<It, boost::string_ref() ,qi::locals<char> , qi::blank_type> unquoted;
qi::rule<It> delimiter;
qi::rule<It, std::vector<boost::string_ref>(), qi::blank_type> line;
qi::rule<It, MatrixType(), qi::blank_type, qi::locals<char>> table;

};

示例输入文件:

"a","b",   "c", "d,e,f"
"a", -1, abc, 0.1

实际的解析器添加一个,而不是现有的空行。文件末尾没有“\n”。

最佳答案

问题是行尾和输入结束是隐式定界符。

由于未加引号的字段允许为“空”(零长度),这将只解析包含单个空字段的最后一行。

我建议特别检查输入结束:

row = !eoi >> any_string % delimiter;

如果没有任何内容可读,行将被拒绝。为了宽容并允许尾随空行,您可以“吃掉”那些:

table = row % eol >> *eol;

最后,如果您还想“吃掉”表格行之间的空行,只需添加重复(kleene plus):

table = row % +eol >> *eol;

演示 Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/utility/string_ref.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;

using MatrixType = std::vector<std::vector<boost::string_ref>>;

template<typename It>
struct parser : qi::grammar<It, MatrixType(), qi::blank_type >
{
parser() : parser::base_type(table, "parser")
{
namespace px = boost::phoenix;
using namespace qi;

delimiter = ',';
quoted =
char_("'\"") [_a = _1]
>> raw [ *(char_ - char_(_a)) ] [ _val = px::construct<boost::string_ref>(px::begin(_1), px::size(_1)) ]
>> lit(_a);
unquoted = raw[ *(char_ - (eol | delimiter) ) ] [ _val = px::construct<boost::string_ref>(px::begin(_1), px::size(_1))];

any_string = quoted | unquoted;
row = !eoi >> any_string % delimiter;
table = row % +eol >> *eol;

BOOST_SPIRIT_DEBUG_NODES((delimiter)(quoted)(unquoted)(any_string)(row)(table))
}

private:
qi::rule<It, MatrixType(), qi::blank_type> table;
qi::rule<It, MatrixType::value_type(), qi::blank_type> row;

// lexemes
qi::rule<It, boost::string_ref(), qi::locals<char> > quoted;
qi::rule<It, boost::string_ref()> any_string, unquoted;
qi::rule<It> delimiter;
};

#include <fstream>
#include <boost/iostreams/device/mapped_file.hpp>

int main() {
using It = const char*;

boost::iostreams::mapped_file_source source("input.txt");
It first = source.begin();
It last = source.end();

parser<It> grammar;
MatrixType data;
bool ok = qi::phrase_parse(first, last, grammar, qi::blank, data);

if (ok) {
std::cout << "Parsed: \n";
for (auto& row : data)
{
for (auto& cell : row)
std::cout << cell << "|";
std::cout << "\n";
}
} else
{
std::cout << "Failed to parse\n";
}

if (first != last) {
std::cout << "Remaining input unparsed: '" << std::string(first, last) << "'\n";
}
}

打印:

Parsed: 
a|b|c|d,e,f|
a|-1|abc|0.1|
a||abc|0.1|

关于c++ - 使用 Spirit 引号和正常解析内存映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com