-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellSubstitute.cpp
54 lines (43 loc) · 1.53 KB
/
shellSubstitute.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <fmt/core.h>
#include <range/v3/all.hpp>
#include <string_view>
#include <unordered_map>
const auto shellSubstitute =
[](std::string_view input,
const std::unordered_map<std::string, std::string> variables)
-> std::string {
using namespace std::literals;
using namespace ranges;
auto &&output =
input | views::split('$') |
views::transform([&variables](range auto &&rng) {
auto &&splitRng = rng | views::split('}');
auto &&word = *begin(splitRng);
auto &&wordSv = std::string_view{
&*word.begin(), static_cast<std::size_t>(distance(word))};
auto &&trimmedWord = (wordSv[0] == '{') ? wordSv.substr(1) : wordSv;
auto &&rest = splitRng | views::tail | views::join | views::common;
auto substitute =
[&variables](std::string_view sv) -> std::string_view {
if (auto it = variables.find(std::string{sv});
it != variables.cend()) {
return it->second;
} else {
return ""sv;
}
};
return views::concat(substitute(trimmedWord), rest);
}) |
views::join;
return output | to<std::string>;
};
auto main() -> int {
using namespace std::literals;
auto &&input = "${ABC}/hello/${HOME}/dir/${USER}/file"sv;
auto &&variables = std::unordered_map<std::string, std::string>{
{"HOME", "root"}, {"USER", "vlad"}};
auto &&output = shellSubstitute(input, variables);
fmt::print("input: {}\n", input);
fmt::print("output: {}\n", output);
return 0;
}