Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

c_str.hpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_C_STR_HPP
  14. #define RANGES_V3_VIEW_C_STR_HPP
  15. #include <type_traits>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/iterator/unreachable_sentinel.hpp>
  18. #include <range/v3/utility/static_const.hpp>
  19. #include <range/v3/view/delimit.hpp>
  20. #include <range/v3/view/subrange.hpp>
  21. namespace ranges
  22. {
  23. /// \cond
  24. namespace detail
  25. {
  26. template<typename T>
  27. struct is_char_type_ : std::false_type
  28. {};
  29. template<>
  30. struct is_char_type_<char> : std::true_type
  31. {};
  32. template<>
  33. struct is_char_type_<wchar_t> : std::true_type
  34. {};
  35. template<>
  36. struct is_char_type_<char16_t> : std::true_type
  37. {};
  38. template<>
  39. struct is_char_type_<char32_t> : std::true_type
  40. {};
  41. template<typename T>
  42. using is_char_type = is_char_type_<meta::_t<std::remove_cv<T>>>;
  43. } // namespace detail
  44. /// \endcond
  45. /// \addtogroup group-views
  46. /// @{
  47. namespace views
  48. {
  49. /// View a `\0`-terminated C string (e.g. from a const char*) as a
  50. /// range.
  51. struct c_str_fn
  52. {
  53. // Fixed-length
  54. template<typename Char, std::size_t N>
  55. auto operator()(Char (&sz)[N]) const -> CPP_ret(ranges::subrange<Char *>)( //
  56. requires detail::is_char_type<Char>::value)
  57. {
  58. return {&sz[0], &sz[N - 1]};
  59. }
  60. // Null-terminated
  61. template<typename Char>
  62. auto operator()(Char * sz) const volatile
  63. -> CPP_ret(ranges::delimit_view<
  64. ranges::subrange<Char *, ranges::unreachable_sentinel_t>,
  65. meta::_t<std::remove_cv<Char>>>)( //
  66. requires detail::is_char_type<Char>::value)
  67. {
  68. using ch_t = meta::_t<std::remove_cv<Char>>;
  69. return ranges::views::delimit(sz, ch_t(0));
  70. }
  71. };
  72. /// \relates c_str_fn
  73. /// \ingroup group-views
  74. RANGES_INLINE_VARIABLE(c_str_fn, c_str)
  75. } // namespace views
  76. } // namespace ranges
  77. #endif