-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.cpp
31 lines (28 loc) · 1006 Bytes
/
helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <lib/helpers.hpp>
namespace Helpers {
std::string trim_base_indent(const std::string & source) {
std::deque<std::string> lines;
boost::split(lines, source, boost::is_any_of("\n"));
lines.pop_front();
lines.pop_back();
uint32_t min = 9999;
std::for_each(lines.begin(), lines.end(), [&min](const auto & a){
if (a.size() == 0) return;
std::regex re(R"(^ +)");
std::smatch m;
auto c = static_cast<uint32_t>(std::regex_search(a, m, re) ? m.str().size() : 0);
min = std::min(min, c);
});
std::for_each(lines.begin(), lines.end(), [&min](auto & a){
if (a.size() == 0) return;
auto aa = a.substr(min);
a.swap(aa);
});
return boost::algorithm::join(lines, "\n");
}
void times(uint32_t n, std::function<void(uint32_t)> callback) {
for (uint32_t i = 0; i < n; ++i) {
callback(i);
}
}
}