-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
62 lines (57 loc) · 1.47 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
#ifndef TOBSTERLANG_UTILS_H
#define TOBSTERLANG_UTILS_H
#include <iostream>
#include <string>
namespace Tobsterlang::Utils {
inline auto unescape(std::string const& str) {
std::string result;
result.reserve(str.size());
for (auto i = 0u; i < str.size(); ++i) {
if (str[i] == '\\') {
switch (str[++i]) {
case '\\':
result += '\\';
break;
case '\'':
result += '\'';
break;
case '\"':
result += '\"';
break;
case '0':
result += '\0';
break;
case 'a':
result += '\a';
break;
case 'b':
result += '\b';
break;
case 'f':
result += '\f';
break;
case 'n':
result += '\n';
break;
case 'r':
result += '\r';
break;
case 't':
result += '\t';
break;
case 'v':
result += '\v';
break;
default:
std::cerr << "Unknown escape sequence: \\" << str[i]
<< std::endl;
break;
}
} else {
result += str[i];
}
}
return result;
}
} // namespace Tobsterlang::Utils
#endif // TOBSTERLANG_UTILS_H