16#include <userver/utils/checked_pointer.hpp>
18USERVER_NAMESPACE_BEGIN
23template <
typename ResultString = std::string,
typename... Strings>
24ResultString
StrCat(
const Strings&... strings) {
25 return [](
auto... string_views) {
26 std::size_t result_size = 0;
27 ((result_size += string_views.size()), ...);
30 result.reserve(result_size);
31 (result.append(string_views), ...);
33 }(std::string_view{strings}...);
38template <
class Map,
class Key>
40 const auto it = map.find(key);
41 return (it == map.end() ?
nullptr : &it->second);
46template <
class Map,
class Key,
class Default>
49 const auto* ptr = utils::FindOrNullptr(map, key);
51 return {std::forward<Default>(def)};
58template <
class Map,
class Key>
60 const auto ptr = USERVER_NAMESPACE::utils::FindOrNullptr(map, key);
69template <
class Map,
class Key>
72 const auto ptr = utils::FindOrNullptr(map, key);
81template <
typename Map,
typename Key>
83 ->
decltype(utils::MakeCheckedPtr(&map.find(key)->second)) {
84 if (
auto f = map.find(key); f != map.end()) {
85 return utils::MakeCheckedPtr(&f->second);
91template <
class ToContainer,
class FromContainer>
93 if constexpr (std::is_rvalue_reference_v<
decltype(container)>) {
94 return ToContainer(std::make_move_iterator(std::begin(container)),
95 std::make_move_iterator(std::end(container)));
97 return ToContainer(std::begin(container), std::end(container));
102template <
typename Container,
typename =
void>
103struct HasKeyType : std::false_type {};
105template <
typename Container>
106struct HasKeyType<Container, std::void_t<
typename Container::key_type>>
109template <
typename Container>
110inline constexpr bool kHasKeyType = HasKeyType<Container>::value;
114template <
class Container,
class Pred>
115auto EraseIf(Container& container, Pred pred) {
116 if constexpr (impl::kHasKeyType<Container>) {
117 auto old_size = container.size();
118 for (
auto it = std::begin(container), last = std::end(container);
121 it = container.erase(it);
126 return old_size - container.size();
128 auto it = std::remove_if(std::begin(container), std::end(container), pred);
129 const auto removed = std::distance(it, std::end(container));
130 container.erase(it, std::end(container));
136template <
class Container,
class T>
138 if constexpr (impl::kHasKeyType<Container>) {
139 return container.erase(elem);
142 auto it = std::remove(std::begin(container), std::end(container), elem);
143 const auto removed = std::distance(it, std::end(container));
144 container.erase(it, std::end(container));
151template <
typename Container,
typename Pred>
153 return std::find_if(std::begin(container), std::end(container), pred) !=