1
2
3
4
5
6 #ifndef SOULNG_UTIL_STRING_INCLUDED
7 #define SOULNG_UTIL_STRING_INCLUDED
8 #include <soulng/util/Defines.hpp>
9 #include <soulng/util/Error.hpp>
10
11 namespace soulng { namespace util {
12
13 inline uint64_t StringLen(const char32_t* s)
14 {
15 uint64_t n = 0;
16 Assert(s, "null string ptr");
17 while (*s++) ++n;
18 return n;
19 }
20
21 class StringPtr
22 {
23 public:
24 StringPtr(const char32_t* value_) : value(value_) {}
25 const char32_t* Value() const { return value; }
26 bool IsEmpty() const
27 {
28 Assert(value, "null string ptr");
29 return !*value;
30 }
31 private:
32 const char32_t* value;
33 };
34
35 inline bool operator==(StringPtr left, StringPtr right)
36 {
37 const char32_t* p = left.Value();
38 const char32_t* q = right.Value();
39 Assert(p, "null string ptr");
40 Assert(q, "null string ptr");
41 while (*p && *q && *p == *q)
42 {
43 ++p;
44 ++q;
45 }
46 return !*p && !*q;
47 }
48
49 inline bool operator!=(StringPtr left, StringPtr right)
50 {
51 return !(left == right);
52 }
53
54 inline bool operator<(StringPtr left, StringPtr right)
55 {
56 const char32_t* p = left.Value();
57 const char32_t* q = right.Value();
58 Assert(p, "null string ptr");
59 Assert(q, "null string ptr");
60 while (*p && *q && *p == *q)
61 {
62 ++p;
63 ++q;
64 }
65 return *p < *q;
66 }
67
68 struct StringPtrHash
69 {
70 #if defined(BITS_64)
71
72
73 #elif defined(BITS_32)
74
75
76 #else
77 #error either BITS_64 or BITS_32 must be defined
78 #endif
79 size_t operator()(StringPtr s) const
80 {
81 size_t value = offset;
82 const char32_t* p = s.Value();
83 Assert(p, "null string ptr");
84 while (*p)
85 {
86 value ^= (size_t)*p;
87 value *= prime;
88 ++p;
89 }
90 return value;
91 }
92 };
93
94 } }
95
96 #endif // SOULNG_UTIL_STRING_INCLUDED