의 미러
https://github.com/quitesimpleorg/qsmaddy.git
synced 2024-06-16 09:36:14 +02:00
Albert S
e0e0db80d5
In particular, this allows making dynamic decisions: Some links may for example require a different CSS class or so, therefore a static hardcoded template is not enough for those cases.
37 lines
916 B
C++
37 lines
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;
|
|
}
|
|
|
|
}
|
|
|