Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/include/lcdf/permstr.hh

raw
1 // -*- related-file-name: "../../liblcdf/permstr.cc" -*-
2 #ifndef LCDF_PERMSTR_HH
3 #define LCDF_PERMSTR_HH
4 #include <assert.h>
5 #include <stddef.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <lcdf/inttypes.h>
9 #include <lcdf/hashcode.hh>
10 class PermString;
11 inline bool operator==(PermString a, PermString b);
12 inline bool operator!=(PermString a, PermString b);
13
14 class PermString { struct Doodad; public:
15
16 typedef Doodad *Capsule;
17 // Declare a PermString::Initializer in any file in which you declare
18 // static global PermStrings.
19 struct Initializer { Initializer(); };
20
21 PermString()
22 : _rep(zero_char_doodad.data) {
23 }
24 explicit PermString(char c)
25 : _rep(one_char_doodad[(unsigned char) c].data) {
26 }
27 inline PermString(const char *s);
28 inline PermString(const char *s, int len);
29 inline PermString(const char *begin, const char *end);
30
31 typedef int (PermString::*unspecified_bool_type)() const;
32 inline operator unspecified_bool_type() const;
33 inline bool operator!() const;
34
35 inline int length() const;
36 char operator[](int i) const;
37
38 inline const char* data() const {
39 return _rep;
40 }
41 inline const char *c_str() const {
42 return _rep;
43 }
44 inline char operator*() const {
45 return *_rep;
46 }
47
48 inline const char *begin() const;
49 inline const char *end() const;
50
51 inline bool equals(const char *s, int len) const;
52 friend inline bool operator==(PermString a, PermString b);
53 friend inline bool operator!=(PermString a, PermString b);
54
55 inline Capsule capsule() const;
56 inline static PermString decapsule(Capsule c);
57
58 friend PermString permprintf(const char*, ...);
59 friend PermString vpermprintf(const char*, va_list);
60 friend PermString permcat(PermString, PermString);
61 friend PermString permcat(PermString, PermString, PermString);
62
63 private:
64
65 struct Doodad {
66 Doodad *next;
67 int length;
68 char data[2];
69 };
70
71 const char *_rep;
72
73 PermString(Doodad* d) : _rep(d->data) { }
74 void initialize(const char*, int);
75 Doodad* doodad() const { return (Doodad*)(_rep - offsetof(Doodad, data)); }
76
77 friend struct PermString::Initializer;
78 static void static_initialize();
79
80 enum { NHASH = 1024 }; // must be power of 2
81 static Doodad zero_char_doodad, one_char_doodad[256], *buckets[NHASH];
82
83 };
84
85
86 inline PermString::PermString(const char* s)
87 {
88 initialize(s, -1);
89 }
90
91 inline PermString::PermString(const char* s, int len)
92 {
93 initialize(s, len);
94 }
95
96 inline PermString::PermString(const char* begin, const char* end)
97 {
98 assert(end);
99 initialize(begin, end > begin ? end - begin : 0);
100 }
101
102 inline PermString::operator unspecified_bool_type() const
103 {
104 return _rep != zero_char_doodad.data ? &PermString::length : 0;
105 }
106
107 inline bool PermString::operator!() const
108 {
109 return _rep == zero_char_doodad.data;
110 }
111
112 inline int PermString::length() const
113 {
114 return doodad()->length;
115 }
116
117 inline const char *PermString::begin() const
118 {
119 return _rep;
120 }
121
122 inline const char *PermString::end() const
123 {
124 return _rep + doodad()->length;
125 }
126
127 inline char PermString::operator[](int i) const
128 {
129 assert((unsigned) i < (unsigned) length());
130 return c_str()[i];
131 }
132
133 inline bool operator==(PermString a, PermString b)
134 {
135 return a._rep == b._rep;
136 }
137
138 inline bool operator==(PermString a, const char *b)
139 {
140 return (!a || !b ? !a && !b : strcmp(a.c_str(), b) == 0);
141 }
142
143 inline bool operator==(const char *a, PermString b)
144 {
145 return b == a;
146 }
147
148 inline bool operator!=(PermString a, PermString b)
149 {
150 return a._rep != b._rep;
151 }
152
153 inline bool operator!=(PermString a, const char *b)
154 {
155 return !(a == b);
156 }
157
158 inline bool operator!=(const char *a, PermString b)
159 {
160 return !(b == a);
161 }
162
163 inline bool operator<(PermString a, PermString b)
164 {
165 // NOT lexicographic ordering!
166 return a.begin() < b.begin();
167 }
168
169 inline bool PermString::equals(const char *s, int len) const
170 {
171 if (len > 0)
172 return len == length() && memcmp(s, _rep, len) == 0;
173 else
174 return strcmp(s, _rep) == 0;
175 }
176
177 inline PermString::Capsule PermString::capsule() const
178 {
179 return doodad();
180 }
181
182 inline PermString PermString::decapsule(Capsule c)
183 {
184 return PermString(c);
185 }
186
187 inline hashcode_t hashcode(PermString s)
188 {
189 return (uintptr_t) s.c_str();
190 }
191
192 PermString permprintf(const char *format, ...);
193 PermString vpermprintf(const char *format, va_list val);
194
195 PermString permcat(PermString a, PermString b);
196
197 #endif