81inline cstring operator""_cs(
const char *str, std::size_t len);
86 const char *str =
nullptr;
98 cstring(
const char *
string, std::size_t length) {
99 if (
string !=
nullptr) {
100 construct_from_shared(
string, length);
107 explicit cstring(
const char *
string) {
108 if (
string !=
nullptr) {
109 construct_from_shared(
string, std::strlen(
string));
115 cstring(
const std::string &
string) {
116 construct_from_shared(
string.data(),
string.length());
121 explicit cstring(std::string_view
string) {
122 construct_from_shared(
string.data(),
string.length());
130 cstring(
const std::stringstream &stream)
137 static cstring own(
const char *
string, std::size_t length) {
138 if (
string ==
nullptr) {
143 result.construct_from_unique(
string, length);
148 template <
typename T, std::size_t N,
149 typename =
typename std::enable_if<std::is_same<T, const char>::value>::type>
150 static cstring literal(T (&
string)[N]) {
152 result.construct_from_literal(
string, N - 1 );
157 static bool is_cached(std::string_view s);
163 void construct_from_shared(
const char *
string, std::size_t length);
166 void construct_from_unique(
const char *
string, std::size_t length);
169 void construct_from_literal(
const char *
string, std::size_t length);
171 friend cstring P4::literals::operator
""_cs(
const char *str, std::size_t len);
179 template <
typename Iter>
180 cstring(Iter begin, Iter end) {
181 *
this = std::string(begin, end);
184 char get(
unsigned index)
const {
return (index < size()) ? str[index] : 0; }
185 const char *c_str()
const {
return str; }
186 operator const char *()
const {
return str; }
188 std::string string()
const {
return str ? std::string(str) : std::string(
""); }
189 explicit operator std::string()
const {
return string(); }
191 std::string_view string_view()
const {
192 return str ? std::string_view(str) : std::string_view(
"");
194 explicit operator std::string_view()
const {
return string_view(); }
197 size_t size()
const {
201 return str ? strlen(str) : 0;
203 bool isNull()
const {
return str ==
nullptr; }
204 bool isNullOrEmpty()
const {
return str ==
nullptr ? true : str[0] == 0; }
207 const char *begin()
const {
return str; }
208 const char *end()
const {
return str ? str + strlen(str) : str; }
211 const char *find(
int c)
const {
return str ? strchr(str, c) :
nullptr; }
212 const char *findlast(
int c)
const {
return str ? strrchr(str, c) : str; }
215 const char *find(
const char *s)
const {
return str ? strstr(str, s) :
nullptr; }
218 bool operator==(
cstring a)
const {
return str == a.str; }
219 bool operator!=(
cstring a)
const {
return str != a.str; }
221 bool operator==(std::nullptr_t)
const {
return str ==
nullptr; }
222 bool operator!=(std::nullptr_t)
const {
return str !=
nullptr; }
225 bool operator==(
const char *a)
const {
return str ? a && !strcmp(str, a) : !a; }
226 bool operator!=(
const char *a)
const {
return str ? !a || !!strcmp(str, a) : !!a; }
227 bool operator<(
cstring a)
const {
return *
this < a.str; }
228 bool operator<(
const char *a)
const {
return str ? a && strcmp(str, a) < 0 : !!a; }
229 bool operator<=(
cstring a)
const {
return *
this <= a.str; }
230 bool operator<=(
const char *a)
const {
return str ? a && strcmp(str, a) <= 0 :
true; }
231 bool operator>(
cstring a)
const {
return *
this > a.str; }
232 bool operator>(
const char *a)
const {
return str ? !a || strcmp(str, a) > 0 :
false; }
233 bool operator>=(
cstring a)
const {
return *
this >= a.str; }
234 bool operator>=(
const char *a)
const {
return str ? !a || strcmp(str, a) >= 0 : !a; }
235 bool operator==(std::string_view a)
const {
return str ? a.compare(str) == 0 : a.empty(); }
236 bool operator!=(std::string_view a)
const {
return str ? a.compare(str) != 0 : !a.empty(); }
238 bool operator==(
const std::string &a)
const {
return *
this == a.c_str(); }
239 bool operator!=(
const std::string &a)
const {
return *
this != a.c_str(); }
240 bool operator<(
const std::string &a)
const {
return *
this < a.c_str(); }
241 bool operator<=(
const std::string &a)
const {
return *
this <= a.c_str(); }
242 bool operator>(
const std::string &a)
const {
return *
this > a.c_str(); }
243 bool operator>=(
const std::string &a)
const {
return *
this >= a.c_str(); }
245 bool startsWith(std::string_view prefix)
const;
246 bool endsWith(std::string_view suffix)
const;
257 cstring operator+=(
const char *a);
258 cstring operator+=(std::string a);
261 cstring before(
const char *at)
const;
262 cstring substr(
size_t start)
const {
263 return (start >= size()) ? cstring::literal(
"") : substr(start, size() - start);
265 cstring substr(
size_t start,
size_t length)
const;
266 cstring replace(
char find,
char replace)
const;
267 cstring replace(std::string_view find, std::string_view replace)
const;
268 cstring exceptLast(
size_t count) {
return substr(0, size() - count); }
271 cstring trim(
const char *ws =
" \t\r\n")
const;
278 template <
typename T>
279 static cstring to_cstring(
const T &t) {
280 std::stringstream ss;
284 template <
typename Iterator>
285 static cstring join(Iterator begin, Iterator end,
const char *delim =
", ") {
286 std::stringstream ss;
287 for (
auto current = begin; current != end; ++current) {
288 if (begin != current) ss << delim;
294 static cstring make_unique(
const T &inuse,
cstring base,
char sep =
'.');
296 static cstring make_unique(
const T &inuse,
cstring base,
int &counter,
char sep =
'.');
312inline bool operator==(
const char *a,
cstring b) {
return b == a; }
313inline bool operator!=(
const char *a, cstring b) {
return b != a; }
314inline bool operator==(
const std::string &a, cstring b) {
return b == a; }
315inline bool operator!=(
const std::string &a, cstring b) {
return b != a; }
317inline std::string operator+(cstring a, cstring b) {
322inline std::string operator+(cstring a,
const char *b) {
327inline std::string operator+(cstring a,
const std::string &b) {
332inline std::string operator+(cstring a,
char b) {
337inline std::string operator+(
const char *a, cstring b) {
342inline std::string operator+(std::string a, cstring b) {
346inline std::string operator+(
char a, cstring b) {
347 std::string rv(1, a);
352inline cstring cstring::operator+=(cstring a) {
356inline cstring cstring::operator+=(
const char *a) {
360inline cstring cstring::operator+=(std::string a) {
364inline cstring cstring::operator+=(
char a) {
369inline std::string &operator+=(std::string &a, cstring b) {
374inline cstring cstring::newline = cstring::literal(
"\n");
375inline cstring cstring::empty = cstring::literal(
"");
378cstring cstring::make_unique(
const T &inuse, cstring base,
int &counter,
char sep) {
379 if (!inuse.count(base))
return base;
384 snprintf(suffix,
sizeof(suffix) /
sizeof(suffix[0]),
"%c%d", sep, counter++);
386 }
while (inuse.count(rv));
391cstring cstring::make_unique(
const T &inuse, cstring base,
char sep) {
393 return make_unique(inuse, base, counter, sep);
396inline std::ostream &operator<<(std::ostream &out, cstring s) {
397 return out << (s ? s.c_str() :
"<null>");
409inline cstring operator""_cs(
const char *str, std::size_t len) {
411 result.construct_from_literal(str, len);
418struct hash<
P4::cstring> {
419 std::size_t operator()(
const P4::cstring &c)
const {
cstring toUpper() const
Convert the cstring to uppercase.
Definition cstring.cpp:311
static size_t cache_size(size_t &count)
Definition cstring.cpp:217
cstring escapeJson() const
Definition cstring.cpp:272
cstring indent(size_t amount) const
Append this many spaces after each newline (and before the first string).
Definition cstring.cpp:265
static cstring get_cached(std::string_view s)
Definition cstring.cpp:194
cstring toLower() const
Convert the cstring to lowercase.
Definition cstring.cpp:317
static bool is_cached(std::string_view s)
Definition cstring.cpp:192
cstring capitalize() const
Capitalize the first symbol.
Definition cstring.cpp:323
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24