-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.h
73 lines (63 loc) · 1.92 KB
/
Utils.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <algorithm>
#include <filesystem>
#include <initializer_list>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
inline auto ToLower(std::string str) {
std::transform(
str.begin(), str.end(), str.begin(),
[](auto c) { return std::tolower(c); });
return str;
}
template <class T>
void SortStrings(T&& strs) {
std::sort(strs.begin(), strs.end(), [](const auto& a, const auto& b) {
return std::strcoll(a.c_str(), b.c_str()) < 0;
});
}
template <class T>
auto JoinStringsImpl(T&& strs, const std::string& separator) -> std::string {
std::string result;
for (const auto& str : strs) {
if (!str.empty()) {
if (!result.empty()) {
result += separator;
}
result += str;
}
}
return result;
}
inline auto JoinStrings(
std::initializer_list<std::string_view> strs,
const std::string& separator = " ") -> std::string {
return JoinStringsImpl(strs, separator);
}
template <class T>
auto JoinStrings(T&& strs, const std::string& separator = " ") -> std::string {
return JoinStringsImpl(std::forward<T>(strs), separator);
}
auto RegexSplit(const std::string& str, const std::string& pattern) -> std::vector<std::string>;
auto RunCommand(const std::string& cmd) -> std::string;
inline auto IsNewer(const std::string& p1, const std::string& p2) -> bool {
return std::filesystem::last_write_time(p1) > std::filesystem::last_write_time(p2);
}
template <class Filter, class Collector>
void WalkDirectory(const std::string& path, Filter filter, Collector collector) {
for (std::vector<std::string> stack{path}; !stack.empty();) {
auto dir = stack.back();
stack.pop_back();
for (const auto& iter : std::filesystem::directory_iterator(dir)) {
const auto& path = iter.path();
if (filter(path)) {
if (std::filesystem::is_directory(path)) {
stack.push_back(path);
}
collector(path);
}
}
}
}