| 1 |
#ifndef STATIC_STRING_VIEW_HH |
| 2 |
#define STATIC_STRING_VIEW_HH |
| 3 |
|
| 4 |
#include "StringStorage.hh" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <cassert> |
| 8 |
#include <string_view> |
| 9 |
#include <utility> |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class static_string_view |
| 29 |
{ |
| 30 |
public: |
| 31 |
|
| 32 |
template<size_t N> |
| 33 |
constexpr static_string_view(char (&buf)[N]) = delete; |
| 34 |
|
| 35 |
|
| 36 |
template<size_t N> |
| 37 |
constexpr static_string_view(const char (&buf)[N]) |
| 38 |
: s(buf, N-1) |
| 39 |
{ |
| 40 |
assert(buf[N - 1] == '\0'); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
struct lifetime_ok_tag {}; |
| 45 |
constexpr static_string_view(lifetime_ok_tag, std::string_view v) |
| 46 |
: s(v) {} |
| 47 |
|
| 48 |
|
| 49 |
explicit(false) constexpr operator std::string_view() const { return s; } |
| 50 |
|
| 51 |
private: |
| 52 |
std::string_view s; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
inline auto make_string_storage(std::string_view sv) |
| 65 |
{ |
| 66 |
auto storage = allocate_string_storage(sv.size()); |
| 67 |
char* p = storage.get(); |
| 68 |
std::ranges::copy(sv, p); |
| 69 |
return std::pair{std::move(storage), |
| 70 |
static_string_view(static_string_view::lifetime_ok_tag{}, |
| 71 |
std::string_view(p, sv.size()))}; |
| 72 |
} |
| 73 |
|
| 74 |
#endif |