2

我正在尝试使用 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;
}
4

3 回答 3

3

已编辑如果递归不是目标,这是一个解决问题和引用转义的编辑版本:Live on Coliru 只是来源

唔。看起来您可能一直在使用递归属性/规则:

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';
    }
}
于 2013-08-07T11:59:14.783 回答
2

如果你迭代你的数据类型,你应该迭代你的规则。

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

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

int 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);

  karma::rule<std::back_insert_iterator<std::string>, Item()> itemRule =
      int_ | (lit('"') << string << lit('"'));

  karma::rule<std::back_insert_iterator<std::string>, ParameterList()>
    parameterListRule =  itemRule % lit(", ");

  karma::rule<std::back_insert_iterator<std::string>, Parameter()>
    parameterRule = (int_ | (lit('"') << string << lit('"')) | parameterListRule) << eol;

  karma::generate(sink, parameterRule, 1);
  karma::generate(sink, parameterRule, "foo");
  karma::generate(sink, parameterRule, Parameter(ParameterList {1, "foo"}));
  std::cout << generated;

  return 0;
}
于 2013-08-07T11:05:33.303 回答
0
#include <iostream>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_right_alignment.hpp>

using namespace boost;

void foo(char* buffer, uint32_t lhOid) {
    boost::spirit::karma::generate(buffer, boost::spirit::right_align(20)[boost::spirit::karma::int_], lhOid);
    *buffer = '\0';
}

int main() {
    char arr[21];
    foo(arr, 1234);
    std::cout.write(arr, 21) << std::endl;
    return 0;
}
于 2017-01-05T09:45:08.393 回答