Debian Codegraph @

3rd_party/range-v3/range/v3/view/c_str.hpp

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