diff options
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/cxx/util.hxx | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 4ec1d568c..32ec0b55c 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -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>> { @@ -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}; @@ -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> { @@ -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>())), |