1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * (at your option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
18 | * |
20 | * |
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | */ |
22 | */ |
21 | |
23 | |
22 | #ifndef SHSTR_H__ |
24 | #ifndef SHSTR_H__ |
23 | #define SHSTR_H__ |
25 | #define SHSTR_H__ |
… | |
… | |
32 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
34 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
33 | |
35 | |
34 | template<int size> |
36 | template<int size> |
35 | struct shstr_vec |
37 | struct shstr_vec |
36 | { |
38 | { |
|
|
39 | uint32_t hash; |
37 | uint32_t len; |
40 | uint32_t len; |
38 | uint32_t refcnt; |
41 | uint32_t refcnt; |
|
|
42 | // pointer points here |
39 | char string[size]; |
43 | char string [size]; |
40 | }; |
44 | }; |
41 | |
45 | |
42 | // this class is a non-refcounted shared string |
46 | // this class is a non-refcounted shared string |
43 | // it cannot be used to create or store shared strings, but |
47 | // it cannot be used to create or store shared strings, but |
44 | // it can be used to apss shared strings around, i.e. as function arguments |
48 | // it can be used to pass shared strings around, i.e. as function arguments |
45 | // or return values. their lifetime must not span a gc () call, i.e. |
49 | // or return values. their lifetime must not span a gc () call, i.e. |
46 | // they are only valid as temporary values within the same server tick. |
50 | // they are only valid as temporary values within the same server tick. |
47 | struct shstr_tmp |
51 | struct shstr_tmp |
48 | { |
52 | { |
49 | static shstr_vec<sizeof ("(null)")> nullvec; |
53 | static shstr_vec<sizeof ("(null)")> nullvec; |
50 | static const char *null () { return nullvec.string; } // this is the null pointer value |
54 | static const char *null () { return nullvec.string; } // this is the null pointer value |
51 | |
55 | |
52 | const char *s; |
56 | const char *s; |
53 | |
57 | |
|
|
58 | static unsigned int &hash (const char *s) |
|
|
59 | { |
|
|
60 | return *((unsigned int *)s - 3); |
|
|
61 | } |
|
|
62 | |
|
|
63 | int hash () const |
|
|
64 | { |
|
|
65 | return hash (s); |
|
|
66 | } |
|
|
67 | |
54 | static unsigned int &length (const char *s) |
68 | static unsigned int &length (const char *s) |
55 | { |
69 | { |
56 | return *((unsigned int *)s - 2); |
70 | return *((unsigned int *)s - 2); |
57 | } |
71 | } |
58 | |
72 | |
… | |
… | |
71 | } |
85 | } |
72 | |
86 | |
73 | // returns true if the substring is contained in the shstr |
87 | // returns true if the substring is contained in the shstr |
74 | // if the shstr is 0, then this always returns false. |
88 | // if the shstr is 0, then this always returns false. |
75 | // the shstr is (theoretically) treated as a comma/colon/space etc. separated list. |
89 | // the shstr is (theoretically) treated as a comma/colon/space etc. separated list. |
76 | bool contains (const char *substring) const |
90 | bool contains (const char *substring) const; |
77 | { |
|
|
78 | return s != null () && strstr (s, substring); |
|
|
79 | } |
|
|
80 | |
91 | |
81 | //TODO: case sensitive should be eradicated |
92 | //TODO: case sensitive should be eradicated |
82 | bool eq_nc (const char *otherstring) const |
93 | bool eq_nc (const char *otherstring) const |
83 | { |
94 | { |
84 | return !strcasecmp (s, otherstring); |
95 | return !strcasecmp (s, otherstring); |
… | |
… | |
127 | inline int strlen (shstr_tmp sh) |
138 | inline int strlen (shstr_tmp sh) |
128 | { |
139 | { |
129 | return sh.length (); |
140 | return sh.length (); |
130 | } |
141 | } |
131 | |
142 | |
132 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
143 | static inline std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
133 | { |
144 | { |
134 | o.write (sh.s, sh.length ()); |
145 | o.write (sh.s, sh.length ()); |
135 | |
146 | |
136 | return o; |
147 | return o; |
137 | } |
148 | } |
… | |
… | |
168 | { |
179 | { |
169 | ++refcnt (); |
180 | ++refcnt (); |
170 | } |
181 | } |
171 | |
182 | |
172 | explicit shstr (const char *str) |
183 | explicit shstr (const char *str) |
173 | : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str)) |
184 | : shstr_tmp (0, ecb_is_constant (str) && !str ? null () : intern (str)) |
174 | { |
185 | { |
175 | } |
186 | } |
176 | |
187 | |
177 | ~shstr () |
188 | ~shstr () |
178 | { |
189 | { |
… | |
… | |
200 | |
211 | |
201 | // shstr_tmp doesn't have this one |
212 | // shstr_tmp doesn't have this one |
202 | shstr &operator =(const char *str) |
213 | shstr &operator =(const char *str) |
203 | { |
214 | { |
204 | --refcnt (); |
215 | --refcnt (); |
205 | s = is_constant (str) && !str ? null () : intern (str); |
216 | s = ecb_is_constant (str) && !str ? null () : intern (str); |
206 | |
217 | |
207 | return *this; |
218 | return *this; |
208 | } |
219 | } |
209 | }; |
220 | }; |
210 | |
221 | |