-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdependency_selector.cppm
More file actions
104 lines (89 loc) · 2.9 KB
/
dependency_selector.cppm
File metadata and controls
104 lines (89 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// mcpp.pm.dependency_selector — parse user dependency selectors into
// ordered package-coordinate candidates.
export module mcpp.pm.dependency_selector;
import std;
import mcpp.pm.dep_spec;
export namespace mcpp::pm {
enum class DependencySelectorMode {
OmittedMcpplibsPriority,
};
struct DependencySelector {
std::vector<DependencyCoordinate> candidates;
std::string stableMapKey;
};
inline std::vector<std::string> split_dependency_selector(std::string_view selector)
{
std::vector<std::string> segments;
std::size_t start = 0;
for (std::size_t i = 0; i <= selector.size(); ++i) {
if (i == selector.size() || selector[i] == '.') {
segments.emplace_back(selector.substr(start, i - start));
start = i + 1;
}
}
return segments;
}
inline std::string join_dependency_segments(const std::vector<std::string>& segments,
std::size_t first,
std::size_t last)
{
std::string out;
for (std::size_t i = first; i < last && i < segments.size(); ++i) {
if (!out.empty()) out += ".";
out += segments[i];
}
return out;
}
inline DependencySelector make_direct_dependency_selector(
std::string_view ns,
std::string_view shortName,
std::string_view stableMapKey)
{
DependencySelector out;
out.stableMapKey = std::string(stableMapKey);
out.candidates.push_back(DependencyCoordinate{
.namespace_ = std::string(ns),
.shortName = std::string(shortName),
});
return out;
}
inline DependencySelector resolve_dependency_selector(
std::string_view selector,
DependencySelectorMode)
{
DependencySelector out;
out.stableMapKey = std::string(selector);
auto segments = split_dependency_selector(selector);
if (segments.empty()) return out;
if (segments.size() == 1) {
out.candidates.push_back(DependencyCoordinate{
.namespace_ = std::string(kDefaultNamespace),
.shortName = segments.front(),
});
out.candidates.push_back(DependencyCoordinate{
.namespace_ = {},
.shortName = segments.front(),
});
return out;
}
const auto shortName = segments.back();
const auto nsWithoutShort = join_dependency_segments(
segments, 0, segments.size() - 1);
if (segments.front() == kDefaultNamespace) {
out.candidates.push_back(DependencyCoordinate{
.namespace_ = nsWithoutShort,
.shortName = shortName,
});
return out;
}
out.candidates.push_back(DependencyCoordinate{
.namespace_ = std::format("{}.{}", kDefaultNamespace, nsWithoutShort),
.shortName = shortName,
});
out.candidates.push_back(DependencyCoordinate{
.namespace_ = nsWithoutShort,
.shortName = shortName,
});
return out;
}
} // namespace mcpp::pm