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) {
32 return str_view.substr(start, pos - start);
37 while (z + 1 < num.size() && num[z] ==
'0') {
50inline bool wildcardMatch(std::string_view pattern, std::string_view str)
noexcept {
53 size_t star_idx = std::string_view::npos;
56 while (s_idx < str.size()) {
57 if (p_idx < pattern.size() && (pattern[p_idx] ==
'?' || pattern[p_idx] == str[s_idx])) {
60 }
else if (p_idx < pattern.size() && pattern[p_idx] ==
'*') {
64 }
else if (star_idx != std::string_view::npos) {
73 while (p_idx < pattern.size() && pattern[p_idx] ==
'*') {
77 return p_idx == pattern.size();
88inline std::vector<std::filesystem::path>
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)) {
99 for (
const auto &entry : std::filesystem::directory_iterator(search_dir, error_code)) {
103 const std::string filename_str = entry.path().filename().string();
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));
112 return next_candidates;
123 if (!pattern.contains(
'*') && !pattern.contains(
'?')) {
127 const std::filesystem::path pattern_path(pattern);
128 std::vector<std::filesystem::path> current_candidates = {std::filesystem::path{}};
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(
'?');
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));
144 current_candidates = std::move(next_candidates);
147 if (current_candidates.empty()) {
152 if (current_candidates.empty()) {
153 throw std::runtime_error(
"Wildcard pattern matched no files: " + pattern);
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());
173inline bool naturalLess(std::string_view str_a, std::string_view str_b)
noexcept {
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;
181 if (is_digit_a && is_digit_b) {
188 if (sig_a.size() != sig_b.size()) {
189 return sig_a.size() < sig_b.size();
191 if (sig_a != sig_b) {
192 return sig_a < sig_b;
194 if (num_a.size() != num_b.size()) {
195 return num_a.size() > num_b.size();
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])));
208 return i_idx >= str_a.size() && j_idx < str_b.size();
224 if (!pattern.contains(
'*') && !pattern.contains(
'?')) {
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);
236 globfree(&glob_result);
237 throw std::runtime_error(
"Error expanding wildcard pattern: " + pattern);
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]);
245 globfree(&glob_result);
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