Debian Codegraph @

avogadro/core/utilities.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_UTILITIES_H
7 #define AVOGADRO_CORE_UTILITIES_H
8
9 #include <algorithm>
10 #include <optional>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14
15 namespace Avogadro::Core {
16
17 /**
18 * @brief Split the supplied @p string by the @p delimiter.
19 * @param string The string to be split up.
20 * @param delimiter The delimiter to split the string by.
21 * @param skipEmpty If true any empty items will be skipped.
22 * @return A vector containing the items.
23 */
24 inline std::vector<std::string> split(const std::string& string, char delimiter,
25 bool skipEmpty = true)
26 {
27 std::vector<std::string> elements;
28 std::stringstream stringStream(string);
29 std::string item;
30 while (std::getline(stringStream, item, delimiter)) {
31 if (skipEmpty && item.empty())
32 continue;
33 elements.push_back(item);
34 }
35 return elements;
36 }
37
38 /**
39 * @brief Search the input string for the search string.
40 * @param input String to be examined.
41 * @param search String that will be searched for.
42 * @return True if the string contains search, false otherwise.
43 */
44 inline bool contains(const std::string& input, const std::string& search,
45 bool caseSensitive = true)
46 {
47 if (caseSensitive) {
48 return input.find(search) != std::string::npos;
49 } else {
50 std::string inputLower = input;
51 std::string searchLower = search;
52 std::transform(inputLower.begin(), inputLower.end(), inputLower.begin(),
53 ::tolower);
54 std::transform(searchLower.begin(), searchLower.end(), searchLower.begin(),
55 ::tolower);
56 return inputLower.find(searchLower) != std::string::npos;
57 }
58 }
59
60 /**
61 * @brief Efficient method to confirm input starts with the search string.
62 * @param input String to be examined.
63 * @param search String that will be searched for.
64 * @return True if the string starts with search, false otherwise.
65 */
66 inline bool startsWith(const std::string& input, const std::string& search)
67 {
68 return input.size() >= search.size() &&
69 input.compare(0, search.size(), search) == 0;
70 }
71
72 /**
73 * @brief Efficient method to confirm input ends with the ending string.
74 * @param input String to be examined.
75 * @param ending String that will be searched for.
76 * @return True if the string ends with ending, false otherwise.
77 */
78 inline bool endsWith(std::string const& input, std::string const& ending)
79 {
80 if (ending.size() > input.size())
81 return false;
82 return std::equal(ending.rbegin(), ending.rend(), input.rbegin());
83 }
84
85 /**
86 * @brief Trim a string of whitespace from the left and right.
87 */
88 inline std::string trimmed(const std::string& input)
89 {
90 size_t start = input.find_first_not_of(" \n\r\t");
91 size_t end = input.find_last_not_of(" \n\r\t");
92 if (start == std::string::npos && end == std::string::npos)
93 return "";
94 return input.substr(start, end - start + 1);
95 }
96
97 /**
98 * @brief Remove trailing part of `str` after `c`
99 */
100 inline std::string rstrip(const std::string& str, char c)
101 {
102 return str.substr(0, str.find_first_of(c));
103 }
104
105 /**
106 * @brief Cast the inputString to the specified type.
107 * @param inputString String to cast to the specified type.
108 * @retval converted value if cast is successful
109 * @retval std::nullopt otherwise
110 */
111 template <typename T>
112 std::optional<T> lexicalCast(const std::string& inputString)
113 {
114 T value;
115 std::istringstream stream(inputString);
116 stream >> value;
117 if (stream.fail())
118 return std::nullopt;
119 return value;
120 }
121
122 /**
123 * @brief Cast the inputString to the specified type.
124 * @param inputString String to cast to the specified type.
125 * @param ok Set to true on success, and false if the string could not be
126 * converted to the specified type.
127 */
128 template <typename T>
129 T lexicalCast(const std::string& inputString, bool& ok)
130 {
131 if (auto value = lexicalCast<T>(inputString)) {
132 ok = true;
133 return *value;
134 }
135 ok = false;
136 return {};
137 }
138
139 /**
140 * @brief Cast the range to the specified type.
141 * @param first Start of the range
142 * @param last End of the range
143 * @retval converted values if cast of ALL the elements are successful
144 * @retval std::nullopt otherwise
145 */
146 template <typename T, typename Iterator>
147 std::optional<std::vector<T>> lexicalCast(Iterator first, Iterator last)
148 {
149 std::vector<T> values;
150 for (; first != last; ++first) {
151 if (auto value = lexicalCast<T>(*first)) {
152 values.emplace_back(*value);
153 } else {
154 return std::nullopt;
155 }
156 }
157 return values;
158 }
159
160 } // namespace Avogadro::Core
161
162 #endif // AVOGADRO_CORE_UTILITIES_H