c_str.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <range/v3/detail/prologue.hpp>
  22. namespace ranges
  23. {
  24. /// \cond
  25. namespace detail
  26. {
  27. template<typename T>
  28. struct is_char_type_ : std::false_type
  29. {};
  30. template<>
  31. struct is_char_type_<char> : std::true_type
  32. {};
  33. template<>
  34. struct is_char_type_<wchar_t> : std::true_type
  35. {};
  36. template<>
  37. struct is_char_type_<char16_t> : std::true_type
  38. {};
  39. template<>
  40. struct is_char_type_<char32_t> : std::true_type
  41. {};
  42. template<typename T>
  43. using is_char_type = is_char_type_<meta::_t<std::remove_cv<T>>>;
  44. } // namespace detail
  45. /// \endcond
  46. /// \addtogroup group-views
  47. /// @{
  48. namespace views
  49. {
  50. /// View a `\0`-terminated C string (e.g. from a const char*) as a
  51. /// range.
  52. struct c_str_fn
  53. {
  54. // Fixed-length
  55. template(typename Char, std::size_t N)(
  56. requires detail::is_char_type<Char>::value) //
  57. ranges::subrange<Char *> operator()(Char (&sz)[N]) const
  58. {
  59. return {&sz[0], &sz[N - 1]};
  60. }
  61. // Null-terminated
  62. template(typename Char)(
  63. requires detail::is_char_type<Char>::value) //
  64. ranges::delimit_view<
  65. ranges::subrange<Char *, ranges::unreachable_sentinel_t>,
  66. meta::_t<std::remove_cv<Char>>> //
  67. operator()(Char * sz) const volatile
  68. {
  69. using ch_t = meta::_t<std::remove_cv<Char>>;
  70. return ranges::views::delimit(sz, ch_t(0));
  71. }
  72. };
  73. /// \relates c_str_fn
  74. /// \ingroup group-views
  75. RANGES_INLINE_VARIABLE(c_str_fn, c_str)
  76. } // namespace views
  77. } // namespace ranges
  78. #include <range/v3/detail/epilogue.hpp>
  79. #endif