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