… | |
… | |
23 | #define SHSTR_H__ |
23 | #define SHSTR_H__ |
24 | |
24 | |
25 | #include <cstring> |
25 | #include <cstring> |
26 | #include <sstream> |
26 | #include <sstream> |
27 | |
27 | |
28 | #include "util.h" |
28 | #include "traits.h" |
29 | |
29 | |
30 | extern size_t shstr_alloc; |
30 | extern size_t shstr_alloc; |
31 | |
31 | |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
|
|
33 | |
|
|
34 | template<int size> |
|
|
35 | struct shstr_vec |
|
|
36 | { |
|
|
37 | uint32_t len; |
|
|
38 | uint32_t refcnt; |
|
|
39 | char string[size]; |
|
|
40 | }; |
33 | |
41 | |
34 | // this class is a non-refcounted shared string |
42 | // this class is a non-refcounted shared string |
35 | // it cannot be used to create or store shared strings, but |
43 | // it cannot be used to create or store shared strings, but |
36 | // it can be used to apss shared strings around, i.e. as function arguments |
44 | // it can be used to apss shared strings around, i.e. as function arguments |
37 | // or return values. their lifetime must not span a gc () call, i.e. |
45 | // or return values. their lifetime must not span a gc () call, i.e. |
38 | // they are only valid as temporary values within the same server tick. |
46 | // they are only valid as temporary values within the same server tick. |
39 | struct shstr_tmp |
47 | struct shstr_tmp |
40 | { |
48 | { |
41 | static const char *null; |
49 | static shstr_vec<sizeof ("(null)")> nullvec; |
|
|
50 | static const char *null () { return nullvec.string; } // this is the null pointer value |
42 | |
51 | |
43 | const char *s; |
52 | const char *s; |
44 | |
53 | |
45 | static unsigned int &length (const char *s) |
54 | static unsigned int &length (const char *s) |
46 | { |
55 | { |
… | |
… | |
61 | return length () >= plen && !strncasecmp (s, prefix, plen); |
70 | return length () >= plen && !strncasecmp (s, prefix, plen); |
62 | } |
71 | } |
63 | |
72 | |
64 | bool contains (const char *substring) const |
73 | bool contains (const char *substring) const |
65 | { |
74 | { |
66 | return strstr (s, substring); |
75 | return s != null () && strstr (s, substring); |
|
|
76 | } |
|
|
77 | |
|
|
78 | //TODO: case sensitive should be eradicated |
|
|
79 | bool eq_nc (const char *otherstring) const |
|
|
80 | { |
|
|
81 | return !strcasecmp (s, otherstring); |
67 | } |
82 | } |
68 | |
83 | |
69 | shstr_tmp () |
84 | shstr_tmp () |
70 | : s (null) |
85 | : s (null ()) |
71 | { |
86 | { |
72 | } |
87 | } |
73 | |
88 | |
74 | shstr_tmp (const shstr_tmp &sh) |
89 | shstr_tmp (const shstr_tmp &sh) |
75 | : s (sh.s) |
90 | : s (sh.s) |
… | |
… | |
84 | } |
99 | } |
85 | |
100 | |
86 | // this is used for informational messages and the like |
101 | // this is used for informational messages and the like |
87 | const char *operator &() const { return s; } |
102 | const char *operator &() const { return s; } |
88 | |
103 | |
89 | operator const char *() const { return s == null ? 0 : s; } |
104 | operator const char *() const { return s == null () ? 0 : s; } |
|
|
105 | |
|
|
106 | protected: |
|
|
107 | // dummy is there so it isn't used as type converter accidentally |
|
|
108 | shstr_tmp (int dummy, const char *s) |
|
|
109 | : s(s) |
|
|
110 | { |
|
|
111 | } |
90 | }; |
112 | }; |
91 | |
113 | |
92 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
114 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
93 | { |
115 | { |
94 | return a.s == b.s; |
116 | return a.s == b.s; |
… | |
… | |
97 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
119 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
98 | { |
120 | { |
99 | return a.s != b.s; |
121 | return a.s != b.s; |
100 | } |
122 | } |
101 | |
123 | |
102 | inline int strlen (const shstr_tmp &sh) |
124 | inline int strlen (shstr_tmp sh) |
103 | { |
125 | { |
104 | return sh.length (); |
126 | return sh.length (); |
105 | } |
127 | } |
106 | |
|
|
107 | // undefined external reference to catch people using strcmp when they shouldn't |
|
|
108 | int strcmp (const shstr_tmp &a, const shstr_tmp &b); |
|
|
109 | |
128 | |
110 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
129 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
111 | { |
130 | { |
112 | o.write (sh.s, sh.length ()); |
131 | o.write (sh.s, sh.length ()); |
113 | |
132 | |
… | |
… | |
146 | { |
165 | { |
147 | ++refcnt (); |
166 | ++refcnt (); |
148 | } |
167 | } |
149 | |
168 | |
150 | explicit shstr (const char *str) |
169 | explicit shstr (const char *str) |
151 | { |
|
|
152 | s = is_constant (str) && !str ? null : intern (str); |
170 | : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str)) |
|
|
171 | { |
153 | } |
172 | } |
154 | |
173 | |
155 | ~shstr () |
174 | ~shstr () |
156 | { |
175 | { |
157 | --refcnt (); |
176 | --refcnt (); |
… | |
… | |
178 | |
197 | |
179 | // shstr_tmp doesn't have this one |
198 | // shstr_tmp doesn't have this one |
180 | shstr &operator =(const char *str) |
199 | shstr &operator =(const char *str) |
181 | { |
200 | { |
182 | --refcnt (); |
201 | --refcnt (); |
183 | s = is_constant (str) && !str ? null : intern (str); |
202 | s = is_constant (str) && !str ? null () : intern (str); |
184 | |
203 | |
185 | return *this; |
204 | return *this; |
186 | } |
205 | } |
187 | }; |
206 | }; |
188 | |
207 | |
… | |
… | |
190 | // temporary passing, basically a non-refcounted shstr |
209 | // temporary passing, basically a non-refcounted shstr |
191 | struct shstr_cmp |
210 | struct shstr_cmp |
192 | { |
211 | { |
193 | const char *s; |
212 | const char *s; |
194 | |
213 | |
|
|
214 | // initialies to the non-matching string (as opposed to the null string) |
|
|
215 | shstr_cmp () |
|
|
216 | { |
|
|
217 | s = 0; |
|
|
218 | } |
|
|
219 | |
195 | explicit shstr_cmp (const char *str) |
220 | shstr_cmp (const char *str) |
196 | : s (shstr::find (str)) |
221 | : s (shstr::find (str)) |
197 | { |
222 | { |
198 | } |
223 | } |
199 | |
224 | |
200 | shstr_cmp (const shstr_cmp &sh) |
225 | shstr_cmp (shstr_tmp sh) |
201 | : s (sh.s) |
|
|
202 | { |
|
|
203 | } |
|
|
204 | |
|
|
205 | shstr_cmp (const shstr &sh) |
|
|
206 | : s (sh.s) |
226 | : s (sh.s) |
207 | { |
227 | { |
208 | } |
228 | } |
209 | |
229 | |
210 | // this is used for informational messages and the like |
230 | // this is used for informational messages and the like |
211 | const char *operator &() const { return s; } |
231 | const char *operator &() const { return s; } |
212 | |
232 | |
213 | shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
|
|
214 | operator const char *() const { return s; } |
233 | operator const char *() const { return s; } |
215 | }; |
234 | }; |
216 | |
235 | |
217 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
236 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
218 | { |
237 | { |
… | |
… | |
222 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
241 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
223 | { |
242 | { |
224 | return a.s == b.s; |
243 | return a.s == b.s; |
225 | } |
244 | } |
226 | |
245 | |
227 | extern const shstr shstr_null; |
|
|
228 | |
|
|
229 | #define def(str) extern const shstr shstr_ ## str; |
246 | #define def2(id,str) extern const shstr id; |
|
|
247 | #define def(id) def2(shstr_ ## id, # id) |
230 | # include "shstrinc.h" |
248 | # include "shstrinc.h" |
231 | #undef def |
249 | #undef def |
|
|
250 | #undef def2 |
|
|
251 | |
|
|
252 | // undefined external reference to catch people using str* functions when they shouldn't |
|
|
253 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
|
|
254 | template<class any> void strstr (const shstr_tmp &a, any b); |
232 | |
255 | |
233 | #endif |
256 | #endif |
234 | |
257 | |