File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 1515#ifndef STRTAG_H
1616#define STRTAG_H
1717
18+ #include < string_view>
19+ #include < cstring>
1820#include < stdexcept>
21+ #include < cstdint>
1922#include < string>
23+ #include < type_traits>
24+
25+ template <class T = uint64_t , std::size_t N>
26+ constexpr T qStr2Tag (const char (&str)[N])
27+ {
28+ static_assert (std::is_trivially_copyable_v<T>);
29+ static_assert (N - 1 == sizeof (T), " Invalid tag length" );
30+ T value{};
31+ for (std::size_t i = 0 ; i < sizeof (T); ++i) {
32+ value |= T (static_cast <unsigned char >(str[i])) << (i * 8 );
33+ }
34+ return value;
35+ }
2036
2137template <class T = uint64_t >
22- constexpr T qStr2Tag (const char * str)
38+ constexpr T qStr2Tag (std::string_view str)
2339{
24- if (strlen ( str) != sizeof (T)) {
40+ if (str. size ( ) != sizeof (T)) {
2541 throw std::runtime_error (" Invalid tag length" );
2642 }
2743 T tmp;
You can’t perform that action at this time.
0 commit comments