-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaocio.hpp
More file actions
208 lines (180 loc) · 5.37 KB
/
Copy pathaocio.hpp
File metadata and controls
208 lines (180 loc) · 5.37 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include <iostream>
#include <fstream>
#include <filesystem>
#include <vector>
#include <limits>
#include <cassert>
#include <optional>
#ifndef AOC_INPUT_PATH
#define AOC_INPUT_PATH ""
#endif
#ifndef AOC_SRC_DIR
#define AOC_SRC_DIR ""
#endif
namespace aocio
{
inline bool file_getlines(std::string_view fname, std::vector<std::string>& lines)
{
std::ifstream file {fname};
if (!file) {
std::cerr << "Cannot open file " << fname << "\n";
return false;
}
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
return true;
}
inline void remove_leading_empty_lines(std::vector<std::string>& lines)
{
auto line = lines.begin();
while (line != lines.end()) {
std::size_t idx = line->find_first_not_of(" \t", 0);
if (idx == std::string::npos) {
lines.erase(line);
line = lines.begin();
} else {
assert(line->size());
break;
}
}
}
inline void remove_trailing_empty_lines(std::vector<std::string>& lines)
{
auto line = lines.rbegin();
while (line != lines.rend()) {
std::size_t idx = line->find_first_not_of(" \t", 0);
if (idx == std::string::npos) {
lines.erase(std::next(line).base()); // cf. https://stackoverflow.com/questions/1830158/how-to-call-erase-with-a-reverse-iterator [1]
line = lines.rbegin();
} else {
assert(line->size());
break;
}
}
// [1] last retrieved 2024-06-25
}
inline void line_tokenise(const std::string& line, const std::string& delims, const std::string& preserved_delims, std::vector<std::string>& tokens)
{
for (char d : preserved_delims) {
if (delims.find(d) == std::string::npos) {
throw std::invalid_argument("Preserved delim not in delims");
}
}
std::string::size_type start_pos = 0;
while (start_pos < line.size()) {
auto token_end_pos = line.find_first_of(delims, start_pos);
if (token_end_pos == std::string::npos) {
token_end_pos = line.size();
}
std::string token = line.substr(start_pos, token_end_pos - start_pos);
if (token.size()) {
tokens.push_back(token);
}
if (token_end_pos != std::string::npos && preserved_delims.size() && preserved_delims.find(line[token_end_pos]) != std::string::npos) {
tokens.push_back(std::string{line[token_end_pos]});
}
start_pos = token_end_pos + 1;
}
}
static inline std::string str_without_whitespace(std::string_view str)
{
std::string result;
auto is_ws = [](char c) -> bool { return c == ' ' || c == '\t'; };
std::remove_copy_if(str.cbegin(), str.cend(), std::back_inserter(result), is_ws);
return result;
}
static inline void str_remove_whitespace(std::string& str)
{
auto is_ws = [](char c) -> bool { return c == ' ' || c == '\t'; };
auto no_space_end = std::remove_if(str.begin(), str.end(), is_ws);
str.erase(no_space_end, str.end());
}
static inline std::optional<int> parse_num(const std::string &str)
{
size_t num_read = 0;
int n = std::stoi(str, &num_read);
if (num_read == 0) {
return {};
}
return n;
}
static inline std::optional<int64_t> parse_num_i64(const std::string& str)
{
size_t num_read = 0;
int64_t n = std::stoll(str, &num_read);
if (num_read == 0) {
return {};
}
return n;
}
static inline std::optional<int> parse_digit(char c)
{
int digit = static_cast<int>(c) - '0';
if (digit >= 0 && digit <= 9) {
return digit;
} else {
return {};
}
}
template <typename IntT = int>
static inline std::optional<IntT> parse_hex(std::string_view str)
{
if (str.size() == 0) {
return {};
}
int prefix = 0;
while (str.at(prefix) == ' ' || str.at(prefix) == '\t') { // Remove leading whitespace
++prefix;
}
str = str.substr(prefix, str.size());
if (str.size() >= 2) { // Handle # and 0x prefixes
if (str.at(0) == '#') {
str = str.substr(1, str.size());
} else if (str.size() >= 3) {
if (str.at(0) == '0' && str.at(1) == 'x') {
str = str.substr(2, str.size());
}
}
}
int end_idx = std::ssize(str) - 1;
while (str.at(end_idx) == ' ' || str.at(end_idx) == '\t') { // Remove trailing whitespace.
end_idx -= 1;
}
IntT res = 0;
IntT fac = 1;
for (int i = end_idx; i >= 0; --i, fac *= 16) {
char sym = str.at(i);
IntT digit = 0;
if (sym >= '0' && sym <= '9') {
digit = aocio::parse_digit(sym).value();
} else {
digit = 10 + (sym - 'a');
if (digit < 10 || digit > 15) { // Try uppercase.
digit = 10 + (sym - 'A');
}
if (digit < 10 || digit > 15) {
return {};
}
}
res += digit * fac;
}
return res;
}
inline void print_day()
{
std::string day_name {std::filesystem::path(AOC_SRC_DIR).parent_path().filename()};
if (day_name.size()) {
day_name[0] = std::toupper(day_name[0]);
}
std::string debug_release;
#ifdef NDEBUG
debug_release = "Release";
#else
debug_release = "Debug";
#endif
std::cout << day_name << " (" << debug_release << ")\n";
}
}