所以,除了我 8 小时前发表的评论之外:
也许相关:使用 Boost.Spirit 编译一个简单的解析器 https://stackoverflow.com/questions/9404558/compiling-a-simple-parser-with-boost-spirit/9405546#9405546, 更换字符串片段 https://stackoverflow.com/questions/17241897/replacing-pieces-of-string/17243219#17243219, 如何使用 Boost 扩展 .ini 文件中的环境变量 https://stackoverflow.com/questions/17112494/how-to-expand-environment-variables-in-ini-files-using-boost/17126962#17126962也许最有趣的是快速多重替换为字符串 https://stackoverflow.com/questions/22571578/fast-multi-replacement-into-string/22571753#22571753
我看到了还可以采用另一种方法的空间。如果...您需要基于相同的文本模板进行多次替换,但使用不同的替换地图怎么办?
因为我最近发现了如何Boost ICL 可用于映射输入字符串的区域 https://stackoverflow.com/questions/28397514/randomly-selecting-specific-subsequence-from-string/28401533?s=1%7C0.0000#28401533,我想在这里做同样的事情。
我让事情变得非常通用,并使用 Spirit 来进行分析(study
):
template <
typename InputRange,
typename It = typename boost::range_iterator<InputRange const>::type,
typename IntervalSet = boost::icl::interval_set<It> >
IntervalSet study(InputRange const& input) {
using std::begin;
using std::end;
It first(begin(input)), last(end(input));
using namespace boost::spirit::qi;
using boost::spirit::repository::qi::seek;
IntervalSet variables;
parse(first, last, *seek [ raw [ "{$" >> +alnum >> "}" ] ], variables);
return variables;
}
正如你所看到的,我们没有进行任何替换,而是返回一个interval_set<It>
所以我们知道变量在哪里。现在,这是可用于从替换字符串映射执行替换的“智慧”:
template <
typename InputRange,
typename Replacements,
typename OutputIterator,
typename StudyMap,
typename It = typename boost::range_iterator<InputRange const>::type
>
OutputIterator perform_replacements(InputRange const& input, Replacements const& m, StudyMap const& wisdom, OutputIterator out)
{
using std::begin;
using std::end;
It current(begin(input));
for (auto& replace : wisdom)
{
It l(lower(replace)),
u(upper(replace));
if (current < l)
out = std::copy(current, l, out);
auto match = m.find({l+2, u-1});
if (match == m.end())
out = std::copy(l, u, out);
else
out = std::copy(begin(match->second), end(match->second), out);
current = u;
}
if (current!=end(input))
out = std::copy(current, end(input), out);
return out;
}
现在,一个简单的测试程序将如下所示:
int main()
{
using namespace std;
string const input = "This {$oops} is an {$severity} {$experience}!\n";
auto const wisdom = study(input);
cout << "Wisdom: ";
for(auto& entry : wisdom)
cout << entry;
auto m = map<string, string> {
{ "severity", "absolute" },
{ "OOPS", "REALLY" },
{ "experience", "nightmare" },
};
ostreambuf_iterator<char> out(cout);
out = '\n';
perform_replacements(input, m, wisdom, out);
// now let's use a case insensitive map, still with the same "study"
map<string, string, ci_less> im { m.begin(), m.end() };
im["eXperience"] = "joy";
perform_replacements(input, im, wisdom, out);
}
Prints
Wisdom: {$oops}{$severity}{$experience}
This {$oops} is an absolute nightmare!
This REALLY is an absolute joy!
您可以使用输入字符串文字来调用它unordered_map
用于更换等。您可以省略wisdom
,在这种情况下,实现将即时研究它。
完整计划
Live On Coliru http://coliru.stacked-crooked.com/a/e3a03d5364f0b9a4
#include <iostream>
#include <map>
#include <boost/regex.hpp>
#include <boost/icl/interval_set.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>
namespace boost { namespace spirit { namespace traits {
template <typename It>
struct assign_to_attribute_from_iterators<icl::discrete_interval<It>, It, void> {
template <typename ... T> static void call(It b, It e, icl::discrete_interval<It>& out) {
out = icl::discrete_interval<It>::right_open(b, e);
}
};
} } }
template <
typename InputRange,
typename It = typename boost::range_iterator<InputRange const>::type,
typename IntervalSet = boost::icl::interval_set<It> >
IntervalSet study(InputRange const& input) {
using std::begin;
using std::end;
It first(begin(input)), last(end(input));
using namespace boost::spirit::qi;
using boost::spirit::repository::qi::seek;
IntervalSet variables;
parse(first, last, *seek [ raw [ "{$" >> +alnum >> "}" ] ], variables);
return variables;
}
template <
typename InputRange,
typename Replacements,
typename OutputIterator,
typename StudyMap,
typename It = typename boost::range_iterator<InputRange const>::type
>
OutputIterator perform_replacements(InputRange const& input, Replacements const& m, StudyMap const& wisdom, OutputIterator out)
{
using std::begin;
using std::end;
It current(begin(input));
for (auto& replace : wisdom)
{
It l(lower(replace)),
u(upper(replace));
if (current < l)
out = std::copy(current, l, out);
auto match = m.find({l+2, u-1});
if (match == m.end())
out = std::copy(l, u, out);
else
out = std::copy(begin(match->second), end(match->second), out);
current = u;
}
if (current!=end(input))
out = std::copy(current, end(input), out);
return out;
}
template <
typename InputRange,
typename Replacements,
typename OutputIterator,
typename It = typename boost::range_iterator<InputRange const>::type
>
OutputIterator perform_replacements(InputRange const& input, Replacements const& m, OutputIterator out) {
return perform_replacements(input, m, study(input), out);
}
// for demo program
#include <boost/algorithm/string.hpp>
struct ci_less {
template <typename S>
bool operator() (S const& a, S const& b) const {
return boost::lexicographical_compare(a, b, boost::is_iless());
}
};
namespace boost { namespace icl {
template <typename It>
static inline std::ostream& operator<<(std::ostream& os, discrete_interval<It> const& i) {
return os << make_iterator_range(lower(i), upper(i));
}
} }
int main()
{
using namespace std;
string const input = "This {$oops} is an {$severity} {$experience}!\n";
auto const wisdom = study(input);
cout << "Wisdom: ";
for(auto& entry : wisdom)
cout << entry;
auto m = map<string, string> {
{ "severity", "absolute" },
{ "OOPS", "REALLY" },
{ "experience", "nightmare" },
};
ostreambuf_iterator<char> out(cout);
out = '\n';
perform_replacements(input, m, wisdom, out);
// now let's use a case insensitive map, still with the same "study"
map<string, string, ci_less> im { m.begin(), m.end() };
im["eXperience"] = "joy";
perform_replacements(input, im, wisdom, out);
}
就地操作
只要确保替换字符串始终比替换字符串短{$pattern}
字符串(或等长),您可以简单地调用此函数input.begin()
作为输出迭代器。
Live On Coliru http://coliru.stacked-crooked.com/a/b4fbf351df4a158a
string input1 = "This {$803525c8-3ce4-423a-ad25-cc19bbe8422a} is an {$efa72abf-fe96-4983-b373-a35f70551e06} {$8a10abaa-cc0d-47bd-a8e1-34a8aa0ec1ef}!\n",
input2 = input1;
auto m = map<string, string> {
{ "efa72abf-fe96-4983-b373-a35f70551e06", "absolute" },
{ "803525C8-3CE4-423A-AD25-CC19BBE8422A", "REALLY" },
{ "8a10abaa-cc0d-47bd-a8e1-34a8aa0ec1ef", "nightmare" },
};
input1.erase(perform_replacements(input1, m, input1.begin()), input1.end());
map<string, string, ci_less> im { m.begin(), m.end() };
im["8a10abaa-cc0d-47bd-a8e1-34a8aa0ec1ef"] = "joy";
input2.erase(perform_replacements(input2, im, input2.begin()), input2.end());
std::cout << input1
<< input2;
Prints
This {$803525c8-3ce4-423a-ad25-cc19bbe8422a} is an absolute nightmare!
This REALLY is an absolute joy!
请注意,您(显然)不能在同一输入模板上再次重复使用相同的“智慧”,因为它已经被修改了。