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 (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify |
6 | * Deliantra is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
9 | * (at your option) any later version. |
… | |
… | |
20 | */ |
20 | */ |
21 | |
21 | |
22 | #ifndef SHSTR_H__ |
22 | #ifndef SHSTR_H__ |
23 | #define SHSTR_H__ |
23 | #define SHSTR_H__ |
24 | |
24 | |
|
|
25 | #include <cstring> |
25 | #include <sstream> |
26 | #include <sstream> |
26 | |
27 | |
27 | #include "util.h" |
28 | #include "util.h" |
28 | |
29 | |
29 | extern size_t shstr_alloc; |
30 | extern size_t shstr_alloc; |
30 | |
31 | |
31 | 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); |
32 | |
33 | |
|
|
34 | // this class is a non-refcounted shared string |
|
|
35 | // 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 |
|
|
37 | // 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. |
33 | struct shstr |
39 | struct shstr_tmp |
34 | { |
40 | { |
35 | static const char *null; |
41 | static const char *null; |
36 | |
42 | |
37 | const char *s; |
43 | const char *s; |
38 | |
44 | |
39 | static int &refcnt (const char *s) |
|
|
40 | { |
|
|
41 | return *((int *)s - 1); |
|
|
42 | } |
|
|
43 | |
|
|
44 | static int &length (const char *s) |
45 | static unsigned int &length (const char *s) |
45 | { |
46 | { |
46 | return *((int *)s - 2); |
47 | return *((unsigned int *)s - 2); |
47 | } |
48 | } |
48 | |
49 | |
49 | int &refcnt () const |
50 | int length () const |
|
|
51 | { |
|
|
52 | return length (s); |
|
|
53 | } |
|
|
54 | |
|
|
55 | // returns whether this shared string begins with the given prefix, |
|
|
56 | // used mainly for searches when users give only the start of a name. |
|
|
57 | bool starts_with (const char *prefix) const |
|
|
58 | { |
|
|
59 | int plen = strlen (prefix); |
|
|
60 | |
|
|
61 | return length () >= plen && !strncasecmp (s, prefix, plen); |
|
|
62 | } |
|
|
63 | |
|
|
64 | bool contains (const char *substring) const |
|
|
65 | { |
|
|
66 | return strstr (s, substring); |
|
|
67 | } |
|
|
68 | |
|
|
69 | shstr_tmp () |
|
|
70 | : s (null) |
|
|
71 | { |
|
|
72 | } |
|
|
73 | |
|
|
74 | shstr_tmp (const shstr_tmp &sh) |
|
|
75 | : s (sh.s) |
|
|
76 | { |
|
|
77 | } |
|
|
78 | |
|
|
79 | shstr_tmp operator =(const shstr_tmp &sh) |
|
|
80 | { |
|
|
81 | s = sh.s; |
|
|
82 | |
|
|
83 | return *this; |
|
|
84 | } |
|
|
85 | |
|
|
86 | // this is used for informational messages and the like |
|
|
87 | const char *operator &() const { return s; } |
|
|
88 | |
|
|
89 | operator const char *() const { return s == null ? 0 : s; } |
|
|
90 | }; |
|
|
91 | |
|
|
92 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
|
|
93 | { |
|
|
94 | return a.s == b.s; |
|
|
95 | } |
|
|
96 | |
|
|
97 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
|
|
98 | { |
|
|
99 | return a.s != b.s; |
|
|
100 | } |
|
|
101 | |
|
|
102 | inline int strlen (const shstr_tmp &sh) |
|
|
103 | { |
|
|
104 | return sh.length (); |
|
|
105 | } |
|
|
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 | |
|
|
110 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
|
|
111 | { |
|
|
112 | o.write (sh.s, sh.length ()); |
|
|
113 | |
|
|
114 | return o; |
|
|
115 | } |
|
|
116 | |
|
|
117 | struct shstr : shstr_tmp |
|
|
118 | { |
|
|
119 | static unsigned int &refcnt (const char *s) |
|
|
120 | { |
|
|
121 | return *((unsigned int *)s - 1); |
|
|
122 | } |
|
|
123 | |
|
|
124 | unsigned int &refcnt () const |
50 | { |
125 | { |
51 | return refcnt (s); |
126 | return refcnt (s); |
52 | } |
127 | } |
53 | |
128 | |
54 | int length () const |
129 | shstr () |
55 | { |
130 | { |
56 | return length (s); |
|
|
57 | } |
|
|
58 | |
|
|
59 | // returns wether this shared string begins with the given prefix, |
|
|
60 | // used mainly for searched when users give only the start of a name. |
|
|
61 | bool begins_with (const char *prefix) const |
|
|
62 | { |
|
|
63 | int plen = strlen (prefix); |
|
|
64 | return !strncasecmp (s, prefix, plen) && length () >= plen; |
|
|
65 | } |
131 | } |
66 | |
132 | |
67 | static const char *find (const char *s); |
133 | static const char *find (const char *s); |
68 | static const char *intern (const char *s); |
134 | static const char *intern (const char *s); |
69 | |
135 | |
70 | static void gc (); // garbage collect a few strings |
136 | static void gc (); // garbage collect a few strings |
71 | |
137 | |
72 | // this is used for informational messages and the like |
|
|
73 | const char *operator &() const { return s; } |
|
|
74 | |
|
|
75 | const char &operator [](int i) const { return s[i]; } |
|
|
76 | operator const char *() const { return s == null ? 0 : s; } |
|
|
77 | |
|
|
78 | shstr () |
|
|
79 | : s (null) |
|
|
80 | { |
|
|
81 | } |
|
|
82 | |
|
|
83 | shstr (const shstr &sh) |
138 | shstr (const shstr &sh) |
84 | : s (sh.s) |
139 | : shstr_tmp (sh) |
85 | { |
140 | { |
86 | ++refcnt (); |
141 | ++refcnt (); |
87 | } |
142 | } |
88 | |
143 | |
|
|
144 | shstr (const shstr_tmp &sh) |
|
|
145 | : shstr_tmp (sh) |
|
|
146 | { |
|
|
147 | ++refcnt (); |
|
|
148 | } |
|
|
149 | |
89 | explicit shstr (const char *s) |
150 | explicit shstr (const char *str) |
90 | : s (intern (s)) |
|
|
91 | { |
151 | { |
|
|
152 | s = is_constant (str) && !str ? null : intern (str); |
92 | } |
153 | } |
93 | |
154 | |
94 | ~shstr () |
155 | ~shstr () |
95 | { |
156 | { |
96 | --refcnt (); |
157 | --refcnt (); |
97 | } |
158 | } |
98 | |
159 | |
|
|
160 | using shstr_tmp::operator &; |
|
|
161 | using shstr_tmp::operator const char *; |
|
|
162 | |
|
|
163 | // (note: not the copy constructor) |
99 | const shstr &operator =(const shstr &sh) |
164 | shstr &operator =(const shstr_tmp &sh) |
100 | { |
165 | { |
101 | --refcnt (); |
166 | --refcnt (); |
102 | s = sh.s; |
167 | s = sh.s; |
103 | ++refcnt (); |
168 | ++refcnt (); |
104 | |
169 | |
105 | return *this; |
170 | return *this; |
106 | } |
171 | } |
107 | |
172 | |
|
|
173 | // here it comes |
|
|
174 | shstr &operator =(const shstr &sh) |
|
|
175 | { |
|
|
176 | return (*this) = (shstr_tmp)sh; |
|
|
177 | } |
|
|
178 | |
|
|
179 | // shstr_tmp doesn't have this one |
108 | const shstr &operator =(const char *str) |
180 | shstr &operator =(const char *str) |
109 | { |
181 | { |
110 | --refcnt (); |
182 | --refcnt (); |
111 | |
183 | s = is_constant (str) && !str ? null : intern (str); |
112 | // this optimises the important case of str == constant 0 |
|
|
113 | if (is_constant (str)) |
|
|
114 | s = str ? intern (str) : null; |
|
|
115 | else |
|
|
116 | s = intern (str); |
|
|
117 | |
184 | |
118 | return *this; |
185 | return *this; |
119 | } |
186 | } |
120 | |
|
|
121 | bool operator ==(const shstr &b) |
|
|
122 | { |
|
|
123 | return s == b.s; |
|
|
124 | } |
|
|
125 | |
|
|
126 | bool operator !=(const shstr &b) |
|
|
127 | { |
|
|
128 | return !(*this == b); |
|
|
129 | } |
|
|
130 | }; |
187 | }; |
131 | |
188 | |
132 | inline int strlen (const shstr &sh) |
|
|
133 | { |
|
|
134 | return sh.length (); |
|
|
135 | } |
|
|
136 | |
|
|
137 | inline int strcmp (const shstr &a, const shstr &b) |
|
|
138 | { |
|
|
139 | // TODO: use this to find all the occurences of people using strcmp |
|
|
140 | // all uses should be bogus, as we should be never interested in |
|
|
141 | // comparing shstr's alphabetically |
|
|
142 | #if 0 |
|
|
143 | extern void do_not_use_strcmp_to_compare_shstr_values (); |
|
|
144 | do_not_use_strcmp_to_compare_shstr_values (); |
|
|
145 | #endif |
|
|
146 | return a != b; |
|
|
147 | } |
|
|
148 | |
|
|
149 | static std::ostream &operator <<(std::ostream &o, const shstr &sh) |
|
|
150 | { |
|
|
151 | o.write (sh.s, sh.length ()); |
|
|
152 | return o; |
|
|
153 | } |
|
|
154 | |
|
|
155 | // only good for mass comparisons to shstr objects |
189 | // only good for mass comparisons to shstr objects, or very |
|
|
190 | // temporary passing, basically a non-refcounted shstr |
156 | struct shstr_cmp |
191 | struct shstr_cmp |
157 | { |
192 | { |
158 | const char *s; |
193 | const char *s; |
159 | |
194 | |
160 | explicit shstr_cmp (const char *s) |
195 | explicit shstr_cmp (const char *str) |
161 | : s (shstr::find (s)) |
196 | : s (shstr::find (str)) |
162 | { |
197 | { |
163 | } |
198 | } |
164 | |
199 | |
165 | shstr_cmp (const shstr_cmp &sh) |
200 | shstr_cmp (const shstr_cmp &sh) |
166 | : s (sh.s) |
201 | : s (sh.s) |
167 | { |
202 | { |
168 | } |
203 | } |
169 | |
204 | |
|
|
205 | shstr_cmp (const shstr &sh) |
|
|
206 | : s (sh.s) |
|
|
207 | { |
|
|
208 | } |
|
|
209 | |
|
|
210 | // this is used for informational messages and the like |
|
|
211 | const char *operator &() const { return s; } |
|
|
212 | |
170 | shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
213 | shstr_cmp operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
171 | operator const char *() const { return s; } |
214 | operator const char *() const { return s; } |
172 | }; |
215 | }; |
173 | |
216 | |
174 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
217 | inline bool operator ==(const shstr_cmp &a, const shstr_tmp &b) |
175 | { |
218 | { |
176 | return a.s == b.s; |
219 | return a.s == b.s; |
177 | } |
220 | } |
178 | |
221 | |
179 | inline bool operator ==(const shstr &a, const shstr_cmp &b) |
222 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
180 | { |
223 | { |
181 | return b == a; |
224 | return a.s == b.s; |
182 | } |
225 | } |
183 | |
226 | |
184 | extern const shstr shstr_null; |
227 | extern const shstr shstr_null; |
185 | |
228 | |
186 | #define def(str) extern const shstr shstr_ ## str; |
229 | #define def(str) extern const shstr shstr_ ## str; |