Boost::qi 解析字符串

Boost::qi parse string

我需要从下一个 hls 标签中解析“title”

标签样式: #EXTINF:<持续时间>[<标题>]

真实标签示例:

#EXTINF:10,Title of the segment => I need "Title of the segment" phrase

#EXTINF:20,Title => I need "Title" phrase

#EXTINF:12 => I need "" phrase

我写了下一个代码

double duration;
std::string title;

boost::spirit::qi::rule<Iterator, std::string()> quoutedString;
quoutedString %= lexeme[+(char_)];

bool r = parse(first, last,
    ("#EXTINF:" >> double_[ref(duration) = _1] >> -(',' >> quoutedString[ref(title) = _1] ) )
);
if (!r || first != last) {
    addMinorProblem(stateObj, _("Cannot parse information from #EXTINF tag"));
    return false;
}

但是我在编译过程中遇到了下一个错误:

error: call of overloaded ‘ref(std::__cxx11::string&)’ is ambiguous
         ("#EXTINF:" >> double_[ref(duration) = _1] >> -(',' >> quoutedString[ref(title) = _1] ) )

请帮助我。我做错了什么?

您过多地使用了命名空间。此外,无论如何,ADL 都会为 std::string 参数引入 std::ref,除非 ref 被加括号或命名空间限定。

过度使用 using namespace 从来都不是一个好主意(参见 Why is "using namespace std;" considered bad practice?),在这种情况下,消息说明了 std::ref 和 [=20= 之间的混淆](可能还有其他人,但您没有包含完整的消息)。

直接说不:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/optional.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;

#define addMinorProblem(...) do {} while (0)

boost::optional<std::pair<double, std::string>> parse(std::string_view input)
{
    using Iterator = std::string_view::const_iterator;

    Iterator    first = begin(input), last = end(input);
    double      duration;
    std::string title;

    boost::spirit::qi::rule<Iterator, std::string()> quoutedString;
    quoutedString %= qi::lexeme[+(qi::char_)];

    bool r = parse(first, last,
                   ("#EXTINF:" >> qi::double_[px::ref(duration) = qi::_1] >>
                    -(',' >> quoutedString[px::ref(title) = qi::_1])));
    if (!r || first != last) {
        addMinorProblem(stateObj,
                        _("Cannot parse information from #EXTINF tag"));
        return {};
    }

    return std::make_pair(duration, title);
}

int main()
{
    for (std::string const input : {
             "#EXTINF:10,Title of the segment", // => I need "Title of the
                                                // segment" phrase
             "#EXTINF:20,Title",                // => I need "Title" phrase
             "#EXTINF:12",                      // => I need "" phrase
         }) {
        if (auto result = parse(input)) {
            std::cout << "Parsed: (" << result->first << ", " << std::quoted(result->second) << ")\n";
        } else {
            std::cout << "Cannot parse " << std::quoted(input) << "\n";
        }
    }
}

版画

Parsed: (10, "Title of the segment")
Parsed: (20, "Title")
Parsed: (12, "")

改进,小事

  1. 通过一些明智的 local 使用声明,您可以再次使其“更短”:

    using namespace qi::labels;
    using px::ref;
    bool r = parse(first, last,
                   ("#EXTINF:" >> qi::double_[ref(duration) = _1] >>
                    -(',' >> quoutedString[(ref)(title) = _1])));
    

    我个人觉得这比清楚更晦涩(想象一下你自己在代码审查中解释 (ref)(title)?)

  2. operator %=没有语义动作就没有意义

  3. qi::lexeme[]没有船长就没有意义(见Boost spirit skipper issues

  4. quoutedString [原文如此] 是用词不当(现在?)因为它不解析引号

  5. 为什么不使用自动属性传播来代替痛苦的语义操作?它们只会增加编译时间,而且正如您所发现的那样,还会增加开发时间。参见 Boost Spirit: "Semantic actions are evil"?

  6. 此外,与其在解析后繁琐地检查 first == last,不如在表达式中匹配 >> qi::eoi?

以上所有简化为:

Live On Coliru

boost::optional<std::pair<double, std::string>> parse(std::string_view input)
{
    namespace qi = boost::spirit::qi;
    double duration;
    std::string title;

    if (qi::parse(
        begin(input), end(input), //
        ("#EXTINF:" >> qi::double_ >> -(',' >> +qi::char_) >> qi::eoi),
        duration, title))
    {
        return std::make_pair(duration, title);
    }

    addMinorProblem(stateObj, _("Cannot parse information from #EXTINF tag"));
    return {};
}

不再有phoenix,语义动作,什么都没有。仍在打印:

Parsed: (10, "Title of the segment")
Parsed: (20, "Title")
Parsed: (12, "")