gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:54:00 25 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/

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