Nebula
Loading...
Searching...
No Matches
stringatom.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
16#include "util/string.h"
17
18//------------------------------------------------------------------------------
19namespace Util
20{
22{
23public:
25 StringAtom();
27 StringAtom(const StringAtom& rhs);
29 StringAtom(char* ptr);
31 StringAtom(const char* ptr);
33 StringAtom(const char* ptr, size_t len);
35 StringAtom(unsigned char* ptr);
37 StringAtom(const unsigned char* ptr);
39 StringAtom(const String& str);
41 StringAtom(std::nullptr_t);
42
44 void operator=(const StringAtom& rhs);
46 void operator=(const char* ptr);
48 void operator=(const String& str);
49
51 bool operator==(const StringAtom& rhs) const;
53 bool operator==(std::nullptr_t) const;
55 bool operator!=(const StringAtom& rhs) const;
57 bool operator>(const StringAtom& rhs) const;
59 bool operator<(const StringAtom& rhs) const;
61 bool operator>=(const StringAtom& rhs) const;
63 bool operator<=(const StringAtom& rhs) const;
64
66 bool operator==(const char* rhs) const;
68 bool operator!=(const char* rhs) const;
70 bool operator==(const String& rhs) const;
72 bool operator!=(const String& rhs) const;
73
75 void Clear();
77 bool IsValid() const;
79 const char* Value() const;
81 String AsString() const;
82
84 uint32_t HashCode() const;
86 uint32_t StringHashCode() const;
87
89 const char* c_str() const;
90 size_t length() const;
91 bool empty() const;
92
93private:
95 void Setup(const char* str);
96
97 const char* content;
98};
99
100//------------------------------------------------------------------------------
103__forceinline
105 content(nullptr)
106{
107 // empty
108}
109
110//------------------------------------------------------------------------------
113__forceinline
115 content(rhs.content)
116{
117 // empty
118}
119
120//------------------------------------------------------------------------------
123inline
125{
126 if (nullptr != str)
127 {
128 this->Setup(str);
129 }
130 else
131 {
132 this->content = nullptr;
133 }
134}
135
136//------------------------------------------------------------------------------
139inline
141{
142 if (nullptr != str)
143 {
144 this->Setup(str);
145 }
146 else
147 {
148 this->content = nullptr;
149 }
150}
151
152//------------------------------------------------------------------------------
155inline StringAtom::StringAtom(const char* str, size_t len)
156{
157 if (nullptr != str)
158 {
159 Util::String string(str, len);
160 this->Setup(string.AsCharPtr());
161 }
162 else
163 {
164 this->content = nullptr;
165 }
166}
167
168//------------------------------------------------------------------------------
171inline
172StringAtom::StringAtom(unsigned char* str)
173{
174 if (nullptr != str)
175 {
176 this->Setup((const char*)str);
177 }
178 else
179 {
180 this->content = nullptr;
181 }
182}
183
184//------------------------------------------------------------------------------
187inline
188StringAtom::StringAtom(const unsigned char* str)
189{
190 if (nullptr != str)
191 {
192 this->Setup((const char*)str);
193 }
194 else
195 {
196 this->content = nullptr;
197 }
198}
199
200//------------------------------------------------------------------------------
203inline
205{
206 this->Setup(str.AsCharPtr());
207}
208
209//------------------------------------------------------------------------------
212inline
214{
215 this->content = nullptr;
216}
217
218//------------------------------------------------------------------------------
221__forceinline void
223{
224 this->content = rhs.content;
225}
226
227//------------------------------------------------------------------------------
230inline void
231StringAtom::operator=(const char* str)
232{
233 if (0 != str)
234 {
235 this->Setup(str);
236 }
237 else
238 {
239 this->content = 0;
240 }
241}
242
243//------------------------------------------------------------------------------
246inline void
248{
249 this->Setup(str.AsCharPtr());
250}
251
252//------------------------------------------------------------------------------
255__forceinline bool
257{
258 return this->content == rhs.content;
259}
260
261//------------------------------------------------------------------------------
264__forceinline bool
266{
267 return this->content != rhs.content;
268}
269
270//------------------------------------------------------------------------------
273__forceinline bool
275{
276 return this->content > rhs.content;
277}
278
279//------------------------------------------------------------------------------
282__forceinline bool
283StringAtom::operator<(const StringAtom& rhs) const
284{
285 return this->content < rhs.content;
286}
287
288//------------------------------------------------------------------------------
291__forceinline bool
293{
294 return this->content >= rhs.content;
295}
296
297//------------------------------------------------------------------------------
300__forceinline bool
301StringAtom::operator<=(const StringAtom& rhs) const
302{
303 return this->content <= rhs.content;
304}
305
306//------------------------------------------------------------------------------
310inline bool
312{
313 if (0 == this->content)
314 {
315 return false;
316 }
317 else
318 {
319 return (rhs == this->content);
320 }
321}
322
323//------------------------------------------------------------------------------
327inline bool
329{
330 if (0 == this->content)
331 {
332 return false;
333 }
334 else
335 {
336 return (rhs != this->content);
337 }
338}
339
340//------------------------------------------------------------------------------
343__forceinline void
345{
346 this->content = 0;
347}
348
349//------------------------------------------------------------------------------
352__forceinline bool
354{
355 return (0 != this->content) && (0 != this->content[0]);
356}
357
358//------------------------------------------------------------------------------
361__forceinline const char*
363{
364 return this->content;
365}
366
367//------------------------------------------------------------------------------
371inline String
373{
374 return String(this->content);
375}
376
377//------------------------------------------------------------------------------
380inline uint32_t
382{
383 return Math::pointerhash((void *)this->content);
384}
385
386//------------------------------------------------------------------------------
389__forceinline const char*
391{
392 return this->Value();
393}
394
395//------------------------------------------------------------------------------
398__forceinline size_t
400{
401 return strlen(this->content);
402}
403
404
405//------------------------------------------------------------------------------
408__forceinline bool
410{
411 return (0 != this->content) && ('\0' != this->content[0]);
412}
413
414} // namespace Util
415
416//------------------------------------------------------------------------------
419Util::StringAtom operator ""_atm(const char* c, std::size_t s);
420
421//------------------------------------------------------------------------------
A StringAtom.
Definition stringatom.h:22
bool operator<=(const StringAtom &rhs) const
less-or-equal operator
Definition stringatom.h:301
const char * Value() const
get contained string as char ptr (fast)
Definition stringatom.h:362
bool operator!=(const StringAtom &rhs) const
inequality operator
Definition stringatom.h:265
StringAtom()
default constructor
Definition stringatom.h:104
size_t length() const
Definition stringatom.h:399
void Clear()
clear content (becomes invalid)
Definition stringatom.h:344
bool operator>=(const StringAtom &rhs) const
greater-or-equal operator
Definition stringatom.h:292
const char * content
Definition stringatom.h:97
bool operator==(const StringAtom &rhs) const
equality operator
Definition stringatom.h:256
const char * c_str() const
helpers to interface with libraries that expect std::string like apis
Definition stringatom.h:390
uint32_t HashCode() const
calculate hash code for Util::HashTable (basically just the adress)
Definition stringatom.h:381
String AsString() const
get containted string as string object (SLOW!!!)
Definition stringatom.h:372
void Setup(const char *str)
setup the string atom from a string pointer
Definition stringatom.cc:30
uint32_t StringHashCode() const
calculate persistent hash code (based on string content)
Definition stringatom.cc:111
bool empty() const
Definition stringatom.h:409
void operator=(const StringAtom &rhs)
assignment
Definition stringatom.h:222
bool IsValid() const
return true if valid (contains a non-empty string)
Definition stringatom.h:353
bool operator>(const StringAtom &rhs) const
greater-then operator
Definition stringatom.h:274
bool operator<(const StringAtom &rhs) const
less-then operator
Definition stringatom.h:283
__forceinline uint32_t pointerhash(void *ptr)
cheap pointer hash using modulo with a mersenne prime
Definition scalar.h:808
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
Nebula's universal string class.
Definition string.h:50
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:542