我想解析一个句子,其中一些字符串可能不加引号,"引用"或"引用".下面的代码几乎可以工作 - 但它无法匹配收尾报价.我猜这是因为qq参考.修改在代码中被注释,修改引用"引用"或"引用"也解析并帮助显示原始问题与结束引用.该代码还描述了确切的语法.
要完全清楚:不带引号的字符串解析.引用的字符串'hello'将解析打开的引号',所有字符 hello,但然后无法解析最终引用'.
我做了另一次尝试,类似于boost教程中的开始/结束标记匹配,但没有成功.
template <typename Iterator>
struct test_parser : qi::grammar<Iterator, dectest::Test(), ascii::space_type>
{
test_parser()
:
test_parser::base_type(test, "test")
{
using qi::fail;
using qi::on_error;
using qi::lit;
using qi::lexeme;
using ascii::char_;
using qi::repeat;
using namespace qi::labels;
using boost::phoenix::construct;
using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::val;
using boost::phoenix::ref;
using qi::space;
char qq;
arrow = lit("->");
open_quote = (char_('\'') | char_('"')) [ref(qq) = _1]; // Remember what the opening quote was
close_quote = lit(val(qq)); …Run Code Online (Sandbox Code Playgroud) 所以我们有一个简单的分裂:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
vector<string> result;
if (delim.empty()) {
result.push_back(s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
当我从C ++(11)中读取文件时,我使用以下命令将它们映射到内存中:
boost::interprocess::file_mapping* fm = new file_mapping(path, boost::interprocess::read_only);
boost::interprocess::mapped_region* region = new mapped_region(*fm, boost::interprocess::read_only);
char* bytes = static_cast<char*>(region->get_address());
Run Code Online (Sandbox Code Playgroud)
当我希望非常快地逐字节读取时,这很好。但是,我创建了一个csv文件,该文件要映射到内存,读取每一行并在逗号上分割每一行。
是否可以通过对上面的代码进行一些修改来做到这一点?
(我正在映射到内存,因为我有很多内存,并且我不希望磁盘/ IO流出现任何瓶颈)。