BOOST 正则表达式 - u32regex_replace() 没有带有回调函数的原型

BOOST regex - no prototype for u32regex_replace() with callback function

正在使用 Boost Regex v1.56 from VS2010 platform/C++/Mfc

我正在将一些 C++ ANSI 转换为 Unicode。
我将 regex_replace() 与仿函数回调 (Formatter fmt) 一起使用。

我找不到使用 u32regex_replace() 的回调函数。
icu.hpp 中似乎没有类似的模板。

我宁愿不必使用 u32regex_search() 来模拟替换。 任何人都知道 u32 对应方是否存在回调功能 regex_replace

注意 - Boost 正则表达式文档在 u32 等价物上说明了这一点:

For each regex_replace algorithm defined by <boost/regex.hpp>, then <boost/regex/icu.hpp> defines an overloaded algorithm that takes the same arguments, but which is called u32regex_replace, and which will accept UTF-8, UTF-16 or UTF-32 encoded data, as well as an ICU UnicodeString as input. The input sequence and the format string specifier passed to the algorithm, can be encoded independently (for example one can be UTF-8, the other in UTF-16), but the result string / output iterator argument must use the same character encoding as the text being searched.

版本控制来拯救!

2009 年 12 月 8 日 regex_replace.hpp shows that the template Formatter was added with commit ae79f29 HEAD 版本的快速 git blame,即 Boost 版本 1.42 中的新内容。

2005 年 1 月 13 日 icu.hpp on the other hand, have not been changed since commit 71a0e02u32regex_replace 的模板定义,即 v.1.33.

基于此我们必须得出结论,格式化时对函数对象和字符串的支持从未进入 u32 对应函数,不幸的是,您此时运气不佳。

boost.org 提供 this information 关于报告和修复错误的信息。

我必须通过解决方法来回答我自己的问题。
有点想 u32regex_replace 的回调不可用,因为我找不到它。

在查看 regex_replace 和 icu.cpp 中的 u32 内容后,
很明显,他对几乎所有内容都使用 regex_iterator

所以,这几乎会复制 regex_replace 和 Formatter fmt
函子。

 // Ficticous formatter string.
 std::wstring sReplace = _T( "" );

 // Callback Functor.
 std::wstring Callback( const boost::wsmatch m )
 {
    // Do stuff here, thats why its a callbck !! 
    return m.format( sReplace );
 }

 // ------------------------------------------

 // Ficticous test regex
 boost::u32regex Regex = make_u32regex( _T("(?<=(\w))(?=(\w))") ));

 // Create a u32regex_iterator via make_u32regex_iterator
 // 
 boost::u32regex_iterator<std::wstring::const_iterator>
    i(boost::make_u32regex_iterator( sInput, Regex)), j;

 // Ficticous input string.
 std::wstring sInput = _T( "This is a sentence"" );

 // Maintain a last iterator to use for the ending.
 std::wstring::const_iterator last = sInput.begin();

 // Clear the output string
 sOutput = _T("");

 // Do global replace with callback.
 while(i != j)
 {
    sOutput.append( (*i).prefix() );      // append last match to here
    sOutput.append( Callback( (*i) ) ) ;  // append callback string
    last = (*i)[0].second;                // save to 'last' the end of this match
    ++i;
 }

 // Append any trailing text.
 sOutput.append( last, stext.end() );