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>
48 const auto* ptr =
utils::FindOrNullptr(map, key);
50 return {std::forward<Default>(def)};
57template <
class Map,
class Key>
59 const auto ptr = USERVER_NAMESPACE::
utils::FindOrNullptr(map, key);
68template <
class Map,
class Key>
70 const auto ptr =
utils::FindOrNullptr(map, key);
79template <
typename Map,
typename Key>
80auto CheckedFind(Map& map,
const Key& key) ->
decltype(
utils::MakeCheckedPtr(&map.find(key)->second)) {
81 if (
auto f = map.find(key); f != map.end()) {
82 return utils::MakeCheckedPtr(&f->second);
88template <
class ToContainer,
class FromContainer>
90 if constexpr (std::is_rvalue_reference_v<
decltype(container)>) {
92 std::make_move_iterator(std::begin(container)), std::make_move_iterator(std::end(container))
95 return ToContainer(std::begin(container), std::end(container));
100template <
typename Container,
typename =
void>
101struct HasKeyType : std::false_type {};
103template <
typename Container>
104struct HasKeyType<Container, std::void_t<
typename Container::key_type>> : std::true_type {};
106template <
typename Container>
107inline constexpr bool kHasKeyType = HasKeyType<Container>::value;
111template <
class Container,
class Pred>
112auto EraseIf(Container& container, Pred pred) {
113 if constexpr (impl::kHasKeyType<Container>) {
114 auto old_size = container.size();
115 for (
auto it = std::begin(container), last = std::end(container); it != last;) {
117 it = container.erase(it);
122 return old_size - container.size();
124 auto it = std::remove_if(std::begin(container), std::end(container), pred);
125 const auto removed = std::distance(it, std::end(container));
126 container.erase(it, std::end(container));
132template <
class Container,
class T>
134 if constexpr (impl::kHasKeyType<Container>) {
135 return container.erase(elem);
138 auto it = std::remove(std::begin(container), std::end(container), elem);
139 const auto removed = std::distance(it, std::end(container));
140 container.erase(it, std::end(container));
147template <
typename Container,
typename Pred>
149 return std::find_if(std::begin(container), std::end(container), pred) != std::end(container);