Skip to content

Commit

Permalink
[Minor] Fix some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 17, 2023
1 parent 3e816ad commit 87c6b54
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/libutil/cxx/util.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ constexpr auto array_of(Ts &&...t) -> std::array<typename std::decay_t<typename
return {{std::forward<T>(t)...}};
}

/**
* Find a value in a map
* @tparam C Map type
* @tparam K Key type
* @tparam V Value type
* @param c Map to search
* @param k Key to search
* @return Value if found or std::nullopt otherwise
*/
template<class C, class K, class V = typename C::mapped_type, typename std::enable_if_t<std::is_constructible_v<typename C::key_type, K> && std::is_constructible_v<typename C::mapped_type, V>, bool> = false>
constexpr auto find_map(const C &c, const K &k) -> std::optional<std::reference_wrapper<const V>>
{
Expand Down Expand Up @@ -93,17 +102,18 @@ inline auto string_foreach_line(const S &input, const F &functor)

/**
* Iterate over elements in a string
* @tparam S
* @tparam F
* @param input
* @param delim
* @param functor
* @param ignore_empty
* @return
* @tparam S string type
* @tparam D delimiter type
* @tparam F functor type
* @param input string to iterate
* @param delim delimiter to use
* @param functor functor to call
* @param ignore_empty ignore empty elements
* @return nothing
*/
template<class S, class D, class F,
typename std::enable_if_t<std::is_invocable_v<F, std::string_view> && std::is_constructible_v<std::string_view, S> && std::is_constructible_v<std::string_view, D>, bool> = true>
inline auto string_foreach_delim(const S &input, const D &delim, const F &functor, const bool ignore_empty = true)
inline auto string_foreach_delim(const S &input, const D &delim, const F &functor, const bool ignore_empty = true) -> void
{
size_t first = 0;
auto sv_input = std::string_view{input};
Expand All @@ -124,6 +134,13 @@ inline auto string_foreach_delim(const S &input, const D &delim, const F &functo
}
}

/**
* Split string on a character
* @tparam S string type
* @param input string to split
* @param chr character to split on
* @return pair of strings
*/
template<class S, typename std::enable_if_t<std::is_constructible_v<std::string_view, S>, bool> = true>
inline auto string_split_on(const S &input, std::string_view::value_type chr) -> std::pair<std::string_view, std::string_view>
{
Expand All @@ -144,6 +161,10 @@ inline auto string_split_on(const S &input, std::string_view::value_type chr) ->

/**
* Enumerate for range loop
* @tparam T iterable type
* @tparam TIter iterator type
* @param iterable iterable object
* @return iterator object
*/
template<typename T,
typename TIter = decltype(std::begin(std::declval<T>())),
Expand Down

0 comments on commit 87c6b54

Please sign in to comment.