gpt4 book ai didi

引号中字符串的 boost::spirit::karma 输出

转载 作者:行者123 更新时间:2023-12-02 04:55:18 34 4
gpt4 key购买 nike

我正在尝试使用 boost::spirit::karma 转义引号中的字符串。如果它只是一个字符串,这很好用。但是,对于 std::vector 中的 boost::variant 中的字符串,它不会。只是打印字符串确实有效,但我不太明白为什么。

第 (1) 行工作正常,但不符合我的要求。第 (2) 行应该这样做,但实际上没有。

#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;

typedef std::vector<boost::variant<int, std::string>> ParameterList;
typedef boost::variant<int, std::string, ParameterList> Parameter;

main()
{
using karma::int_;
using boost::spirit::ascii::string;
using karma::eol;
using karma::lit;

std::string generated;
std::back_insert_iterator<std::string> sink(generated);

// (1)
karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = (int_ | string) % lit(", "); // This works!

// (2)
//karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = (int_ | (lit('"') << string << lit('"'))) % lit(", "); // This does not work

karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | (lit('"') << string << lit('"')) | parameterListRule) << eol; // This does work, even though it also escapes the string in a pair of quotation marks

karma::generate(sink, parameterRule, 1); // Works
karma::generate(sink, parameterRule, "foo"); // Works
karma::generate(sink, parameterRule, Parameter(ParameterList{1, "foo"})); // Only works using rule (1), not with (2)
std::cout << generated;
}

最佳答案

Edited In case recursion was not the goal, here's an edited version that solves the issue and the quote escaping: Live on Coliru (or just source here)

嗯。看起来您可能一直在寻找递归属性/规则:

typedef boost::make_recursive_variant<int, std::string, std::vector<boost::recursive_variant_> >::type Parameter;

在这种情况下,这里有一个简单的生成方法:

gen = int_ | string | gen % ", ";

现在,您的标题建议包含双引号的字符串应该转义。我建议

str = '"' << *('\\' << char_('"') | char_) << '"';
gen = int_ | str | gen % ", ";

现在下面的测试用例

for (Parameter p : Parameters { 
1,
"foo",
Parameters { 1, "foo" },
Parameters { 1, "escape: \"foo\"", Parameters { "2", "bar" } }
})
{
std::cout << karma::format(gen, p) << '\n';
}

结果:

1
"foo"
1, "foo"
1, "escape: \"foo\"", "2", "bar"

如果递归真的是一个特性,你会希望看到嵌套参数列表的分组:

gen = int_ | str | '{' << gen % ", " << '}';

现在打印

1
"foo"
{1, "foo"}
{1, "escape: \"foo\"", {"2", "bar"}}

完整示例程序:

#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>

namespace karma = boost::spirit::karma;
typedef boost::make_recursive_variant<int, std::string, std::vector<boost::recursive_variant_> >::type Parameter;
typedef std::vector<Parameter> Parameters;

int main()
{
typedef boost::spirit::ostream_iterator It;

karma::rule<It, Parameter()> gen;
karma::rule<It, std::string()> str;

str = '"' << *('\\' << karma::char_('"') | karma::char_) << '"';
gen = (karma::int_ | str | '{' << gen % ", " << '}');

for (Parameter p : Parameters {
1,
"foo",
Parameters { 1, "foo" },
Parameters { 1, "escape: \"foo\"", Parameters { "2", "bar" } }
})
{
std::cout << karma::format(gen, p) << '\n';
}
}

关于引号中字符串的 boost::spirit::karma 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101509/

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