-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_fstring.cpp
More file actions
177 lines (156 loc) · 4.92 KB
/
Copy pathhello_fstring.cpp
File metadata and controls
177 lines (156 loc) · 4.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdlib.h>
#include <iostream>
#include <cassert>
#include "fstring.h"
#include "fstringstream.h"
template<typename TStr>
struct fixed_or_std_str_ref
{
TStr m_string_ref;
void reserve( size_t new_cap )
{
if constexpr (std::is_same_v<std::string, std::decay_t<TStr>>)
{
m_string_ref.reserve(new_cap);
}
else if constexpr (std::is_same_v<fstr::fstr_ref, TStr>)
{
assert(new_cap <= m_string_ref.capacity());
}
}
size_t size() const
{
return m_string_ref.size();
}
size_t capacity() const
{
return m_string_ref.capacity();
}
size_t max_size() const
{
return m_string_ref.max_size();
}
const char* c_str() const
{
return m_string_ref.c_str();
}
fixed_or_std_str_ref& operator=(std::string_view in_sv)
{
m_string_ref = in_sv;
return *this;
}
fixed_or_std_str_ref& operator+=(std::string_view in_sv)
{
m_string_ref += in_sv;
return *this;
}
char& operator[]( size_t pos )
{
return m_string_ref[pos];
}
const char& operator[]( size_t pos ) const
{
return m_string_ref[pos];
}
void resize( size_t count )
{
m_string_ref.resize(count);
}
char* data()
{
return m_string_ref.data();
}
constexpr const char*const data() const
{
return m_string_ref.data();
}
constexpr operator std::string_view() const noexcept
{
return std::string_view(c_str(), size());
}
constexpr void clear() noexcept {m_string_ref.clear();}
constexpr fixed_or_std_str_ref& insert(size_t index, size_t count, char ch)
{
m_string_ref.insert(index, count, ch);
return *this;
}
constexpr fixed_or_std_str_ref& insert(size_t index, const std::string_view sv, size_t count)
{
m_string_ref.insert(index, sv, count);
return *this;
}
constexpr fixed_or_std_str_ref& insert(size_t index, const std::string_view sv)
{
m_string_ref.insert(index, sv);
return *this;
}
constexpr fixed_or_std_str_ref& replace(size_t pos, size_t count,
const std::string_view replacement_str)
{
m_string_ref.replace(pos, count, replacement_str);
return *this;
}
constexpr fixed_or_std_str_ref& replace(size_t pos, const std::string_view replacement_str)
{
m_string_ref.replace(pos, replacement_str);
return *this;
}
};
template<typename TStr>
static void ukalili(const char* name, fixed_or_std_str_ref<TStr> the_ref)
{
the_ref = "UKALILI";
the_ref += "!";
std::cout << name << ":\n";
std::cout << " '" << std::string_view(the_ref) << "'\n";
std::cout << " size: " << the_ref.size() << " chars\n";
std::cout << " capacity: " << the_ref.capacity() << " chars\n";
std::cout << " max_size: " << the_ref.max_size() << " chars\n";
std::cout << " 1st char: " << the_ref[0] << "\n";
std::cout << " last char: " << the_ref[the_ref.size()-1] << "\n";
the_ref.resize(3);
std::cout << " after resize(3): " << the_ref.data() << "\n";
the_ref.reserve(30);
std::cout << " after reserve(30), capacity: " << the_ref.capacity() << "\n";
}
static constexpr fstr::fstr127 will_it_constexpr()
{
constexpr fstr::fstr31 f0;
constexpr fstr::fstr31 f1('X');
constexpr fstr::fstr31 f2("Y");
constexpr fstr::fstr31 f3("Z"sv);
constexpr fstr::fstr31 b0(false);
constexpr fstr::fstr31 b1(true);
fstr::fstr31 f4(1); // number conversions are not constexpr yet
fstr::fstr31 f5(2.3);
fstr::fstr127 retVal(b0, f0, f1, b1, f2, f3, f4, f5);
return retVal;
}
int main(int argc, char** argv)
{
std::cout << "sizeof(fstr::fstr_base<0, char>) = " << sizeof(fstr::fstring_base<0, char>) << std::endl;
std::cout << "sizeof(fstr::fstr15) = " << sizeof(fstr::fstr15) << std::endl;
std::cout << "sizeof(fstr::fstr31) = " << sizeof(fstr::fstr31) << std::endl;
std::cout << "sizeof(fstr::fstr63) = " << sizeof(fstr::fstr63) << std::endl;
std::cout << "sizeof(fstr::fstr_base_ref) = " << sizeof(fstr::fstr_ref) << std::endl;
constexpr fstr::fstr31 fstr("Hello fstring");
std::cout << std::string_view(fstr) << std::endl;
{
fstr::fstr31 original("ukalili");
fixed_or_std_str_ref<fstr::fstr_ref> the_ref{original};
ukalili("fixed_ref", the_ref);
}
{
std::string original("ukalili");
fixed_or_std_str_ref<std::string&> the_ref{original};
ukalili("std_ref", the_ref);
}
{
std::string original("ukalili");
fixed_or_std_str_ref<std::string> the_ref{original};
ukalili("std_conc", the_ref);
}
auto sum_of_all_constexpr = will_it_constexpr();
std::cout << sum_of_all_constexpr.sv() << std::endl;
return 0;
}