string_helpers.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2023 John Maddock.
  3. // Copyright Christopher Kormanyos 2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MP_DETAIL_STRING_HELPERS_HPP
  8. #define BOOST_MP_DETAIL_STRING_HELPERS_HPP
  9. #include <algorithm>
  10. #include <cstring>
  11. namespace boost { namespace multiprecision { namespace detail {
  12. struct is_in_string
  13. {
  14. const char* begin;
  15. const char* end;
  16. is_in_string(const char* p) : begin(p), end(p + std::strlen(p)) {}
  17. bool operator()(char s) { return std::find(begin, end, s) != end; }
  18. };
  19. struct is_not_in_string
  20. {
  21. const char* begin;
  22. const char* end;
  23. is_not_in_string(const char* p) : begin(p), end(p + std::strlen(p)) {}
  24. bool operator()(char s) { return std::find(begin, end, s) == end; }
  25. };
  26. template <class Iterator>
  27. std::size_t find_first_of(Iterator begin, Iterator end, const char* what)
  28. {
  29. return static_cast<std::size_t>(std::find_if(begin, end, is_in_string(what)) - begin);
  30. }
  31. template <class Iterator>
  32. std::size_t find_first_not_of(Iterator begin, Iterator end, const char* what)
  33. {
  34. return static_cast<std::size_t>(std::find_if(begin, end, is_not_in_string(what)) - begin);
  35. }
  36. }}} // namespace boost::multiprecision::detail
  37. #endif // BOOST_MP_DETAIL_STRING_HELPERS_HPP