-
Notifications
You must be signed in to change notification settings - Fork 6
/
string_concat_view.hpp
238 lines (200 loc) · 5.93 KB
/
string_concat_view.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// Copyright Jason Rice 2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef NBDL_DETAIL_STRING_CONCAT_VIEW_HPP
#define NBDL_DETAIL_STRING_CONCAT_VIEW_HPP
#include <array>
#include <boost/hana/ext/std/array.hpp>
#include <boost/hana/length.hpp>
#include <boost/hana/string.hpp>
#include <dyno/poly.hpp>
#include <experimental/string_view>
#include <type_traits>
#include <utility>
namespace nbdl::detail
{
namespace hana = boost::hana;
using std::experimental::string_view;
// Keeping stuff in the context so I don't have to write my
// own iterator which would probably result in more code
constexpr auto copy_s = BOOST_HANA_STRING("copy");
constexpr auto equal_s = BOOST_HANA_STRING("equal");
constexpr auto length_s = BOOST_HANA_STRING("length");
// TODO the interface for "equal" needs a second look :/
struct StringConcatView : decltype(dyno::requires(
hana::make_pair(copy_s, dyno::function<void(dyno::T const&, std::string::iterator)>)
, hana::make_pair(equal_s, dyno::function<bool(dyno::T const&, std::string::const_iterator)>)
, hana::make_pair(length_s, dyno::function<std::size_t(dyno::T const&)>)
)) { };
}
template <typename T>
constexpr auto dyno::default_concept_map<nbdl::detail::StringConcatView, T> = dyno::make_concept_map(
boost::hana::make_pair(
nbdl::detail::copy_s
, [](T const& self, std::string::iterator itr) { self.copy(itr); }
)
, boost::hana::make_pair(
nbdl::detail::equal_s
, [](T const& self, std::string::const_iterator itr) { return self.equal(itr); }
)
, boost::hana::make_pair(
nbdl::detail::length_s
, [](T const& self) { return self.length(); }
)
);
namespace nbdl::detail
{
template <typename T>
struct string_concat_view_impl;
template <>
struct string_concat_view_impl<string_view>
{
string_concat_view_impl(string_view x)
: storage{std::move(x)}
{ }
void copy(std::string::iterator itr) const
{
std::copy(storage.begin(), storage.end(), itr);
}
bool equal(std::string::const_iterator itr) const
{
return std::equal(storage.begin(), storage.end(), itr);
}
std::size_t length() const
{
return storage.length();
}
private:
string_view storage;
};
static_assert(
std::is_copy_constructible<string_concat_view_impl<string_view>>::value
);
// The actual dynamic type
struct string_concat_view
{
string_concat_view()
: poly_{string_concat_view_impl<string_view>{string_view{}}}
{ }
template <typename T>
string_concat_view(T x)
: poly_{string_concat_view_impl<T>{std::move(x)}}
{ }
std::string to_string() const
{
std::string result(length(), 0);
copy(result.begin());
return result;
}
void copy(std::string::iterator itr) const
{
poly_.virtual_(copy_s)(poly_, itr);
}
bool equal(std::string::const_iterator itr) const
{
return poly_.virtual_(equal_s)(poly_, itr);
}
std::size_t length() const
{
return poly_.virtual_(length_s)(poly_);
}
private:
dyno::poly<StringConcatView> poly_;
};
static_assert(std::is_copy_constructible<string_concat_view>::value);
// Impl for arrays of string_concat_view
template <std::size_t size>
struct string_concat_view_impl<std::array<string_concat_view, size>>
{
string_concat_view_impl(std::array<string_concat_view, size> x)
: storage{std::move(x)}
{ }
void copy(std::string::iterator itr) const
{
std::string::iterator current = itr;
hana::for_each(storage, [&](auto const& view)
{
view.copy(current);
current += view.length();
});
}
bool equal(std::string::const_iterator itr) const
{
std::string::const_iterator current = itr;
bool result = true;
hana::for_each(storage, [&](auto const& view)
{
// TODO Consider using nbdl::promise once compile times are under control
if (result)
{
result = result && view.equal(current);
current += view.length();
}
});
return result;
}
std::size_t length() const
{
return hana::unpack(storage, [](auto const& ...x)
{
return (x.length() + ... + 0);
});
}
private:
std::array<string_concat_view, size> storage;
};
static_assert(
std::is_copy_constructible<string_concat_view_impl<std::array<string_concat_view, 5>>>::value
);
// to_string_concat_view
struct to_string_concat_view_fn
{
string_concat_view const& operator()(string_concat_view const& view) const
{
return view;
}
string_concat_view operator()(std::string const& str) const
{
return string_view(str);
}
string_concat_view operator()(std::reference_wrapper<std::string const> str) const
{
return string_view(std::cref(str).get());
}
template <char ...c>
string_concat_view operator()(hana::string<c...>) const
{
return string_view(hana::string<c...>::c_str(), sizeof...(c));
}
template <typename String>
auto operator()(String const& str) const
-> std::enable_if_t<
!decltype(hana::is_a<string_concat_view, String>)::value
&& !decltype(hana::is_a<std::string, decltype(std::cref(str).get())>)::value
&& !decltype(hana::is_a<hana::string_tag, String>)::value
, string_concat_view
>
{
return string_view(hana::to<char const*>(std::cref(str).get()));
}
};
constexpr to_string_concat_view_fn to_string_concat_view{};
constexpr auto make_string_concat_view = [](auto const& ...strings)
-> string_concat_view
{
if constexpr(sizeof...(strings) == 1)
{
return to_string_concat_view(strings...);
}
else
{
return std::array<string_concat_view, sizeof...(strings)>{{
to_string_concat_view(strings)...
}};
}
};
}
#endif