|
|
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
|
|
5 | * |
|
|
6 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
|
|
7 | * under the terms of the GNU General Public License as published by the Free |
|
|
8 | * Software Foundation; either version 2 of the License, or (at your option) |
|
|
9 | * any later version. |
|
|
10 | * |
|
|
11 | * This program is distributed in the hope that it will be useful, but |
|
|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|
|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
14 | * for more details. |
|
|
15 | * |
|
|
16 | * You should have received a copy of the GNU General Public License along |
|
|
17 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
|
|
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
|
|
21 | */ |
|
|
22 | |
1 | #ifndef SHSTR_H__ |
23 | #ifndef SHSTR_H__ |
2 | #define SHSTR_H__ |
24 | #define SHSTR_H__ |
|
|
25 | |
|
|
26 | #include <sstream> |
3 | |
27 | |
4 | #include "util.h" |
28 | #include "util.h" |
5 | |
29 | |
6 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
30 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
7 | |
31 | |
… | |
… | |
27 | } |
51 | } |
28 | |
52 | |
29 | int length () const |
53 | int length () const |
30 | { |
54 | { |
31 | return length (s); |
55 | return length (s); |
|
|
56 | } |
|
|
57 | |
|
|
58 | // returns wether this shared string begins with the given prefix, |
|
|
59 | // used mainly for searched when users give only the start of a name. |
|
|
60 | bool begins_with (const char *prefix) const |
|
|
61 | { |
|
|
62 | int plen = strlen (prefix); |
|
|
63 | return !strncasecmp (s, prefix, plen) && length () >= plen; |
32 | } |
64 | } |
33 | |
65 | |
34 | static const char *find (const char *s); |
66 | static const char *find (const char *s); |
35 | static const char *intern (const char *s); |
67 | static const char *intern (const char *s); |
36 | |
68 | |
… | |
… | |
99 | inline int strlen (const shstr &sh) |
131 | inline int strlen (const shstr &sh) |
100 | { |
132 | { |
101 | return sh.length (); |
133 | return sh.length (); |
102 | } |
134 | } |
103 | |
135 | |
|
|
136 | inline int strcmp (const shstr &a, const shstr &b) |
|
|
137 | { |
|
|
138 | // TODO: use this to find all the occurences of people using strcmp |
|
|
139 | // all uses should be bogus, as we should be never interested in |
|
|
140 | // comparing shstr's alphabetically |
|
|
141 | #if 0 |
|
|
142 | extern void do_not_use_strcmp_to_compare_shstr_values (); |
|
|
143 | do_not_use_strcmp_to_compare_shstr_values (); |
|
|
144 | #endif |
|
|
145 | return a != b; |
|
|
146 | } |
|
|
147 | |
|
|
148 | static std::ostream &operator <<(std::ostream &o, const shstr &sh) |
|
|
149 | { |
|
|
150 | o.write (sh.s, sh.length ()); |
|
|
151 | return o; |
|
|
152 | } |
|
|
153 | |
104 | // only good for mass comparisons to shstr objects |
154 | // only good for mass comparisons to shstr objects |
105 | struct shstr_cmp |
155 | struct shstr_cmp |
106 | { |
156 | { |
107 | const char *s; |
157 | const char *s; |
108 | |
158 | |
109 | explicit shstr_cmp (const char *s) |
159 | explicit shstr_cmp (const char *s) |
110 | : s (shstr::find (s)) |
160 | : s (shstr::find (s)) |
111 | { |
161 | { |
112 | } |
162 | } |
113 | |
163 | |
|
|
164 | shstr_cmp (const shstr_cmp &sh) |
|
|
165 | : s (sh.s) |
|
|
166 | { |
|
|
167 | } |
|
|
168 | |
|
|
169 | shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
114 | operator const char *() const { return s; } |
170 | operator const char *() const { return s; } |
115 | }; |
171 | }; |
116 | |
172 | |
117 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
173 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
118 | { |
174 | { |