-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_literal.hpp
63 lines (50 loc) · 1.31 KB
/
string_literal.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
#pragma once
#include <array>
#include <cassert>
#include <cstddef>
#include <string_view>
namespace util {
struct string_literal {
public:
template <std::size_t N>
explicit(false) consteval string_literal(const char (&str)[N]) noexcept
: size_(N - 1)
, cstr_(str) {
assert(*std::next(cstr_, N - 1) == '\0');
}
constexpr StringLiteral() noexcept
: size_(0)
, cstr_("") {}
[[nodiscard]]
constexpr std::size_t size() const {
return size_;
}
[[nodiscard]]
constexpr const char* c_str() const {
return cstr_;
}
explicit(false) constexpr operator const char*() const { return c_str(); }
[[nodiscard]]
constexpr std::string_view as_view() const {
return {cstr_, size_};
}
explicit(false) constexpr operator std::string_view() const { return as_view(); }
private:
std::size_t size_;
const char* cstr_;
};
namespace experimental {
template <std::size_t N>
struct string {
private:
std::array<char, N> data;
std::size_t current_size = 0;
public:
template <std::size_t Size>
consteval string(const char (&str)[Size])
: current_size{Size - 1} {
std::copy(std::begin(str), std::end(str), std::begin(data));
}
};
} // namespace experimental
} // namespace util