boost::spirit::karma output of string in quotation marks

此生再无相见时 提交于 2019-12-02 02:18:45

If you iterate your data types, you should iterate your rules.

#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;
}

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)

Hmm. It looks like you might have been after a recursive attribute/rule:

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

Just in that case, here's a simple approach to generating that:

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

Now, your title suggests that strings containing double-quotes should escape these. I suggest

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

Now the following test cases

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

result in:

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

If recursion is really a feature, you'd want to see the grouping of nested Parameter lists:

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

Now prints

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

Full sample program:

#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';
    }
}
#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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!