mirror of
https://github.com/rkd77/elinks.git
synced 2025-02-02 15:09:23 -05:00
[css2xpath] preg_replace_callback, based on coderedirect
This commit is contained in:
parent
983d3f990d
commit
9b30d73047
@ -4,6 +4,66 @@
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
#include <regex>
|
||||
#include <functional>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <regex>
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
template<class BidirIt, class Traits, class CharT, class UnaryFunction>
|
||||
std::basic_string<CharT> regex_replace2(BidirIt first, BidirIt last,
|
||||
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
|
||||
{
|
||||
std::basic_string<CharT> s;
|
||||
|
||||
typename std::match_results<BidirIt>::difference_type
|
||||
positionOfLastMatch = 0;
|
||||
auto endOfLastMatch = first;
|
||||
|
||||
auto callback = [&](const std::match_results<BidirIt>& match)
|
||||
{
|
||||
auto positionOfThisMatch = match.position(0);
|
||||
auto diff = positionOfThisMatch - positionOfLastMatch;
|
||||
|
||||
auto startOfThisMatch = endOfLastMatch;
|
||||
std::advance(startOfThisMatch, diff);
|
||||
|
||||
s.append(endOfLastMatch, startOfThisMatch);
|
||||
s.append(f(match));
|
||||
|
||||
auto lengthOfMatch = match.length(0);
|
||||
|
||||
positionOfLastMatch = positionOfThisMatch + lengthOfMatch;
|
||||
|
||||
endOfLastMatch = startOfThisMatch;
|
||||
std::advance(endOfLastMatch, lengthOfMatch);
|
||||
};
|
||||
|
||||
std::regex_iterator<BidirIt> begin(first, last, re), end;
|
||||
std::for_each(begin, end, callback);
|
||||
|
||||
s.append(endOfLastMatch, last);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<class Traits, class CharT, class UnaryFunction>
|
||||
std::string regex_replace2(const std::string& s,
|
||||
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
|
||||
{
|
||||
return regex_replace2(s.cbegin(), s.cend(), re, f);
|
||||
}
|
||||
|
||||
} // namespace std
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
std::string
|
||||
implode(const char *const delim, std::vector<std::string> & x)
|
||||
@ -43,8 +103,28 @@ preg_replace(std::string & pattern, const char *replacement, std::string & subje
|
||||
return std::regex_replace(subject, std::regex(pattern), replacement);
|
||||
}
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef std::string (*my_callback)(const std::smatch &m);
|
||||
|
||||
|
||||
std::string
|
||||
preg_replace_callback(std::string & pattern, my_callback callback, std::string & subject)
|
||||
{
|
||||
return std::regex_replace2(subject, std::regex(pattern), callback);
|
||||
}
|
||||
|
||||
#if 0
|
||||
#include <iostream>
|
||||
|
||||
std::string
|
||||
next_year(const std::smatch& matches)
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
||||
os << matches[1] << atoi(matches[2].str().c_str()) + 1;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
@ -66,11 +146,19 @@ main(int argc, char **argv)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
std::string str = "April 15, 2003";
|
||||
std::string pattern = "(\\w+) (\\d+), (\\d+)";
|
||||
const char *replacement = "$1,1,$3";
|
||||
std::cout << preg_replace(pattern, replacement, str);
|
||||
#endif
|
||||
|
||||
std::string text = "April fools day is 04/01/2002\n";
|
||||
text += "Last christmas was 12/24/2001\n";
|
||||
|
||||
std::string pat2 = "(\\d{2}/\\d{2}/)(\\d{4})";
|
||||
|
||||
std::cout << preg_replace_callback(pat2, next_year, text);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user