You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.hxx 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2023 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_UTIL_HXX
  17. #define RSPAMD_UTIL_HXX
  18. #pragma once
  19. #include <memory>
  20. #include <array>
  21. #include <string_view>
  22. #include <optional>
  23. #include <tuple>
  24. #include <algorithm>
  25. /*
  26. * Common C++ utilities
  27. */
  28. namespace rspamd {
  29. /*
  30. * Creates std::array from a standard C style array with automatic size calculation
  31. */
  32. template<typename... Ts>
  33. constexpr auto array_of(Ts &&...t) -> std::array<typename std::decay_t<typename std::common_type_t<Ts...>>, sizeof...(Ts)>
  34. {
  35. using T = typename std::decay_t<typename std::common_type_t<Ts...>>;
  36. return {{std::forward<T>(t)...}};
  37. }
  38. /**
  39. * Find a value in a map
  40. * @tparam C Map type
  41. * @tparam K Key type
  42. * @tparam V Value type
  43. * @param c Map to search
  44. * @param k Key to search
  45. * @return Value if found or std::nullopt otherwise
  46. */
  47. 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>
  48. constexpr auto find_map(const C &c, const K &k) -> std::optional<std::reference_wrapper<const V>>
  49. {
  50. auto f = c.find(k);
  51. if (f != c.end()) {
  52. return std::cref<V>(f->second);
  53. }
  54. return std::nullopt;
  55. }
  56. template<typename It>
  57. inline constexpr auto make_string_view_from_it(It begin, It end)
  58. {
  59. using result_type = std::string_view;
  60. return result_type{((begin != end) ? &*begin : nullptr),
  61. (typename result_type::size_type) std::max(std::distance(begin, end),
  62. (typename result_type::difference_type) 0)};
  63. }
  64. /**
  65. * Iterate over lines in a string, newline characters are dropped
  66. * @tparam S
  67. * @tparam F
  68. * @param input
  69. * @param functor
  70. * @return
  71. */
  72. template<class S, class F, typename std::enable_if_t<std::is_invocable_v<F, std::string_view> && std::is_constructible_v<std::string_view, S>, bool> = true>
  73. inline auto string_foreach_line(const S &input, const F &functor)
  74. {
  75. auto it = input.begin();
  76. auto end = input.end();
  77. while (it != end) {
  78. auto next = std::find(it, end, '\n');
  79. while (next >= it && (*next == '\n' || *next == '\r')) {
  80. --next;
  81. }
  82. functor(make_string_view_from_it(it, next));
  83. it = next;
  84. if (it != end) {
  85. ++it;
  86. }
  87. }
  88. }
  89. /**
  90. * Iterate over elements in a string
  91. * @tparam S string type
  92. * @tparam D delimiter type
  93. * @tparam F functor type
  94. * @param input string to iterate
  95. * @param delim delimiter to use
  96. * @param functor functor to call
  97. * @param ignore_empty ignore empty elements
  98. * @return nothing
  99. */
  100. template<class S, class D, class F,
  101. 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>
  102. inline auto string_foreach_delim(const S &input, const D &delim, const F &functor, const bool ignore_empty = true) -> void
  103. {
  104. size_t first = 0;
  105. auto sv_input = std::string_view{input};
  106. auto sv_delim = std::string_view{delim};
  107. while (first < sv_input.size()) {
  108. const auto second = sv_input.find_first_of(sv_delim, first);
  109. if (first != second || !ignore_empty) {
  110. functor(sv_input.substr(first, second - first));
  111. }
  112. if (second == std::string_view::npos) {
  113. break;
  114. }
  115. first = second + 1;
  116. }
  117. }
  118. /**
  119. * Split string on a character
  120. * @tparam S string type
  121. * @param input string to split
  122. * @param chr character to split on
  123. * @return pair of strings
  124. */
  125. template<class S, typename std::enable_if_t<std::is_constructible_v<std::string_view, S>, bool> = true>
  126. inline auto string_split_on(const S &input, std::string_view::value_type chr) -> std::pair<std::string_view, std::string_view>
  127. {
  128. auto pos = std::find(std::begin(input), std::end(input), chr);
  129. if (pos != input.end()) {
  130. auto first = std::string_view{std::begin(input), static_cast<std::size_t>(std::distance(std::begin(input), pos))};
  131. while (*pos == chr && pos != input.end()) {
  132. ++pos;
  133. }
  134. auto last = std::string_view{pos, static_cast<std::size_t>(std::distance(pos, std::end(input)))};
  135. return {first, last};
  136. }
  137. return {std::string_view{input}, std::string_view{}};
  138. }
  139. /**
  140. * Enumerate for range loop
  141. * @tparam T iterable type
  142. * @tparam TIter iterator type
  143. * @param iterable iterable object
  144. * @return iterator object
  145. */
  146. template<typename T,
  147. typename TIter = decltype(std::begin(std::declval<T>())),
  148. typename = decltype(std::end(std::declval<T>()))>
  149. constexpr auto enumerate(T &&iterable)
  150. {
  151. struct iterator {
  152. size_t i;
  153. TIter iter;
  154. bool operator!=(const iterator &other) const
  155. {
  156. return iter != other.iter;
  157. }
  158. void operator++()
  159. {
  160. ++i;
  161. ++iter;
  162. }
  163. auto operator*() const
  164. {
  165. return std::tie(i, *iter);
  166. }
  167. };
  168. struct iterable_wrapper {
  169. T iterable;
  170. auto begin()
  171. {
  172. return iterator{0, std::begin(iterable)};
  173. }
  174. auto end()
  175. {
  176. return iterator{0, std::end(iterable)};
  177. }
  178. };
  179. return iterable_wrapper{std::forward<T>(iterable)};
  180. }
  181. /**
  182. * Allocator that cleans up memory in a secure way on destruction
  183. * @tparam T
  184. */
  185. template<class T>
  186. class secure_mem_allocator : public std::allocator<T> {
  187. public:
  188. using value_type = typename std::allocator<T>::value_type;
  189. using size_type = typename std::allocator<T>::size_type;
  190. template<class U>
  191. struct rebind {
  192. typedef secure_mem_allocator<U> other;
  193. };
  194. secure_mem_allocator() noexcept = default;
  195. secure_mem_allocator(const secure_mem_allocator &_) noexcept
  196. : std::allocator<T>(_)
  197. {
  198. }
  199. template<class U>
  200. explicit secure_mem_allocator(const secure_mem_allocator<U> &) noexcept
  201. {
  202. }
  203. void deallocate(value_type *p, size_type num) noexcept
  204. {
  205. rspamd_explicit_memzero((void *) p, num);
  206. std::allocator<T>::deallocate(p, num);
  207. }
  208. };
  209. }// namespace rspamd
  210. #endif//RSPAMD_UTIL_HXX