Skip to content

Commit a62e5c6

Browse files
authored
Merge pull request #1755 from evoskuil/master
Add text_t::std::string_view() and deduction guide.
2 parents abe7bd9 + ea4cc87 commit a62e5c6

File tree

6 files changed

+77
-43
lines changed

6 files changed

+77
-43
lines changed

include/bitcoin/system/impl/stream/streamers/sha256t_writer.ipp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ namespace system {
3030
// ----------------------------------------------------------------------------
3131

3232
// protected
33-
template <text_t Tag, typename OStream>
33+
template <data_t Tag, typename OStream>
3434
sha256t_writer<Tag, OStream>::sha256t_writer() NOEXCEPT
3535
: base(),
3636
context_(midstate(), one)
3737
{
3838
}
3939

40-
template <text_t Tag, typename OStream>
40+
template <data_t Tag, typename OStream>
4141
sha256t_writer<Tag, OStream>::sha256t_writer(OStream& sink) NOEXCEPT
4242
: base(sink), context_(midstate(), one)
4343
{
4444
}
4545

46-
template <text_t Tag, typename OStream>
46+
template <data_t Tag, typename OStream>
4747
sha256t_writer<Tag, OStream>::~sha256t_writer() NOEXCEPT
4848
{
4949
// Derived virtual destructor called before base destructor.
@@ -53,7 +53,7 @@ sha256t_writer<Tag, OStream>::~sha256t_writer() NOEXCEPT
5353
// protected
5454
// ----------------------------------------------------------------------------
5555

56-
template <text_t Tag, typename OStream>
56+
template <data_t Tag, typename OStream>
5757
void sha256t_writer<Tag, OStream>::do_write_bytes(const uint8_t* data,
5858
size_t size) NOEXCEPT
5959
{
@@ -62,7 +62,7 @@ void sha256t_writer<Tag, OStream>::do_write_bytes(const uint8_t* data,
6262
context_.write(size, data);
6363
}
6464

65-
template <text_t Tag, typename OStream>
65+
template <data_t Tag, typename OStream>
6666
void sha256t_writer<Tag, OStream>::do_flush() NOEXCEPT
6767
{
6868
flusher();
@@ -73,7 +73,7 @@ void sha256t_writer<Tag, OStream>::do_flush() NOEXCEPT
7373
// ----------------------------------------------------------------------------
7474

7575
// static
76-
template <text_t Tag, typename OStream>
76+
template <data_t Tag, typename OStream>
7777
constexpr sha256::state_t sha256t_writer<Tag, OStream>::midstate() NOEXCEPT
7878
{
7979
// Cache midstate of tagged hash part that does not change for a given tag.
@@ -85,7 +85,7 @@ constexpr sha256::state_t sha256t_writer<Tag, OStream>::midstate() NOEXCEPT
8585

8686
// Only hash overflow returns update false, which requires (2^64-8)/8 bytes.
8787
// The stream could invalidate, but writers shouldn't have to check this.
88-
template <text_t Tag, typename OStream>
88+
template <data_t Tag, typename OStream>
8989
void sha256t_writer<Tag, OStream>::flusher() NOEXCEPT
9090
{
9191
hash_digest hash{};

include/bitcoin/system/literals.hpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,22 @@ CONSTEVAL Domain negative(integer_type value) noexcept(false)
112112
return static_cast<Domain>(~narrowed + narrow{1});
113113
}
114114

115-
template <size_t Size>
116-
struct string_holder
117-
{
118-
char str[Size];
119-
120-
CONSTEVAL string_holder(const char(&string)[Size]) noexcept
121-
{
122-
for (size_t i = 0; i < Size; ++i)
123-
str[i] = string[i];
124-
}
125-
};
126-
127115
BC_POP_WARNING()
128116
BC_POP_WARNING()
129117

130118
/// Text representations.
131119
/// ---------------------------------------------------------------------------
132120

121+
template <data_t Data>
122+
CONSTEVAL auto operator "" _a() noexcept
123+
{
124+
return Data.data;
125+
}
126+
133127
template <text_t Text>
134-
CONSTEVAL auto operator "" _array() noexcept
128+
CONSTEVAL auto operator "" _t() noexcept
135129
{
136-
return Text.data;
130+
return Text.text;
137131
}
138132

139133
/// Integer representations.

include/bitcoin/system/stream/streamers.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,23 +231,23 @@ namespace hash
231231
namespace sha256t
232232
{
233233
/// A hash writer that writes a tagged sha256 hash to a std::ostream.
234-
template <text_t Tag>
234+
template <data_t Tag>
235235
using ostream = sha256t_writer<Tag, std::ostream>;
236236

237237
/// A fast hash writer that writes a tagged sha256 hash to a system::ostream.
238-
template <text_t Tag>
238+
template <data_t Tag>
239239
using fast = sha256t_writer<Tag, system::ostream<>>;
240240

241241
/////// A hash writer that copies a tagged sha256 hash to a data_slab.
242-
////template <text_t Tag>
242+
////template <data_t Tag>
243243
////using copy = make_streamer<copy_sink<data_slab>, tag<Tag, sha256t_writer>::type>;
244244

245245
/////// A hash writer that inserts a tagged sha256 hash into a container.
246-
////template <text_t Tag, typename Container>
246+
////template <data_t Tag, typename Container>
247247
////using push = make_streamer<push_sink<Container>, tag<Tag, sha256t_writer>::type>;
248-
////template <text_t Tag>
248+
////template <data_t Tag>
249249
////using text = push<Tag, std::string>;
250-
////template <text_t Tag>
250+
////template <data_t Tag>
251251
////using data = push<Tag, data_chunk>;
252252
}
253253
}

include/bitcoin/system/stream/streamers/sha256t_writer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace libbitcoin {
2828
namespace system {
2929

3030
/// A tagged hash writer that accepts an ostream.
31-
template <text_t Tag, typename OStream = std::ostream>
31+
template <data_t Tag, typename OStream = std::ostream>
3232
class sha256t_writer
3333
: public byte_writer<OStream>
3434
{

include/bitcoin/system/types.hpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,56 @@ using unsigned_exact_type =
182182
BC_PUSH_WARNING(NO_DYNAMIC_ARRAY_INDEXING)
183183
BC_PUSH_WARNING(NO_ARRAY_INDEXING)
184184

185+
template <size_t Size>
186+
class data_t
187+
{
188+
public:
189+
CONSTEVAL data_t(const char (&string)[Size]) noexcept
190+
: data(to_data(string))
191+
{
192+
}
193+
194+
const std::array<uint8_t, Size - 1> data;
195+
196+
private:
197+
static CONSTEVAL auto to_data(const char(&string)[Size]) noexcept
198+
{
199+
std::array<uint8_t, Size - 1> out{};
200+
for (size_t index{}; index < Size - 1; ++index)
201+
out[index] = string[index];
202+
203+
return out;
204+
}
205+
};
206+
207+
template <size_t Size>
208+
data_t(const char(&)[Size]) noexcept -> data_t<Size>;
209+
185210
template <size_t Size>
186211
class text_t
187212
{
188213
public:
189-
std::array<uint8_t, Size - 1> data;
190214
CONSTEVAL text_t(const char (&string)[Size]) noexcept
191-
: data(to_array(string)) {}
215+
: text(to_text(string))
216+
{
217+
}
218+
219+
const std::array<char, Size - 1> text;
192220

193221
private:
194-
static CONSTEVAL auto to_array(const char(&string)[Size]) noexcept
222+
static CONSTEVAL auto to_text(const char(&string)[Size]) noexcept
195223
{
196-
std::array<uint8_t, Size - 1> data{};
197-
for (size_t index = 0; index < Size - 1; ++index)
198-
data.at(index) = string[index];
224+
std::array<char, Size - 1> out{};
225+
for (size_t index{}; index < Size - 1; ++index)
226+
out[index] = string[index];
199227

200-
return data;
228+
return out;
201229
}
202230
};
203231

232+
template <size_t Size>
233+
text_t(const char(&)[Size]) noexcept -> text_t<Size>;
234+
204235
template <size_t Size, typename Byte,
205236
std::enable_if_t<std::is_same_v<Byte, char>, bool> = true>
206237
constexpr size_t literal_length(const Byte(&)[Size]) noexcept

test/literals.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@
2020

2121
// text literals
2222

23-
static_assert(is_same_type<decltype(""_array), std::array<uint8_t, 0>>);
24-
static_assert(""_array.empty());
25-
26-
static_assert(is_same_type<decltype("test"_array), std::array<uint8_t, 4>>);
27-
static_assert("test"_array.at(0) == 't');
28-
static_assert("test"_array.at(1) == 'e');
29-
static_assert("test"_array.at(2) == 's');
30-
static_assert("test"_array.at(3) == 't');
23+
static_assert(is_same_type<decltype(""_a), std::array<uint8_t, 0>>);
24+
static_assert(""_a.empty());
25+
26+
static_assert(is_same_type<decltype("test"_a), std::array<uint8_t, 4>>);
27+
static_assert("test"_a.at(0) == 't');
28+
static_assert("test"_a.at(1) == 'e');
29+
static_assert("test"_a.at(2) == 's');
30+
static_assert("test"_a.at(3) == 't');
31+
32+
static_assert(is_same_type<decltype(""_t), std::array<char, 0>>);
33+
static_assert(""_t.empty());
34+
35+
static_assert(is_same_type<decltype("test"_t), std::array<char, 4>>);
36+
static_assert("test"_t.at(0) == 't');
37+
static_assert("test"_t.at(1) == 'e');
38+
static_assert("test"_t.at(2) == 's');
39+
static_assert("test"_t.at(3) == 't');
3140

3241
// en.cppreference.com/w/cpp/language/integer_literal
3342

0 commit comments

Comments
 (0)