-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_utils.hpp
56 lines (46 loc) · 1.21 KB
/
text_utils.hpp
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
#pragma once
#include <string>
#include <vector>
#include <map>
namespace text_utils
{
static std::vector<int> apply_xor(std::string message, std::string key)
{
std::vector<int> result;
result.reserve(message.size());
for (auto i = 0; i < message.size(); ++i) {
result.emplace_back(message[i] ^ key[i % key.size()]);
}
return result;
}
static std::string apply_xor(std::vector<int> buffer, std::string key)
{
std::string result;
result.reserve(buffer.size());
for (auto i = 0; i < buffer.size(); ++i) {
result.push_back(buffer[i] ^ key[i % key.size()]);
}
return result;
}
static std::string apply_variables(std::string source, std::map<std::string, std::string> variables)
{
while (true) {
bool done = true;
for (auto i : variables) {
const auto target = "{{" + i.first + "}}";
while (true) {
const auto p = source.find(target);
if (p == std::string::npos) {
break;
}
source.replace(p, target.size(), i.second);
done = false;
}
}
if (done) {
break;
}
}
return source;
}
}