8

のフォーマット関数に問題がありますboost::regex_replace。1つのパラメーターバージョンを呼び出すことはできますが、2つのパラメーターを呼び出すことはできません。

e = "(^|>)([^<>]+)";
h_str = regex_replace(h_str, e, repl_quot, boost::match_default);

repl_quotとして定義されている場所

std::string const &repl_quot(boost::smatch const &what) {
    boost::regex e("\"");
    std::string  repl("&#34;");
    static std::string;
    str = regex_replace(what[0].str(), e, repl, boost::match_default);
    return str;
}

上記は機能しますが、その静的変数を実際には使用したくないので、許容できる2つのパラメーターの代替バージョンであると思ったものを試しました。

std::string const &repl_quot2(boost::smatch const &what, std::string &out) {
    boost::regex e("\"");
    std::string  repl("&#34;");
    out = regex_replace(what[0].str(), e, repl, boost::match_default);
    return out;
}

ただし、regex_replaceはこれを受け入れません(複雑なコンパイラエラー)。Boost::Regexドキュメントの以下に基づいて、2つのパラメータバージョンを使用しようとしています。

テンプレートbasic_stringregex_replace(const basic_string&s、const basic_regex&e、Formatter fmt、match_flag_type flags = match_default);

必須タイプフォーマッタは、関数呼び出しから置換文字列を計算する単項、バイナリ、または三項ファンクタのいずれかである必要があります。置換テキストとして使用されるchar_typeのコンテナを返す必要があるfmt(what)、またはfmtのいずれかです。 (what、out)またはfmt(what、out、flags)。どちらも置換テキストを* outに書き込み、新しいOutputIterator位置を返します。いずれの場合も、見つかった一致を表すmatch_resultsオブジェクトは何ですか。

コンパイラエラーメッセージの要求が繰り返されているので、ここにあります(要求する内容に注意してください)。

c:\ boost \ boost \ regex \ v4\regex_format.hppメンバー関数`OutputIterboost :: re_detail :: format_functor_container :: operator()(const Match&、OutputIter、boost :: regex_constants :: match_flag_type、const Traits&)[with OutputIter = boost :: re_detail :: string_out_iterator、std :: allocator >>、Container = const std :: string&(*)(const boost :: smatch&、std :: string&)、Match = boost :: match_results <__ gnu_cxx :: __normal_iterator、std :: allocator >>、std :: allocator、std :: allocator >> >> >>、Traits = boost :: regex_traits_wrapper >>]':

356 c:\ boost \ boost \ regex \ v4 \ match_results.hppは、 `OutputIterator boost :: match_results :: format(OutputIterator、Functor、boost :: regex_constants :: match_flag_type、const RegexT&)const [with OutputIterator = boost :: re_detail :: string_out_iterator、std :: allocator >>、Functor = const std :: string&(*)(const boost :: smatch&、std :: string&)、RegexT = boost :: basic_regex >>、BidiIterator = __gnu_cxx :: __ normal_iterator 、std :: allocator >>、Allocator = std :: allocator、std :: allocator >> >>] '

60 c:\ boost \ boost \ regex \ v4 \ regex_replace.hppは、 `OutputIterator boost :: regex_replace(OutputIterator、BidirectionIterator、BidirectionIterator、const boost :: basic_regex&、Formatter、boost :: regex_constants :: match_flag_type)[with OutputIterator boost :: re_detail :: string_out_iterator、std :: allocator >>、BidirectionIterator = __gnu_cxx :: __ normal_iterator、std :: allocator >>、traits = boost :: regex_traits>、charT = char、Formatter = const std :: string&(* )(const boost :: smatch&、std :: string&)] '

80 c:\ boost \ boost \ regex \ v4 \ regex_replace.hppは、 `std :: basic_string、std :: allocator <_T2>> boost :: regex_replace(const std :: basic_string、std :: allocator<_T2>>からインスタンス化されました&、const boost :: basic_regex&、Formatter、boost :: regex_constants :: match_flag_type)[with traits = boost :: regex_traits>、charT = char、Formatter = const std :: string&(*)(const boost :: smatch&、std ::ストリング&)]'

327 C:\ Dev-Cpp \ Examples \ wordrad\xhtml_open.cppはここからインスタンス化されます

1064 c:\ boost \ boost \ regex \ v4 \ regex_format.hppメンバーのリクエストbegin' in((boost :: re_detail :: format_functor_container、std :: allocator >>、std :: allocator、std :: allocator >> >> >>、 boost :: regex_traits_wrapper >>> *)this)-> boost :: re_detail :: format_functor_container、std :: allocator >>、std :: allocator、std :: allocator >> >> >>、boost :: regex_traits_wrapper >> >> :: func'、これは非クラスタイプ `const std :: string&(* const)(const boost :: smatch&、std :: string&)'

1064 c:\ boost \ boost \ regex \ v4 \ regex_format.hppメンバーのリクエストend' in((boost :: re_detail :: format_functor_container、std :: allocator >>、std :: allocator、std :: allocator >> >> >>、 boost :: regex_traits_wrapper >>> *)this)-> boost :: re_detail :: format_functor_container、std :: allocator >>、std :: allocator、std :: allocator >> >> >>、boost :: regex_traits_wrapper >> >> :: func'、これは非クラスタイプ `const std :: string&(* const)(const boost :: smatch&、std :: string&)'

4

1 に答える 1

1

OKこれが私がrepl_quot2を書かなければならなかった方法です:

struct formatter
{       
  template<typename Out>
  Out operator()(boost::smatch const &what, Out out) const {
    boost::regex e("\"");    
    std::string  repl("&#34;");
    std::string str
      = regex_replace(what[0].str(), e, repl, boost::match_default);
    out = std::copy(str.begin(), str.end(), out);
    return out;
  }

};

そして、regex_replaceから呼び出す場合:

  e = "(^|>)[^<>]+";
  formatter repl_quot2;
  h_str = regex_replace(h_str, e, repl_quot2, boost::match_default);

これは、 http://boost-sandbox.sourceforge.net/libs/xpressive/doc/html/boost_xpressive/user_s_guide/string_substitutions.htmlのドキュメントに対応しています。

現時点で私を困惑させているのは、2つのパラメーターバージョンが呼び出された場合、関数ではなくファンクター(()演算子を持つクラス)が必要ですが、1つのパラメーターバージョン(OPのrepl_quot)にはファンクターが必要ないことです。 )。とにかく、ストレート関数として機能する新しい2つのparmバージョンを取得していません。しかし、主な問題はout、OPのようにstd :: stringにするのではなく、示されているようにテンプレートパラメータとして渡して返す必要があることでした。それはOutputIteratorであるはずです-それが実際に何であるかはまだわかりません。

ちなみに、この正規表現が行うのは、二重引用符をhtmlエンティティバージョン\&#34;に置き換えることだけです。タグの一部ではないhtmlのテキスト。

また、元の関数repl_quotを置き換えたいと思ったのは、戻り値をrepl_quotの静的ローカル変数に格納する必要があったためです。通常のローカル変数を返すだけでは機能しません。使用する前に割り当てを解除できるためです(クラッシュの原因にもなります)。repl_quotは機能していました-静的に関する私の問題は、スレッドセーフではなく、Boost::RegExがマルチスレッドであるかどうかを知らなかったことです。マルチスレッドとしてコンパイルしたと思いますが、静的変数は問題を引き起こさなかったようです。しかし、repl_quot2を使用して、戻り値を出力パラメーターに書き込みます。

于 2011-01-30T12:29:12.453 に答える