73 const char *str =
nullptr;
85 cstring(
const char *
string, std::size_t length) {
86 if (
string !=
nullptr) {
87 construct_from_shared(
string, length);
95 if (
string !=
nullptr) {
96 construct_from_shared(
string, std::strlen(
string));
102 cstring(
const std::string &
string) {
103 construct_from_shared(
string.data(),
string.length());
108 explicit cstring(std::string_view
string) {
109 construct_from_shared(
string.data(),
string.length());
117 cstring(
const std::stringstream &stream)
124 static cstring own(
const char *
string, std::size_t length) {
125 if (
string ==
nullptr) {
130 result.construct_from_unique(
string, length);
135 template <
typename T, std::size_t N,
136 typename =
typename std::enable_if<std::is_same<T, const char>::value>::type>
137 static cstring literal(T (&
string)[N]) {
139 result.construct_from_literal(
string, N - 1 );
145 void construct_from_shared(
const char *
string, std::size_t length);
148 void construct_from_unique(
const char *
string, std::size_t length);
151 void construct_from_literal(
const char *
string, std::size_t length);
159 template <
typename Iter>
160 cstring(Iter begin, Iter end) {
161 *
this = std::string(begin, end);
164 char get(
unsigned index)
const {
return (index < size()) ? str[index] : 0; }
165 const char *c_str()
const {
return str; }
166 operator const char *()
const {
return str; }
169 size_t size()
const {
173 return str ? strlen(str) : 0;
175 bool isNull()
const {
return str ==
nullptr; }
176 bool isNullOrEmpty()
const {
return str ==
nullptr ? true : str[0] == 0; }
179 const char *begin()
const {
return str; }
180 const char *end()
const {
return str ? str + strlen(str) : str; }
183 const char *find(
int c)
const {
return str ? strchr(str, c) :
nullptr; }
184 const char *findlast(
int c)
const {
return str ? strrchr(str, c) : str; }
187 const char *find(
const char *s)
const {
return str ? strstr(str, s) :
nullptr; }
190 bool operator==(
cstring a)
const {
return str == a.str; }
191 bool operator!=(
cstring a)
const {
return str != a.str; }
194 bool operator==(
const char *a)
const {
return str ? a && !strcmp(str, a) : !a; }
195 bool operator!=(
const char *a)
const {
return str ? !a || !!strcmp(str, a) : !!a; }
196 bool operator<(
cstring a)
const {
return *
this < a.str; }
197 bool operator<(
const char *a)
const {
return str ? a && strcmp(str, a) < 0 : !!a; }
198 bool operator<=(
cstring a)
const {
return *
this <= a.str; }
199 bool operator<=(
const char *a)
const {
return str ? a && strcmp(str, a) <= 0 :
true; }
200 bool operator>(
cstring a)
const {
return *
this > a.str; }
201 bool operator>(
const char *a)
const {
return str ? !a || strcmp(str, a) > 0 :
false; }
202 bool operator>=(
cstring a)
const {
return *
this >= a.str; }
203 bool operator>=(
const char *a)
const {
return str ? !a || strcmp(str, a) >= 0 : !a; }
205 bool operator==(
const std::string &a)
const {
return *
this == a.c_str(); }
206 bool operator!=(
const std::string &a)
const {
return *
this != a.c_str(); }
207 bool operator<(
const std::string &a)
const {
return *
this < a.c_str(); }
208 bool operator<=(
const std::string &a)
const {
return *
this <= a.c_str(); }
209 bool operator>(
const std::string &a)
const {
return *
this > a.c_str(); }
210 bool operator>=(
const std::string &a)
const {
return *
this >= a.c_str(); }
212 bool startsWith(
const cstring &prefix)
const;
213 bool endsWith(
const cstring &suffix)
const;
224 cstring operator+=(
const char *a);
225 cstring operator+=(std::string a);
228 cstring before(
const char *at)
const;
229 cstring substr(
size_t start)
const {
230 return (start >= size()) ?
"" : substr(start, size() - start);
232 cstring substr(
size_t start,
size_t length)
const;
233 cstring replace(
char find,
char replace)
const;
235 cstring exceptLast(
size_t count) {
return substr(0, size() - count); }
238 cstring trim(
const char *ws =
" \t\r\n")
const {
239 if (!str)
return *
this;
240 const char *start = str + strspn(str, ws);
241 size_t len = strlen(start);
242 while (len > 0 && strchr(ws, start[len - 1])) --len;
251 template <
typename T>
252 static cstring to_cstring(
const T &t) {
253 std::stringstream ss;
257 template <
typename Iterator>
258 static cstring join(Iterator begin, Iterator end,
const char *delim =
", ") {
259 std::stringstream ss;
260 for (
auto current = begin; current != end; ++current) {
261 if (begin != current) ss << delim;
267 static cstring make_unique(
const T &inuse,
cstring base,
char sep =
'.');
269 static cstring make_unique(
const T &inuse,
cstring base,
int &counter,
char sep =
'.');
363 return make_unique(inuse, base, counter, sep);
cstring indent(size_t amount) const
Append this many spaces after each newline (and before the first string).
Definition cstring.cpp:232