peilaus alkaen
https://github.com/quitesimpleorg/qsmaddy.git
synced 2024-06-18 02:16:13 +02:00
37 rivejä
916 B
C
37 rivejä
916 B
C
|
/*
|
||
|
* This project is licensed under the MIT license. For more information see the
|
||
|
* LICENSE file.
|
||
|
*/
|
||
|
#pragma once
|
||
|
|
||
|
#include <string>
|
||
|
#include <regex>
|
||
|
#include <functional>
|
||
|
|
||
|
namespace maddy {
|
||
|
|
||
|
inline std::string regex_callback_replacer(std::regex ®ex, const std::string &input,
|
||
|
std::function<std::string(std::smatch &)> &callback)
|
||
|
{
|
||
|
std::string result;
|
||
|
auto tagsbegin = std::sregex_iterator(input.begin(), input.end(), regex);
|
||
|
auto tagsend = std::sregex_iterator();
|
||
|
auto matchbegin = 0;
|
||
|
for(std::sregex_iterator i = tagsbegin; i != tagsend; ++i)
|
||
|
{
|
||
|
std::smatch match = *i;
|
||
|
|
||
|
auto matchlength = match.length(0);
|
||
|
auto matchpos = match.position();
|
||
|
|
||
|
result += input.substr(matchbegin, matchpos - matchbegin);
|
||
|
result += callback(match);
|
||
|
matchbegin = matchpos + matchlength;
|
||
|
}
|
||
|
result += input.substr(matchbegin);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|