95 lines
2.4 KiB
C++
95 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include "fesa/Core/Types.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <optional>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace fesa {
|
|
|
|
inline std::string trim(std::string text) {
|
|
auto is_space = [](unsigned char c) { return std::isspace(c) != 0; };
|
|
text.erase(text.begin(), std::find_if(text.begin(), text.end(), [&](unsigned char c) { return !is_space(c); }));
|
|
text.erase(std::find_if(text.rbegin(), text.rend(), [&](unsigned char c) { return !is_space(c); }).base(), text.end());
|
|
return text;
|
|
}
|
|
|
|
inline std::string lower(std::string text) {
|
|
std::transform(text.begin(), text.end(), text.begin(), [](unsigned char c) {
|
|
return static_cast<char>(std::tolower(c));
|
|
});
|
|
return text;
|
|
}
|
|
|
|
inline std::vector<std::string> splitCsv(const std::string& line) {
|
|
std::vector<std::string> fields;
|
|
std::string field;
|
|
std::istringstream stream(line);
|
|
while (std::getline(stream, field, ',')) {
|
|
fields.push_back(trim(field));
|
|
}
|
|
if (!line.empty() && line.back() == ',') {
|
|
fields.emplace_back();
|
|
}
|
|
return fields;
|
|
}
|
|
|
|
inline std::optional<Real> parseReal(std::string token) {
|
|
token = trim(token);
|
|
if (token.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
std::replace(token.begin(), token.end(), 'D', 'E');
|
|
std::replace(token.begin(), token.end(), 'd', 'e');
|
|
try {
|
|
std::size_t used = 0;
|
|
Real value = std::stod(token, &used);
|
|
if (used != token.size()) {
|
|
return std::nullopt;
|
|
}
|
|
return value;
|
|
} catch (...) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
inline std::optional<GlobalId> parseInt64(const std::string& token) {
|
|
std::string value_text = trim(token);
|
|
if (value_text.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
try {
|
|
std::size_t used = 0;
|
|
long long value = std::stoll(value_text, &used);
|
|
if (used != value_text.size()) {
|
|
return std::nullopt;
|
|
}
|
|
return static_cast<GlobalId>(value);
|
|
} catch (...) {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
inline void addUnique(std::vector<GlobalId>& values, GlobalId value) {
|
|
if (std::find(values.begin(), values.end(), value) == values.end()) {
|
|
values.push_back(value);
|
|
}
|
|
}
|
|
|
|
inline std::vector<GlobalId> generatedRange(GlobalId first, GlobalId last, GlobalId increment) {
|
|
std::vector<GlobalId> values;
|
|
if (increment <= 0) {
|
|
return values;
|
|
}
|
|
for (GlobalId value = first; value <= last; value += increment) {
|
|
values.push_back(value);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
} // namespace fesa
|