Skip to content

Commit

Permalink
MatchSpec small improvements (#3043)
Browse files Browse the repository at this point in the history
* Small CQA

* Add ChannelSpec::str

* Add ChannelContext::make_channel for ChannelSpec

* Split MatchSpec tests
  • Loading branch information
AntoinePrv authored Dec 7, 2023
1 parent df4aeac commit dfcb8c2
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 279 deletions.
3 changes: 2 additions & 1 deletion libmamba/include/mamba/core/channel_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace mamba
*/
ChannelContext(ChannelResolveParams params, std::vector<Channel> has_zst);

auto make_channel(std::string_view name) -> const channel_list&;
[[nodiscard]] auto make_channel(specs::ChannelSpec spec) -> const channel_list&;
[[nodiscard]] auto make_channel(std::string_view name) -> const channel_list&;

[[nodiscard]] auto params() const -> const specs::ChannelResolveParams&;

Expand Down
2 changes: 1 addition & 1 deletion libmamba/include/mamba/core/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace mamba

bool is_simple() const;

static std::tuple<std::string, std::string> parse_version_and_build(const std::string& s);
static std::tuple<std::string, std::string> parse_version_and_build(std::string_view s);
std::string spec;

std::string name;
Expand Down
24 changes: 24 additions & 0 deletions libmamba/include/mamba/specs/channel_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <string>
#include <string_view>

#include <fmt/core.h>
#include <fmt/format.h>

#include "mamba/util/flat_set.hpp"

namespace mamba::specs
Expand Down Expand Up @@ -98,11 +101,32 @@ namespace mamba::specs
[[nodiscard]] auto platform_filters() && -> dynamic_platform_set;
auto clear_platform_filters() -> dynamic_platform_set;

[[nodiscard]] auto str() const -> std::string;

private:

std::string m_location = std::string(unknown_channel);
dynamic_platform_set m_platform_filters = {};
Type m_type = Type::Unknown;
};
}

template <>
struct fmt::formatter<mamba::specs::ChannelSpec>
{
using ChannelSpec = ::mamba::specs::ChannelSpec;

constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator
{
// make sure that range is empty
if (ctx.begin() != ctx.end() && *ctx.begin() != '}')
{
throw fmt::format_error("Invalid format");
}
return ctx.begin();
}

auto format(const ChannelSpec& spec, format_context& ctx) const -> format_context::iterator;
};

#endif
16 changes: 16 additions & 0 deletions libmamba/src/core/channel_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ namespace mamba
return { std::move(params), has_zst };
}

auto ChannelContext::make_channel(specs::ChannelSpec spec) -> const channel_list&
{
auto str = spec.str();
if (const auto it = m_channel_cache.find(str); it != m_channel_cache.end())
{
return it->second;
}

auto [it, inserted] = m_channel_cache.emplace(
std::move(str),
Channel::resolve(std::move(spec), params())
);
assert(inserted);
return it->second;
}

auto ChannelContext::make_channel(std::string_view name) -> const channel_list&
{
if (const auto it = m_channel_cache.find(std::string(name)); it != m_channel_cache.end())
Expand Down
17 changes: 9 additions & 8 deletions libmamba/src/core/match_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ namespace mamba
parse(ctx, channel_context);
}

std::tuple<std::string, std::string> MatchSpec::parse_version_and_build(const std::string& s)
std::tuple<std::string, std::string> MatchSpec::parse_version_and_build(std::string_view s)
{
std::size_t pos = s.find_last_of(" =");
std::size_t const pos = s.find_last_of(" =");
if (pos == s.npos || pos == 0)
{
std::string tmp = s;
std::string tmp = std::string(s);
util::replace_all(tmp, " ", "");
return { tmp, "" };
return { std::move(tmp), "" };
}
else
{
Expand All @@ -59,17 +59,18 @@ namespace mamba
char d = s[pm1];
if (d == '=' || d == '!' || d == '|' || d == ',' || d == '<' || d == '>' || d == '~')
{
std::string tmp = s;
auto tmp = std::string(s);
util::replace_all(tmp, " ", "");
return { tmp, "" };
}
}
// c is either ' ' or pm1 is none of the forbidden chars

std::string v = s.substr(0, pos), b = s.substr(pos + 1);
auto v = std::string(s.substr(0, pos));
auto b = std::string(s.substr(pos + 1));
util::replace_all(v, " ", "");
util::replace_all(b, " ", "");
return { v, b };
return { std::move(v), std::move(b) };
}
}

Expand Down Expand Up @@ -124,7 +125,7 @@ namespace mamba

auto extract_kv = [&spec_str](const std::string& kv_string, auto& map)
{
static std::regex kv_re("([a-zA-Z0-9_-]+?)=([\"\']?)([^\'\"]*?)(\\2)(?:[\'\", ]|$)");
static const std::regex kv_re("([a-zA-Z0-9_-]+?)=([\"\']?)([^\'\"]*?)(\\2)(?:[\'\", ]|$)");
std::cmatch kv_match;
const char* text_iter = kv_string.c_str();

Expand Down
19 changes: 18 additions & 1 deletion libmamba/src/specs/channel_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace mamba::specs
if (m_type == Type::Unknown)
{
m_location = unknown_channel;
m_platform_filters = {};
// Allowing in any platform filters for unkown type can be useful in MatchSpec
}
if (m_location.empty())
{
Expand Down Expand Up @@ -208,4 +208,21 @@ namespace mamba::specs
{
return std::exchange(m_platform_filters, {});
}

auto ChannelSpec::str() const -> std::string
{
return fmt::format("{}", *this);
}
}

auto
fmt::formatter<mamba::specs::ChannelSpec>::format(const ChannelSpec& spec, format_context& ctx) const
-> format_context::iterator
{
auto out = fmt::format_to(ctx.out(), "{}", spec.location());
if (!spec.platform_filters().empty())
{
out = fmt::format_to(ctx.out(), "[{}]", fmt::join(spec.platform_filters(), ","));
}
return out;
}
1 change: 1 addition & 0 deletions libmamba/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(
src/core/test_activation.cpp
src/core/test_channel_context.cpp
src/core/test_configuration.cpp
src/core/test_match_spec.cpp
src/core/test_cpp.cpp
src/core/test_downloader.cpp
src/core/test_env_file_reading.cpp
Expand Down
Loading

0 comments on commit dfcb8c2

Please sign in to comment.