summaryrefslogtreecommitdiff
path: root/util.hpp
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-10-05 02:09:15 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2020-10-05 02:09:15 -0400
commitcfcb0d4e662b960dcdd331a7c61dc2b9c614f930 (patch)
treeef73894f8662a4925160d7126b01b20a896577f7 /util.hpp
parentb7dd1fd5b0cf8c5a6a9c191109112f887cc7ac36 (diff)
downloadabaddon-portaudio-cfcb0d4e662b960dcdd331a7c61dc2b9c614f930.tar.gz
abaddon-portaudio-cfcb0d4e662b960dcdd331a7c61dc2b9c614f930.zip
basic mention parsing
Diffstat (limited to 'util.hpp')
-rw-r--r--util.hpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/util.hpp b/util.hpp
index d9fa8d5..2c3c3b9 100644
--- a/util.hpp
+++ b/util.hpp
@@ -184,3 +184,39 @@ inline double ColorDistance(int c1, int c2) {
int b = b1 - b2;
return sqrt((((512 + rmean) * r * r) >> 8) + 4 * g * g + (((767 - rmean) * b * b) >> 8));
}
+
+// somehow this works
+template<typename F>
+std::string RegexReplaceMany(std::string str, std::string regexstr, F func) {
+ std::regex reg(regexstr, std::regex_constants::ECMAScript);
+ std::smatch match;
+
+ std::vector<std::tuple<int, int, std::string, int>> matches;
+
+ std::string::const_iterator sstart(str.cbegin());
+ while (std::regex_search(sstart, str.cend(), match, reg)) {
+ const auto start = std::distance(str.cbegin(), sstart) + match.position();
+ const auto end = start + match.length();
+ matches.push_back(std::make_tuple(static_cast<int>(start), static_cast<int>(end), match[1].str(), static_cast<int>(match.str().size())));
+ sstart = match.suffix().first;
+ }
+
+ int offset = 0;
+ for (auto it = matches.begin(); it != matches.end(); it++) {
+ const auto start = std::get<0>(*it);
+ const auto end = std::get<1>(*it);
+ const auto match = std::get<2>(*it);
+ const auto full_match_size = std::get<3>(*it);
+ const auto replacement = func(match);
+ const auto diff = full_match_size - replacement.size();
+
+ str = str.substr(0, start) + replacement + str.substr(end);
+
+ offset += diff;
+
+ std::get<0>(*(it + 1)) -= offset;
+ std::get<1>(*(it + 1)) -= offset;
+ }
+
+ return str;
+}