40 lines
1.3 KiB
C++
40 lines
1.3 KiB
C++
#include <expected>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <cstring>
|
|
|
|
std::vector<std::string> split(std::string s, std::string delimiters) {
|
|
std::vector<std::string> tokens;
|
|
size_t last = 0, next = 0;
|
|
while ((next = s.find_first_of(delimiters, last)) != std::string::npos) {
|
|
if (next != last) tokens.push_back(s.substr(last, next - last));
|
|
last = next + 1;
|
|
}
|
|
if (last < s.length()) tokens.push_back(s.substr(last));
|
|
return tokens;
|
|
}
|
|
|
|
std::string trim(std::string& source) {
|
|
auto line = source;
|
|
std::string whitespaces(" \t\n\r\f\v");
|
|
line.erase(0, line.find_first_not_of(whitespaces));
|
|
line.erase(line.find_last_not_of(whitespaces) + 1);
|
|
return line;
|
|
}
|
|
|
|
std::expected<int, std::string> strtoint (std::string source) {
|
|
std::size_t pos{};
|
|
int res;
|
|
try {
|
|
res = std::stoi(source, &pos);
|
|
} catch (std::invalid_argument const& ex) {
|
|
return std::unexpected(std::format("invalid argument:{}", ex.what()));
|
|
} catch (std::out_of_range const& ex) {
|
|
return std::unexpected(std::format("out of range", ex.what()));
|
|
}
|
|
return res;
|
|
}
|