]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::string`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
6 | Distributed under the Boost Software License, Version 1.0. | |
7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) | |
8 | */ | |
9 | ||
10 | #ifndef BOOST_HANA_STRING_HPP | |
11 | #define BOOST_HANA_STRING_HPP | |
12 | ||
13 | #include <boost/hana/fwd/string.hpp> | |
14 | ||
15 | #include <boost/hana/bool.hpp> | |
16 | #include <boost/hana/config.hpp> | |
17 | #include <boost/hana/core/make.hpp> | |
18 | #include <boost/hana/detail/algorithm.hpp> | |
19 | #include <boost/hana/detail/operators/adl.hpp> | |
20 | #include <boost/hana/detail/operators/comparable.hpp> | |
21 | #include <boost/hana/detail/operators/iterable.hpp> | |
22 | #include <boost/hana/detail/operators/orderable.hpp> | |
23 | #include <boost/hana/fwd/at.hpp> | |
24 | #include <boost/hana/fwd/contains.hpp> | |
25 | #include <boost/hana/fwd/core/tag_of.hpp> | |
26 | #include <boost/hana/fwd/core/to.hpp> | |
27 | #include <boost/hana/fwd/drop_front.hpp> | |
28 | #include <boost/hana/fwd/equal.hpp> | |
29 | #include <boost/hana/fwd/find.hpp> | |
30 | #include <boost/hana/fwd/front.hpp> | |
31 | #include <boost/hana/fwd/hash.hpp> | |
32 | #include <boost/hana/fwd/is_empty.hpp> | |
33 | #include <boost/hana/fwd/length.hpp> | |
34 | #include <boost/hana/fwd/less.hpp> | |
35 | #include <boost/hana/fwd/unpack.hpp> | |
36 | #include <boost/hana/if.hpp> | |
37 | #include <boost/hana/integral_constant.hpp> | |
38 | #include <boost/hana/optional.hpp> | |
39 | #include <boost/hana/type.hpp> | |
40 | ||
41 | #include <utility> | |
42 | #include <cstddef> | |
43 | #include <type_traits> | |
44 | ||
45 | ||
46 | BOOST_HANA_NAMESPACE_BEGIN | |
47 | ////////////////////////////////////////////////////////////////////////// | |
48 | // string<> | |
49 | ////////////////////////////////////////////////////////////////////////// | |
50 | //! @cond | |
51 | template <char ...s> | |
52 | struct string | |
53 | : detail::operators::adl<string<s...>> | |
54 | , detail::iterable_operators<string<s...>> | |
55 | { }; | |
56 | //! @endcond | |
57 | ||
58 | template <char ...s> | |
59 | struct tag_of<string<s...>> { | |
60 | using type = string_tag; | |
61 | }; | |
62 | ||
63 | ////////////////////////////////////////////////////////////////////////// | |
64 | // make<string_tag> | |
65 | ////////////////////////////////////////////////////////////////////////// | |
66 | template <> | |
67 | struct make_impl<string_tag> { | |
68 | template <typename ...Chars> | |
69 | static constexpr auto apply(Chars const& ...) { | |
70 | return hana::string<hana::value<Chars>()...>{}; | |
71 | } | |
72 | }; | |
73 | ||
74 | ////////////////////////////////////////////////////////////////////////// | |
75 | // BOOST_HANA_STRING | |
76 | ////////////////////////////////////////////////////////////////////////// | |
77 | namespace string_detail { | |
78 | template <typename S, std::size_t ...N> | |
79 | constexpr string<S::get()[N]...> | |
80 | prepare_impl(S, std::index_sequence<N...>) | |
81 | { return {}; } | |
82 | ||
83 | template <typename S> | |
84 | constexpr decltype(auto) prepare(S s) { | |
85 | return prepare_impl(s, | |
86 | std::make_index_sequence<sizeof(S::get()) - 1>{}); | |
87 | } | |
88 | } | |
89 | ||
90 | #define BOOST_HANA_STRING(s) \ | |
91 | (::boost::hana::string_detail::prepare([]{ \ | |
92 | struct tmp { \ | |
93 | static constexpr decltype(auto) get() { return s; } \ | |
94 | }; \ | |
95 | return tmp{}; \ | |
96 | }())) \ | |
97 | /**/ | |
98 | ||
99 | #ifdef BOOST_HANA_CONFIG_ENABLE_STRING_UDL | |
100 | ////////////////////////////////////////////////////////////////////////// | |
101 | // _s user-defined literal | |
102 | ////////////////////////////////////////////////////////////////////////// | |
103 | namespace literals { | |
104 | template <typename CharT, CharT ...s> | |
105 | constexpr auto operator"" _s() { | |
106 | static_assert(std::is_same<CharT, char>::value, | |
107 | "hana::string: Only narrow string literals are supported with " | |
108 | "the _s string literal right now. See https://goo.gl/fBbKD7 " | |
109 | "if you need support for fancier types of compile-time strings."); | |
110 | return hana::string_c<s...>; | |
111 | } | |
112 | } | |
113 | #endif | |
114 | ||
115 | ////////////////////////////////////////////////////////////////////////// | |
116 | // Operators | |
117 | ////////////////////////////////////////////////////////////////////////// | |
118 | namespace detail { | |
119 | template <> | |
120 | struct comparable_operators<string_tag> { | |
121 | static constexpr bool value = true; | |
122 | }; | |
123 | template <> | |
124 | struct orderable_operators<string_tag> { | |
125 | static constexpr bool value = true; | |
126 | }; | |
127 | } | |
128 | ||
129 | ////////////////////////////////////////////////////////////////////////// | |
130 | // to<char const*> | |
131 | ////////////////////////////////////////////////////////////////////////// | |
132 | template <> | |
133 | struct to_impl<char const*, string_tag> { | |
134 | template <char ...c> | |
135 | static constexpr char const c_string[sizeof...(c) + 1] = {c..., '\0'}; | |
136 | ||
137 | template <char ...c> | |
138 | static constexpr char const* apply(string<c...> const&) | |
139 | { return c_string<c...>; } | |
140 | }; | |
141 | ||
142 | //! @cond | |
143 | template <char ...c> | |
144 | constexpr char const to_impl<char const*, string_tag>::c_string[sizeof...(c) + 1]; | |
145 | //! @endcond | |
146 | ||
147 | ////////////////////////////////////////////////////////////////////////// | |
148 | // Comparable | |
149 | ////////////////////////////////////////////////////////////////////////// | |
150 | template <> | |
151 | struct equal_impl<string_tag, string_tag> { | |
152 | template <typename S> | |
153 | static constexpr auto apply(S const&, S const&) | |
154 | { return hana::true_c; } | |
155 | ||
156 | template <typename S1, typename S2> | |
157 | static constexpr auto apply(S1 const&, S2 const&) | |
158 | { return hana::false_c; } | |
159 | }; | |
160 | ||
161 | ////////////////////////////////////////////////////////////////////////// | |
162 | // Orderable | |
163 | ////////////////////////////////////////////////////////////////////////// | |
164 | template <> | |
165 | struct less_impl<string_tag, string_tag> { | |
166 | template <char ...s1, char ...s2> | |
167 | static constexpr auto | |
168 | apply(string<s1...> const&, string<s2...> const&) { | |
169 | // We put a '\0' at the end only to avoid empty arrays. | |
170 | constexpr char const c_str1[] = {s1..., '\0'}; | |
171 | constexpr char const c_str2[] = {s2..., '\0'}; | |
172 | return hana::bool_c<detail::lexicographical_compare( | |
173 | c_str1, c_str1 + sizeof...(s1), | |
174 | c_str2, c_str2 + sizeof...(s2) | |
175 | )>; | |
176 | } | |
177 | }; | |
178 | ||
179 | ////////////////////////////////////////////////////////////////////////// | |
180 | // Foldable | |
181 | ////////////////////////////////////////////////////////////////////////// | |
182 | template <> | |
183 | struct unpack_impl<string_tag> { | |
184 | template <char ...s, typename F> | |
185 | static constexpr decltype(auto) apply(string<s...> const&, F&& f) | |
186 | { return static_cast<F&&>(f)(char_<s>{}...); } | |
187 | }; | |
188 | ||
189 | template <> | |
190 | struct length_impl<string_tag> { | |
191 | template <char ...s> | |
192 | static constexpr auto apply(string<s...> const&) | |
193 | { return hana::size_c<sizeof...(s)>; } | |
194 | }; | |
195 | ||
196 | ////////////////////////////////////////////////////////////////////////// | |
197 | // Iterable | |
198 | ////////////////////////////////////////////////////////////////////////// | |
199 | template <> | |
200 | struct front_impl<string_tag> { | |
201 | template <char x, char ...xs> | |
202 | static constexpr auto apply(string<x, xs...> const&) | |
203 | { return hana::char_c<x>; } | |
204 | }; | |
205 | ||
206 | template <> | |
207 | struct drop_front_impl<string_tag> { | |
208 | template <std::size_t N, char ...xs, std::size_t ...i> | |
209 | static constexpr auto helper(string<xs...> const&, std::index_sequence<i...>) { | |
210 | constexpr char s[] = {xs...}; | |
211 | return hana::string_c<s[i + N]...>; | |
212 | } | |
213 | ||
214 | template <char ...xs, typename N> | |
215 | static constexpr auto apply(string<xs...> const& s, N const&) { | |
216 | return helper<N::value>(s, std::make_index_sequence< | |
217 | N::value < sizeof...(xs) ? sizeof...(xs) - N::value : 0 | |
218 | >{}); | |
219 | } | |
220 | ||
221 | template <typename N> | |
222 | static constexpr auto apply(string<> const& s, N const&) | |
223 | { return s; } | |
224 | }; | |
225 | ||
226 | template <> | |
227 | struct is_empty_impl<string_tag> { | |
228 | template <char ...s> | |
229 | static constexpr auto apply(string<s...> const&) | |
230 | { return hana::bool_c<sizeof...(s) == 0>; } | |
231 | }; | |
232 | ||
233 | template <> | |
234 | struct at_impl<string_tag> { | |
235 | template <char ...s, typename N> | |
236 | static constexpr auto apply(string<s...> const&, N const&) { | |
237 | // We put a '\0' at the end to avoid an empty array. | |
238 | constexpr char characters[] = {s..., '\0'}; | |
239 | constexpr auto n = N::value; | |
240 | return hana::char_c<characters[n]>; | |
241 | } | |
242 | }; | |
243 | ||
244 | ////////////////////////////////////////////////////////////////////////// | |
245 | // Searchable | |
246 | ////////////////////////////////////////////////////////////////////////// | |
247 | template <> | |
248 | struct contains_impl<string_tag> { | |
249 | template <char ...s, typename C> | |
250 | static constexpr auto | |
251 | helper(string<s...> const&, C const&, hana::true_) { | |
252 | constexpr char const characters[] = {s..., '\0'}; | |
253 | constexpr char c = hana::value<C>(); | |
254 | return hana::bool_c< | |
255 | detail::find(characters, characters + sizeof...(s), c) | |
256 | != characters + sizeof...(s) | |
257 | >; | |
258 | } | |
259 | ||
260 | template <typename S, typename C> | |
261 | static constexpr auto helper(S const&, C const&, hana::false_) | |
262 | { return hana::false_c; } | |
263 | ||
264 | template <typename S, typename C> | |
265 | static constexpr auto apply(S const& s, C const& c) | |
266 | { return helper(s, c, hana::bool_c<hana::Constant<C>::value>); } | |
267 | }; | |
268 | ||
269 | template <> | |
270 | struct find_impl<string_tag> { | |
271 | template <char ...s, typename Char> | |
272 | static constexpr auto apply(string<s...> const& str, Char const& c) { | |
273 | return hana::if_(contains_impl<string_tag>::apply(str, c), | |
274 | hana::just(c), | |
275 | hana::nothing | |
276 | ); | |
277 | } | |
278 | }; | |
279 | ||
280 | ////////////////////////////////////////////////////////////////////////// | |
281 | // Hashable | |
282 | ////////////////////////////////////////////////////////////////////////// | |
283 | template <> | |
284 | struct hash_impl<string_tag> { | |
285 | template <typename String> | |
286 | static constexpr auto apply(String const&) { | |
287 | return hana::type_c<String>; | |
288 | } | |
289 | }; | |
290 | BOOST_HANA_NAMESPACE_END | |
291 | ||
292 | #endif // !BOOST_HANA_STRING_HPP |