-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.h
More file actions
106 lines (86 loc) · 2.89 KB
/
string.h
File metadata and controls
106 lines (86 loc) · 2.89 KB
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
#pragma once
/*
Compile time extension for strings, allow to combine and check text in compile time
Example:
constexpr ext::constexpr_string textFirst = "test";
constexpr ext::constexpr_string textSecond = "second";
constexpr auto TextCombination = textFirst + "_" + textSecond;
static_assert(TextCombination == "test_second");
In C++20 can be used to store text as a template argument:
template <ext::constexpr_string name__>
struct Object {
constexpr std::string_view Name() const {
return name__.str();
}
...
};
Object<"object_name"> object;
static_assert(object.Name() == std::string_view("object_name"));
*/
#include <string_view>
#include <ext/std/array.h>
namespace ext {
template <size_t N>
struct constexpr_string {
constexpr constexpr_string(const char (&_str)[N])
: value_(std::to_array(_str))
{}
constexpr operator std::string_view() const {
return std::string_view(value_.data(), N - 1);
}
constexpr std::string_view str() const {
return *this;
}
constexpr size_t size() const {
return N - 1;
}
constexpr char operator[](std::size_t i) const {
return value_[i];
}
template<std::size_t N2>
constexpr constexpr_string<N+N2-1> operator+(const constexpr_string<N2>& str) const
{
char resultText[N+N2-1] {};
for (size_t i = 0; i < N - 1; ++i) {
resultText[i] = value_[i];
}
for (size_t i = 0; i < N2; ++i) {
resultText[N + i - 1] = str.value_[i];
}
return resultText;
}
template<std::size_t Size>
constexpr bool operator==(const constexpr_string<Size> other) const {
return str() == other;
}
template<std::size_t Size>
constexpr bool operator!=(const constexpr_string<Size> other) const {
return !operator==(other);
}
const std::array<char, N> value_;
};
template<std::size_t N1, std::size_t N2>
constexpr auto operator+(constexpr_string<N1> cStr, const char (&str)[N2]) {
return cStr + constexpr_string<N2>(str);
}
template<std::size_t N1, std::size_t N2>
constexpr auto operator+(const char (&str)[N1], constexpr_string<N2> cStr) {
return constexpr_string<N1>(str) + cStr;
}
template<std::size_t N1, std::size_t N2>
constexpr auto operator==(constexpr_string<N1> cStr, const char (&str)[N2]) {
return cStr == constexpr_string<N2>(str);
}
template<std::size_t N1, std::size_t N2>
constexpr auto operator==(const char (&str)[N1], constexpr_string<N2> cStr) {
return constexpr_string<N1>(str) == cStr;
}
template<std::size_t N1, std::size_t N2>
constexpr auto operator!=(constexpr_string<N1> cStr, const char (&str)[N2]) {
return !operator==(cStr, str);
}
template<std::size_t N1, std::size_t N2>
constexpr auto operator!=(const char (&str)[N1], constexpr_string<N2> cStr) {
return !operator==(str, cStr);
}
} // namespace ext