Skip to content

Commit

Permalink
Refactor MatchSpec::str (#3215)
Browse files Browse the repository at this point in the history
* Add split_once_on_any

* Split MatchSpec::track_features

* Add VersionSpec::expression_size

* Add UnresolvedChannel::is_package

* Refactor MatchSpec::str to use fmt

* Forbid version of in MatchSpec in attributes

* Document more Conda discrepencies
  • Loading branch information
AntoinePrv authored Mar 8, 2024
1 parent a00ef35 commit c54ab6c
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 195 deletions.
20 changes: 17 additions & 3 deletions libmamba/include/mamba/specs/match_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
#include <string>
#include <string_view>

#include <fmt/core.h>

#include "mamba/specs/build_number_spec.hpp"
#include "mamba/specs/error.hpp"
#include "mamba/specs/glob_spec.hpp"
#include "mamba/specs/unresolved_channel.hpp"
#include "mamba/specs/version_spec.hpp"
#include "mamba/util/flat_set.hpp"
#include "mamba/util/heap_optional.hpp"

namespace mamba::specs
Expand All @@ -29,6 +32,8 @@ namespace mamba::specs
using BuildStringSpec = GlobSpec;
using platform_set = typename UnresolvedChannel::platform_set;
using platform_set_const_ref = std::reference_wrapper<const platform_set>;
using string_set = typename util::flat_set<std::string>;
using string_set_const_ref = typename std::reference_wrapper<const string_set>;

inline static constexpr char url_md5_sep = '#';
inline static constexpr char prefered_list_open = '[';
Expand All @@ -41,6 +46,7 @@ namespace mamba::specs
inline static constexpr char attribute_sep = ',';
inline static constexpr char attribute_assign = '=';
inline static constexpr auto package_version_sep = std::array{ ' ', '=', '<', '>', '~', '!' };
inline static constexpr auto feature_sep = std::array{ ' ', ',' };


[[nodiscard]] static auto parse(std::string_view spec) -> expected_parse_t<MatchSpec>;
Expand Down Expand Up @@ -88,8 +94,8 @@ namespace mamba::specs
[[nodiscard]] auto features() const -> std::string_view;
void set_features(std::string val);

[[nodiscard]] auto track_features() const -> std::string_view;
void set_track_features(std::string val);
[[nodiscard]] auto track_features() const -> std::optional<string_set_const_ref>;
void set_track_features(string_set val);

[[nodiscard]] auto optional() const -> bool;
void set_optional(bool opt);
Expand All @@ -112,7 +118,7 @@ namespace mamba::specs
std::string license = {};
std::string license_family = {};
std::string features = {};
std::string track_features = {};
string_set track_features = {};
bool optional = false;
};

Expand Down Expand Up @@ -142,4 +148,12 @@ namespace mamba::specs
auto operator""_ms(const char* str, std::size_t len) -> MatchSpec;
}
}

template <>
struct fmt::formatter<::mamba::specs::MatchSpec>
{
auto parse(format_parse_context& ctx) -> decltype(ctx.begin());

auto format(const ::mamba::specs::MatchSpec& spec, format_context& ctx) -> decltype(ctx.out());
};
#endif
2 changes: 2 additions & 0 deletions libmamba/include/mamba/specs/unresolved_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ namespace mamba::specs
[[nodiscard]] auto platform_filters() && -> platform_set;
auto clear_platform_filters() -> platform_set;

[[nodiscard]] auto is_package() const -> bool;

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

private:
Expand Down
5 changes: 5 additions & 0 deletions libmamba/include/mamba/specs/version_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ namespace mamba::specs
*/
[[nodiscard]] auto contains(const Version& point) const -> bool;

/**
* Return the size of the boolean expression tree.
*/
[[nodiscard]] auto expression_size() const -> std::size_t;

private:

tree_type m_tree;
Expand Down
26 changes: 26 additions & 0 deletions libmamba/include/mamba/util/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ namespace mamba::util
[[nodiscard]] auto rsplit_once(std::string_view str, std::string_view sep)
-> std::tuple<std::optional<std::string_view>, std::string_view>;

[[nodiscard]] auto split_once_on_any(std::string_view str, std::string_view many_seps)
-> std::tuple<std::string_view, std::optional<std::string_view>>;
template <std::size_t N>
[[nodiscard]] auto split_once_on_any(std::string_view str, std::array<char, N> many_seps)
-> std::tuple<std::string_view, std::optional<std::string_view>>;

[[nodiscard]] auto rsplit_once_on_any(std::string_view str, std::string_view many_seps)
-> std::tuple<std::optional<std::string_view>, std::string_view>;
template <std::size_t N>
[[nodiscard]] auto rsplit_once_on_any(std::string_view str, std::array<char, N> many_seps)
-> std::tuple<std::optional<std::string_view>, std::string_view>;

[[nodiscard]] auto
split(std::string_view input, std::string_view sep, std::size_t max_split = SIZE_MAX)
-> std::vector<std::string>;
Expand Down Expand Up @@ -543,6 +555,20 @@ namespace mamba::util
return detail::strip_if_parts_impl(input, std::move(should_strip));
}

template <std::size_t N>
auto split_once_on_any(std::string_view str, std::array<char, N> many_seps)
-> std::tuple<std::string_view, std::optional<std::string_view>>
{
return split_once_on_any(str, std::string_view{ many_seps.data(), many_seps.size() });
}

template <std::size_t N>
auto rsplit_once_on_any(std::string_view str, std::array<char, N> many_seps)
-> std::tuple<std::optional<std::string_view>, std::string_view>
{
return rsplit_once_on_any(str, std::string_view{ many_seps.data(), many_seps.size() });
}

/**************************************
* Implementation of join functions *
**************************************/
Expand Down
Loading

0 comments on commit c54ab6c

Please sign in to comment.