Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
NaturalSort.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <algorithm>
12#include <cctype>
13#include <filesystem>
14#include <stdexcept>
15#include <string>
16#include <string_view>
17#include <vector>
18
19#ifndef _WIN32
20#include <glob.h>
21#endif
22
23namespace correlation::readers {
24
25namespace detail {
26
27inline std::string_view extractDigits(std::string_view str_view, size_t &pos) noexcept {
28 const size_t start = pos;
29 while (pos < str_view.size() && std::isdigit(static_cast<unsigned char>(str_view[pos])) != 0) {
30 ++pos;
31 }
32 return str_view.substr(start, pos - start);
33}
34
35inline std::string_view stripLeadingZeros(std::string_view num) noexcept {
36 size_t z = 0;
37 while (z + 1 < num.size() && num[z] == '0') {
38 ++z;
39 }
40 return num.substr(z);
41}
42
50inline bool wildcardMatch(std::string_view pattern, std::string_view str) noexcept {
51 size_t p_idx = 0;
52 size_t s_idx = 0;
53 size_t star_idx = std::string_view::npos;
54 size_t match_idx = 0;
55
56 while (s_idx < str.size()) {
57 if (p_idx < pattern.size() && (pattern[p_idx] == '?' || pattern[p_idx] == str[s_idx])) {
58 ++p_idx;
59 ++s_idx;
60 } else if (p_idx < pattern.size() && pattern[p_idx] == '*') {
61 star_idx = p_idx;
62 match_idx = s_idx;
63 ++p_idx;
64 } else if (star_idx != std::string_view::npos) {
65 p_idx = star_idx + 1;
66 ++match_idx;
67 s_idx = match_idx;
68 } else {
69 return false;
70 }
71 }
72
73 while (p_idx < pattern.size() && pattern[p_idx] == '*') {
74 ++p_idx;
75 }
76
77 return p_idx == pattern.size();
78}
79
88inline std::vector<std::filesystem::path>
89matchComponentEntries(std::string_view component_pattern, bool is_last,
90 const std::vector<std::filesystem::path> &candidate_paths) {
91 std::vector<std::filesystem::path> next_candidates;
92 for (const auto &base_path : candidate_paths) {
93 std::error_code error_code;
94 const std::filesystem::path search_dir =
95 base_path.empty() ? std::filesystem::path(".") : base_path;
96 if (!std::filesystem::exists(search_dir, error_code)) {
97 continue;
98 }
99 for (const auto &entry : std::filesystem::directory_iterator(search_dir, error_code)) {
100 if (error_code) {
101 break;
102 }
103 const std::string filename_str = entry.path().filename().string();
104 if (wildcardMatch(component_pattern, filename_str)) {
105 if (is_last || entry.is_directory(error_code)) {
106 next_candidates.push_back(base_path.empty() ? std::filesystem::path(filename_str)
107 : (base_path / filename_str));
108 }
109 }
110 }
111 }
112 return next_candidates;
113}
114
122inline std::vector<std::string> expandWildcardsFilesystem(const std::string &pattern) {
123 if (!pattern.contains('*') && !pattern.contains('?')) {
124 return {pattern};
125 }
126
127 const std::filesystem::path pattern_path(pattern);
128 std::vector<std::filesystem::path> current_candidates = {std::filesystem::path{}};
129
130 for (auto comp_it = pattern_path.begin(); comp_it != pattern_path.end(); ++comp_it) {
131 const std::string comp_str = comp_it->string();
132 const bool is_last = (std::next(comp_it) == pattern_path.end());
133 const bool has_wildcard = comp_str.contains('*') || comp_str.contains('?');
134
135 if (has_wildcard) {
136 current_candidates = matchComponentEntries(comp_str, is_last, current_candidates);
137 } else {
138 std::vector<std::filesystem::path> next_candidates;
139 next_candidates.reserve(current_candidates.size());
140 for (const auto &base_path : current_candidates) {
141 next_candidates.push_back(base_path.empty() ? std::filesystem::path(comp_str)
142 : (base_path / comp_str));
143 }
144 current_candidates = std::move(next_candidates);
145 }
146
147 if (current_candidates.empty()) {
148 break;
149 }
150 }
151
152 if (current_candidates.empty()) {
153 throw std::runtime_error("Wildcard pattern matched no files: " + pattern);
154 }
155
156 std::vector<std::string> results;
157 results.reserve(current_candidates.size());
158 for (const auto &candidate : current_candidates) {
159 results.push_back(candidate.string());
160 }
161
162 return results;
163}
164
165} // namespace detail
166
173inline bool naturalLess(std::string_view str_a, std::string_view str_b) noexcept {
174 size_t i_idx = 0;
175 size_t j_idx = 0;
176
177 while (i_idx < str_a.size() && j_idx < str_b.size()) {
178 const bool is_digit_a = std::isdigit(static_cast<unsigned char>(str_a[i_idx])) != 0;
179 const bool is_digit_b = std::isdigit(static_cast<unsigned char>(str_b[j_idx])) != 0;
180
181 if (is_digit_a && is_digit_b) {
182 const std::string_view num_a = detail::extractDigits(str_a, i_idx);
183 const std::string_view num_b = detail::extractDigits(str_b, j_idx);
184
185 const std::string_view sig_a = detail::stripLeadingZeros(num_a);
186 const std::string_view sig_b = detail::stripLeadingZeros(num_b);
187
188 if (sig_a.size() != sig_b.size()) {
189 return sig_a.size() < sig_b.size();
190 }
191 if (sig_a != sig_b) {
192 return sig_a < sig_b;
193 }
194 if (num_a.size() != num_b.size()) {
195 return num_a.size() > num_b.size();
196 }
197 } else {
198 const char c_a = static_cast<char>(std::tolower(static_cast<unsigned char>(str_a[i_idx])));
199 const char c_b = static_cast<char>(std::tolower(static_cast<unsigned char>(str_b[j_idx])));
200 if (c_a != c_b) {
201 return c_a < c_b;
202 }
203 ++i_idx;
204 ++j_idx;
205 }
206 }
207
208 return i_idx >= str_a.size() && j_idx < str_b.size();
209}
210
214inline void naturalSort(std::vector<std::string> &paths) { std::ranges::sort(paths, naturalLess); }
215
223inline std::vector<std::string> expandWildcards(const std::string &pattern) {
224 if (!pattern.contains('*') && !pattern.contains('?')) {
225 return {pattern};
226 }
227
228#ifndef _WIN32
229 glob_t glob_result{};
230 const int res = glob(pattern.c_str(), GLOB_TILDE, nullptr, &glob_result);
231 if (res == GLOB_NOMATCH) {
232 globfree(&glob_result);
233 throw std::runtime_error("Wildcard pattern matched no files: " + pattern);
234 }
235 if (res != 0) {
236 globfree(&glob_result);
237 throw std::runtime_error("Error expanding wildcard pattern: " + pattern);
238 }
239
240 std::vector<std::string> files;
241 files.reserve(glob_result.gl_pathc);
242 for (size_t k = 0; k < glob_result.gl_pathc; ++k) {
243 files.emplace_back(glob_result.gl_pathv[k]);
244 }
245 globfree(&glob_result);
246
247 naturalSort(files);
248 return files;
249#else
250 auto files = detail::expandWildcardsFilesystem(pattern);
251 naturalSort(files);
252 return files;
253#endif
254}
255
256} // namespace correlation::readers
Definition NaturalSort.hpp:25
std::vector< std::filesystem::path > matchComponentEntries(std::string_view component_pattern, bool is_last, const std::vector< std::filesystem::path > &candidate_paths)
Matches directory entries against a component pattern for all candidate paths.
Definition NaturalSort.hpp:89
std::vector< std::string > expandWildcardsFilesystem(const std::string &pattern)
Cross-platform filesystem-based wildcard expansion.
Definition NaturalSort.hpp:122
bool wildcardMatch(std::string_view pattern, std::string_view str) noexcept
Checks if a string matches a wildcard pattern containing '*' and '?'.
Definition NaturalSort.hpp:50
std::string_view stripLeadingZeros(std::string_view num) noexcept
Definition NaturalSort.hpp:35
std::string_view extractDigits(std::string_view str_view, size_t &pos) noexcept
Definition NaturalSort.hpp:27
Definition TrajectoryLoader.hpp:24
std::vector< std::string > expandWildcards(const std::string &pattern)
Expands wildcard patterns (*, ?) in a path and sorts naturally.
Definition NaturalSort.hpp:223
bool naturalLess(std::string_view str_a, std::string_view str_b) noexcept
Performs natural alphanumeric comparison of two strings.
Definition NaturalSort.hpp:173
void naturalSort(std::vector< std::string > &paths)
Sorts a container of file paths in natural alphanumeric order.
Definition NaturalSort.hpp:214