كيف يمكنني تحليل نهاية الخط مع دفعة :: الروح :: تشي؟

StackOverflow https://stackoverflow.com/questions/2429485

سؤال

لا ينبغي أن بسيط eol قم بالخدعة؟

#include <algorithm>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
using boost::spirit::ascii::space;
using boost::spirit::lit;
using boost::spirit::qi::eol;
using boost::spirit::qi::phrase_parse;

struct fix : std::unary_function<char, void> {
  fix(std::string &result) : result(result) {}
  void operator() (char c) {
    if      (c == '\n') result += "\\n";
    else if (c == '\r') result += "\\r";
    else                result += c;
  }
  std::string &result;
};

template <typename Parser>
void parse(const std::string &s, const Parser &p) {
  std::string::const_iterator it = s.begin(), end = s.end();
  bool r = phrase_parse(it, end, p, space);
  std::string label;
  fix f(label);
  std::for_each(s.begin(), s.end(), f);
  std::cout << '"' << label << "\":\n" << "  - ";
  if (r && it == end) std::cout << "success!\n";
  else std::cout << "parse failed; r=" << r << '\n';
}

int main() {
  parse("foo",     lit("foo"));
  parse("foo\n",   lit("foo") >> eol);
  parse("foo\r\n", lit("foo") >> eol);
}

انتاج:

"فو": - النجاح! "foo  n": - فشلت التحليل؛ R = 0 "foo  r  n": - فشل التحليل؛ ص = 0.

لماذا يفشل الأخيران؟


سؤال ذو الصلة:

باستخدام دفعة :: الروح، كيف أحتاج إلى جزء من سجل ليكون على خطها الخاص؟

هل كانت مفيدة؟

المحلول

انت تستخدم space ك Skipper لإجراء مكالماتك إلى There_Parse. هذا المحلل يطابق أي شخصية التي std::isspace إرجاع صحيح (على افتراض أنك تقوم بتحليل ASCII القائم). لهذا السبب \r\n في المدخلات تؤكل من قبل Skipper قبل أن يتم رؤيتك من قبل eol المحلل المحلل

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top