MolSim
Loading...
Searching...
No Matches
StringUtils.h
Go to the documentation of this file.
1
9#pragma once
10
11#include "Arguments.h"
12#include "CLIUtils.h"
13#include <algorithm>
14#include <cctype>
15#include <iostream>
16#include <sstream>
17#include <stdexcept>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
23static inline const std::unordered_map<std::string, WriterType> writerTable = {
24 {"vtk", WriterType::VTK}, {"xyz", WriterType::XYZ}, {"nil", WriterType::NIL}};
25
27static inline const std::unordered_map<std::string, SimulationType> simulationTable = {
28 {"gravity", SimulationType::GRAVITY}, {"lj", SimulationType::LJ}};
29
31static inline const std::unordered_map<std::string, ParallelizationType> parallelizationTable = {
33
35static inline const std::unordered_map<WriterType, std::string> writerStringTable = []() {
36 std::unordered_map<WriterType, std::string> reverseMap;
37 for (const auto &pair : writerTable)
38 reverseMap[pair.second] = pair.first;
39 return reverseMap;
40}();
41
43static inline const std::unordered_map<SimulationType, std::string> simulationStringTable = []() {
44 std::unordered_map<SimulationType, std::string> reverseMap;
45 for (const auto &pair : simulationTable)
46 reverseMap[pair.second] = pair.first;
47 return reverseMap;
48}();
49
51static inline const std::unordered_map<ParallelizationType, std::string> parallelizationStringTable = []() {
52 std::unordered_map<ParallelizationType, std::string> reverseMap;
53 for (const auto &pair : parallelizationTable)
54 reverseMap[pair.second] = pair.first;
55 return reverseMap;
56}();
57
68static inline bool findStringIC(const std::string &haystack, const std::string &needle) {
69 auto it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(),
70 [](unsigned char ch1, unsigned char ch2) { return std::toupper(ch1) == std::toupper(ch2); });
71 return (it != haystack.end());
72}
73
75namespace StringUtils {
82static inline double toDouble(const std::string &str) {
83 try {
84 // invalidate NAN, INF and hex numbers
85 if (findStringIC(str, "inf") || findStringIC(str, "nan") || findStringIC(str, "0x"))
86 throw std::invalid_argument("Invalid number");
87
88 // convert singular passed number as string
89 size_t idx = 0;
90 double converted = std::stod(str, &idx);
91 if (idx != str.length())
92 throw std::invalid_argument("Invalid number");
93 return converted;
94 } catch (const std::invalid_argument &ia) {
95 CLIUtils::error("Could not convert to decimal", str);
96 } catch (const std::out_of_range &oor) {
97 CLIUtils::error("Number out of conversion range", str);
98 }
99 // shouldn't reach this; only here to "mute" compiler warning
100 return 0.0;
101}
102
109static inline double toInt(const std::string &str) {
110 try {
111 size_t idx = 0;
112 int converted = std::stoi(str, &idx);
113 if (idx != str.length())
114 throw std::invalid_argument("Invalid number");
115 return converted;
116 } catch (const std::invalid_argument &ia) {
117 CLIUtils::error("Could not convert to integer", str);
118 } catch (const std::out_of_range &oor) {
119 CLIUtils::error("Number out of conversion range", str);
120 }
121 // shouldn't reach this; only here to "mute" compiler warning
122 return 0;
123}
124
139template <size_t N> static inline std::array<int, N> toIntArray(const std::string &str) {
140 if (str.empty() || N == 0)
141 return {};
142
143 if (str.front() != '{' || str.back() != '}')
144 CLIUtils::error("Invalid array syntax", str);
145
146 std::array<int, N> arr = {};
147 std::string tmp;
148 std::string numbers = str.substr(1, str.size() - 2);
149 std::stringstream ss(numbers);
150
151 size_t i = 0;
152 while (i < N && std::getline(ss, tmp, ',')) {
153 arr[i++] = StringUtils::toInt(tmp);
154 }
155
156 return arr;
157}
158
175template <size_t N> static inline std::array<double, N> toDoubleArray(const std::string &str, bool reqBrackets = true) {
176 if (str.empty() || N == 0)
177 return {};
178
179 if (reqBrackets && (str.front() != '{' || str.back() != '}'))
180 CLIUtils::error("Invalid array syntax", str);
181
182 std::array<double, N> arr = {};
183 std::string tmp;
184 std::string numbers = reqBrackets ? str.substr(1, str.size() - 2) : str;
185 std::stringstream ss(numbers);
186
187 size_t i = 0;
188 while (i < N && std::getline(ss, tmp, ',')) {
189 arr[i++] = StringUtils::toDouble(tmp);
190 }
191
192 return arr;
193}
194
201static inline WriterType toWriterType(const std::string &type) {
202 auto it = writerTable.find(type);
203 if (it != writerTable.end())
204 return it->second;
205 else
206 CLIUtils::error("Invalid output type", type);
207 return WriterType::VTK; // shouldn't reach this; included to silence warning
208}
209
216static inline SimulationType toSimulationType(const std::string &type) {
217 auto it = simulationTable.find(type);
218 if (it != simulationTable.end())
219 return it->second;
220 else
221 CLIUtils::error("Invalid output type", type);
222 return SimulationType::GRAVITY; // shouldn't reach this; included to silence warning
223}
224
231static inline ParallelizationType toParallelizationType(const std::string &type) {
232 auto it = parallelizationTable.find(type);
233 if (it != parallelizationTable.end())
234 return it->second;
235 else
236 CLIUtils::error("Invalid output type", type);
237 return ParallelizationType::COARSE; // shouldn't reach this; included to silence warning
238}
239
246static inline std::string fromChar(char c) {
247 std::string s{c};
248 return s;
249}
250
257static inline std::string fromWriterType(WriterType writerType) { return writerStringTable.at(writerType); }
258
265static inline std::string fromSimulationType(SimulationType simulationType) {
266 return simulationStringTable.at(simulationType);
267}
268
275static inline std::string fromParallelizationType(ParallelizationType parallelizationType) {
276 return parallelizationStringTable.at(parallelizationType);
277}
278
286template <typename T> std::string static inline fromNumber(T number) { return std::to_string(number); }
287
294template <typename T> static inline std::string fromVector(const std::vector<T> &vec) {
295 std::stringstream ss;
296 ss << "[";
297 for (size_t i = 0; i < vec.size(); ++i) {
298 ss << vec[i];
299 if (i != vec.size() - 1)
300 ss << ", ";
301 }
302 ss << "]";
303 return ss.str();
304}
305
306} // namespace StringUtils
Configurable program and simulation arguments.
ParallelizationType
Enum containing each possible parallelization strategy.
Definition Arguments.h:26
SimulationType
Enum containg each possible Simulation to be performed.
Definition Arguments.h:23
WriterType
Enum containing each (valid) type of output writer.
Definition Arguments.h:20
Utility functions for console input / output.
static const std::unordered_map< ParallelizationType, std::string > parallelizationStringTable
Reverse map containing conversion information for converting a ParallelizationType enum to a string.
Definition StringUtils.h:51
static const std::unordered_map< WriterType, std::string > writerStringTable
Reverse map containing conversion information for converting a WriterType enum to a string.
Definition StringUtils.h:35
static const std::unordered_map< std::string, WriterType > writerTable
Map containing conversion information for converting a string to a WriterType enum.
Definition StringUtils.h:23
static const std::unordered_map< std::string, SimulationType > simulationTable
Map containing conversion information for converting a string to a SimulationType enum.
Definition StringUtils.h:27
static const std::unordered_map< SimulationType, std::string > simulationStringTable
Reverse map containing conversion information for converting a SimulationType enum to a string.
Definition StringUtils.h:43
static bool findStringIC(const std::string &haystack, const std::string &needle)
Finds a given substring in a string. Case-insensitive.
Definition StringUtils.h:68
static const std::unordered_map< std::string, ParallelizationType > parallelizationTable
Map containing conversion information for converting a string to a ParallelizationType enum.
Definition StringUtils.h:31
Enumeration class corresponding to the type schema type.
Definition vtk-unstructured.h:2125
static void error(const char *msg, const std::string &opt="", bool usage=true, bool close=true)
Prints an error message to stderr with the prefix "ERROR: ", optionally prints the usage string and e...
Definition CLIUtils.h:149
Namespace defining utility functions for working with strings.
Definition StringUtils.h:75
static SimulationType toSimulationType(const std::string &type)
Converts a string to a SimulationType enum using a dedicated map.
Definition StringUtils.h:216
static double toDouble(const std::string &str)
Converts a string to a double.
Definition StringUtils.h:82
static std::string fromNumber(T number)
Converts a number to a string.
Definition StringUtils.h:286
static std::string fromParallelizationType(ParallelizationType parallelizationType)
Converts a ParallelizationType to a string.
Definition StringUtils.h:275
static ParallelizationType toParallelizationType(const std::string &type)
Converts a string to a ParallelizationType enum using a dedicated map.
Definition StringUtils.h:231
static std::string fromChar(char c)
Converts a char to a string.
Definition StringUtils.h:246
static std::string fromVector(const std::vector< T > &vec)
Converts a vector to a string.
Definition StringUtils.h:294
static double toInt(const std::string &str)
Converts a string to an integer.
Definition StringUtils.h:109
static std::array< int, N > toIntArray(const std::string &str)
Converts a string into an integer array.
Definition StringUtils.h:139
static std::string fromSimulationType(SimulationType simulationType)
Converts a SimulationType to a string.
Definition StringUtils.h:265
static std::string fromWriterType(WriterType writerType)
Converts a WriterType to a string.
Definition StringUtils.h:257
static std::array< double, N > toDoubleArray(const std::string &str, bool reqBrackets=true)
Converts a string into a double array.
Definition StringUtils.h:175
static WriterType toWriterType(const std::string &type)
Converts a string to a WriterType enum using a dedicated map.
Definition StringUtils.h:201