|
19 | 19 | #include <sstream> |
20 | 20 | #include <stdexcept> |
21 | 21 | #include <algorithm> |
| 22 | +#include <regex> |
22 | 23 | #include <queue> |
23 | 24 | #include <set> |
24 | 25 |
|
@@ -103,18 +104,75 @@ std::string A2uiCatalog::render_as_llm_instructions() const { |
103 | 104 | return ss.str(); |
104 | 105 | } |
105 | 106 |
|
| 107 | +static std::string glob_to_regex(const std::string& glob) { |
| 108 | + std::string regex = "^"; |
| 109 | + for (size_t i = 0; i < glob.length(); ++i) { |
| 110 | + char c = glob[i]; |
| 111 | + if (c == '*') { |
| 112 | + if (i + 1 < glob.length() && glob[i+1] == '*') { |
| 113 | + regex += ".*"; |
| 114 | + i++; // skip next * |
| 115 | + if (i + 1 < glob.length() && glob[i+1] == '/') { |
| 116 | + i++; // skip next / |
| 117 | + } |
| 118 | + } else { |
| 119 | + regex += "[^/]*"; |
| 120 | + } |
| 121 | + } else if (c == '?') { |
| 122 | + regex += "[^/]"; |
| 123 | + } else if (c == '.') { |
| 124 | + regex += "\\."; |
| 125 | + } else if (c == '[') { |
| 126 | + regex += c; |
| 127 | + if (i + 1 < glob.length() && glob[i+1] == '!') { |
| 128 | + regex += "^"; |
| 129 | + i++; // skip ! |
| 130 | + } |
| 131 | + } else if (c == ']') { |
| 132 | + regex += c; |
| 133 | + |
| 134 | + } else { |
| 135 | + regex += c; |
| 136 | + } |
| 137 | + } |
| 138 | + regex += "$"; |
| 139 | + return regex; |
| 140 | +} |
| 141 | + |
106 | 142 | std::string A2uiCatalog::load_examples(const std::string& path, bool validate) const { |
107 | 143 | if (path.empty()) return ""; |
108 | 144 |
|
109 | 145 | std::vector<std::string> matched_files; |
110 | | - if (fs::is_directory(path)) { |
111 | | - for (const auto& entry : fs::directory_iterator(path)) { |
112 | | - if (entry.is_regular_file() && entry.path().extension() == ".json") { |
113 | | - matched_files.push_back(entry.path().string()); |
| 146 | + |
| 147 | + size_t first_wildcard = path.find_first_of("*?["); |
| 148 | + if (first_wildcard != std::string::npos) { |
| 149 | + size_t last_slash = path.rfind('/', first_wildcard); |
| 150 | + std::string base_dir = (last_slash == std::string::npos) ? "." : path.substr(0, last_slash); |
| 151 | + std::string pattern = (last_slash == std::string::npos) ? path : path.substr(last_slash + 1); |
| 152 | + |
| 153 | + std::string regex_str = glob_to_regex(pattern); |
| 154 | + std::regex pattern_regex(regex_str); |
| 155 | + |
| 156 | + if (fs::exists(base_dir) && fs::is_directory(base_dir)) { |
| 157 | + for (const auto& entry : fs::recursive_directory_iterator(base_dir)) { |
| 158 | + if (entry.is_regular_file()) { |
| 159 | + std::string rel_path = fs::relative(entry.path(), base_dir).string(); |
| 160 | + if (std::regex_match(rel_path, pattern_regex) && entry.path().extension() == ".json") { |
| 161 | + matched_files.push_back(entry.path().string()); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } else { |
| 167 | + if (fs::is_directory(path)) { |
| 168 | + for (const auto& entry : fs::directory_iterator(path)) { |
| 169 | + if (entry.is_regular_file() && entry.path().extension() == ".json") { |
| 170 | + matched_files.push_back(entry.path().string()); |
| 171 | + } |
114 | 172 | } |
| 173 | + } else if (fs::is_regular_file(path)) { |
| 174 | + matched_files.push_back(path); |
115 | 175 | } |
116 | | - } else if (fs::is_regular_file(path)) { |
117 | | - matched_files.push_back(path); |
118 | 176 | } |
119 | 177 |
|
120 | 178 | if (matched_files.empty()) return ""; |
|
0 commit comments