Flecs: include/flecs/addons/cpp/utils/string.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1
5
6namespace flecs {
7
8struct string_view;
9
15 struct string {
17 explicit string()
18 : str_(nullptr)
19 , const_str_("")
20 , length_(0) { }
21
23 explicit string(char *str)
24 : str_(str)
25 , const_str_(str ? str : "")
26 , length_(str ? ecs_os_strlen(str) : 0) { }
27
29 ~string() {
30 // If flecs is included in a binary but is not used, it is possible that
31 // the OS API is not initialized. Calling ecs_os_free in that case could
32 // crash the application during exit. However, if a string has been set
33 // flecs has been used, and OS API should have been initialized.
34 if (str_) {
35 ecs_os_free(str_);
36 }
37 }
38
40 string(string&& str) noexcept {
41 ecs_os_free(str_);
42 str_ = str.str_;
43 const_str_ = str.const_str_;
44 length_ = str.length_;
45 str.str_ = nullptr;
46 }
47
49 operator const char*() const {
50 return const_str_;
51 }
52
54 string& operator=(string&& str) noexcept {
55 ecs_os_free(str_);
56 str_ = str.str_;
57 const_str_ = str.const_str_;
58 length_ = str.length_;
59 str.str_ = nullptr;
60 return *this;
61 }
62
64 string& operator=(const string& str) = delete;
66 string(const string& str) = delete;
67
69 bool operator==(const flecs::string& str) const {
70 if (str.const_str_ == const_str_) {
71 return true;
72 }
73
74 if (!const_str_ || !str.const_str_) {
75 return false;
76 }
77
78 if (str.length_ != length_) {
79 return false;
80 }
81
82 return ecs_os_strcmp(str, const_str_) == 0;
83 }
84
86 bool operator!=(const flecs::string& str) const {
87 return !(*this == str);
88 }
89
91 bool operator==(const char *str) const {
92 if (const_str_ == str) {
93 return true;
94 }
95
96 if (!const_str_ || !str) {
97 return false;
98 }
99
100 return ecs_os_strcmp(str, const_str_) == 0;
101 }
102
104 bool operator!=(const char *str) const {
105 return !(*this == str);
106 }
107
109 const char* c_str() const {
110 return const_str_;
111 }
112
114 std::size_t length() const {
115 return static_cast<std::size_t>(length_);
116 }
117
119 template <size_t N>
120 static constexpr size_t length( char const (&)[N] ) {
121 return N - 1;
122 }
123
125 std::size_t size() const {
126 return length();
127 }
128
130 void clear() {
131 ecs_os_free(str_);
132 str_ = nullptr;
133 const_str_ = nullptr;
134 }
135
137 bool contains(const char *substr) {
138 if (const_str_) {
139 return strstr(const_str_, substr) != nullptr;
140 } else {
141 return false;
142 }
143 }
144
145protected:
153 string(const char *str)
154 : str_(nullptr)
155 , const_str_(str ? str : "")
156 , length_(str ? ecs_os_strlen(str) : 0) { }
157
158 char *str_ = nullptr;
159 const char *const_str_;
160 ecs_size_t length_;
161};
162
169 struct string_view : string {
171 explicit string_view(const char *str)
172 : string(str) { }
173};
174
175}
flecs::string_view
Non-owning string view.
Definition string.hpp:169
flecs::string_view::string_view
string_view(const char *str)
Construct from a C string (non-owning).
Definition string.hpp:171
flecs::string
Owned string wrapper.
Definition string.hpp:15
flecs::string::length
static constexpr size_t length(char const (&)[N])
Return the length of a string literal at compile time.
Definition string.hpp:120
flecs::string::operator!=
bool operator!=(const char *str) const
Inequality operator for a C string.
Definition string.hpp:104
flecs::string::string
string(const char *str)
Construct from a non-owned C string.
Definition string.hpp:153
flecs::string::operator=
string & operator=(const string &str)=delete
Ban implicit copies/allocations.
flecs::string::contains
bool contains(const char *substr)
Check if the string contains a substring.
Definition string.hpp:137
flecs::string::string
string()
Default constructor.
Definition string.hpp:17
flecs::string::string
string(string &&str) noexcept
Move constructor.
Definition string.hpp:40
flecs::string::length
std::size_t length() const
Return the string length.
Definition string.hpp:114
flecs::string::string
string(const string &str)=delete
Ban implicit copies/allocations.
flecs::string::operator=
string & operator=(string &&str) noexcept
Move assignment operator.
Definition string.hpp:54
flecs::string::c_str
const char * c_str() const
Return the C string.
Definition string.hpp:109
flecs::string::clear
void clear()
Clear the string, freeing the owned memory.
Definition string.hpp:130
flecs::string::operator==
bool operator==(const flecs::string &str) const
Equality operator.
Definition string.hpp:69
flecs::string::~string
~string()
Destructor.
Definition string.hpp:29
flecs::string::string
string(char *str)
Construct from an owned char pointer.
Definition string.hpp:23
flecs::string::operator!=
bool operator!=(const flecs::string &str) const
Inequality operator.
Definition string.hpp:86
flecs::string::size
std::size_t size() const
Return the string size (same as length).
Definition string.hpp:125
flecs::string::operator==
bool operator==(const char *str) const
Equality operator for a C string.
Definition string.hpp:91