|
| 1 | +#include "brace_expansion.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstddef> |
| 5 | +#include <regex> |
| 6 | +#include <string> |
| 7 | +#include <utility> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +std::vector<std::string> splitByComma(const std::string& str) { |
| 13 | + std::vector<std::string> out; |
| 14 | + std::size_t start = 0; |
| 15 | + while (true) { |
| 16 | + const std::size_t pos = str.find(',', start); |
| 17 | + if (pos == std::string::npos) { |
| 18 | + out.emplace_back(str.substr(start)); |
| 19 | + break; |
| 20 | + } |
| 21 | + out.emplace_back(str.substr(start, pos - start)); |
| 22 | + start = pos + 1; |
| 23 | + } |
| 24 | + return out; |
| 25 | +} |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | +std::vector<std::string> braceExpansion(const std::string& input) { |
| 30 | + std::vector<std::string> prefixes; |
| 31 | + prefixes.emplace_back(""); // Start with an empty prefix |
| 32 | + std::string remaining = input; |
| 33 | + |
| 34 | + // Regex captures (compile once; reuse): |
| 35 | + // 1) leading literal before { ([^{}]*) |
| 36 | + // 2) choices inside {} ([^}]*) |
| 37 | + // 3) literal after } ([^{}]*) |
| 38 | + // 4) remaining tail (.*) |
| 39 | + static const std::regex REGEX(R"(([^{}]*)\{([^}]*)\}([^{}]*)(.*))"); |
| 40 | + |
| 41 | + std::smatch match; |
| 42 | + // Iterate group by group until no more braces |
| 43 | + while (std::regex_match(remaining, match, REGEX)) { |
| 44 | + const std::string& lead = match[1].str(); |
| 45 | + const std::string& choicesStr = match[2].str(); |
| 46 | + const std::string& mid = match[3].str(); |
| 47 | + std::string tail = match[4].str(); // will become new remaining |
| 48 | + |
| 49 | + // Expand choices and sort them |
| 50 | + std::vector<std::string> choices = splitByComma(choicesStr); |
| 51 | + if (choices.size() > 1) { |
| 52 | + std::ranges::sort(choices); |
| 53 | + } |
| 54 | + |
| 55 | + std::vector<std::string> newPrefixes; |
| 56 | + newPrefixes.reserve(prefixes.size() * std::max(choices.size(), 1UL)); |
| 57 | + for (const auto& prefix : prefixes) { |
| 58 | + for (const auto& choice : choices) { |
| 59 | + std::string newPrefix = prefix; |
| 60 | + newPrefix += lead; |
| 61 | + newPrefix += choice; |
| 62 | + newPrefix += mid; |
| 63 | + newPrefixes.emplace_back(std::move(newPrefix)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + prefixes.swap(newPrefixes); // reuse memory |
| 68 | + remaining.swap(tail); // continue with the tail |
| 69 | + } |
| 70 | + |
| 71 | + // Append final remaining tail (no more braces) |
| 72 | + std::vector<std::string> results; |
| 73 | + results.reserve(prefixes.size()); |
| 74 | + if (!prefixes.empty()) { |
| 75 | + for (const auto& prefix : prefixes) { |
| 76 | + std::string result = prefix; |
| 77 | + result += remaining; |
| 78 | + results.emplace_back(std::move(result)); |
| 79 | + } |
| 80 | + } else { |
| 81 | + results.emplace_back(remaining); // input had no braces |
| 82 | + } |
| 83 | + |
| 84 | + return results; |
| 85 | +} |
0 commit comments