… | |
… | |
68 | int plen = strlen (prefix); |
68 | int plen = strlen (prefix); |
69 | |
69 | |
70 | return length () >= plen && !strncasecmp (s, prefix, plen); |
70 | return length () >= plen && !strncasecmp (s, prefix, plen); |
71 | } |
71 | } |
72 | |
72 | |
|
|
73 | // returns true if the substring is contained in the shstr |
|
|
74 | // if the shstr is 0, then this always returns false. |
|
|
75 | // the shstr is (theoretically) treated as a comma/colon/space etc. separated list. |
73 | bool contains (const char *substring) const |
76 | bool contains (const char *substring) const |
74 | { |
77 | { |
75 | return strstr (s, substring); |
78 | return s != null () && strstr (s, substring); |
|
|
79 | } |
|
|
80 | |
|
|
81 | //TODO: case sensitive should be eradicated |
|
|
82 | bool eq_nc (const char *otherstring) const |
|
|
83 | { |
|
|
84 | return !strcasecmp (s, otherstring); |
76 | } |
85 | } |
77 | |
86 | |
78 | shstr_tmp () |
87 | shstr_tmp () |
79 | : s (null ()) |
88 | : s (null ()) |
80 | { |
89 | { |
… | |
… | |
94 | |
103 | |
95 | // this is used for informational messages and the like |
104 | // this is used for informational messages and the like |
96 | const char *operator &() const { return s; } |
105 | const char *operator &() const { return s; } |
97 | |
106 | |
98 | operator const char *() const { return s == null () ? 0 : s; } |
107 | operator const char *() const { return s == null () ? 0 : s; } |
|
|
108 | |
|
|
109 | protected: |
|
|
110 | // dummy is there so it isn't used as type converter accidentally |
|
|
111 | shstr_tmp (int dummy, const char *s) |
|
|
112 | : s(s) |
|
|
113 | { |
|
|
114 | } |
99 | }; |
115 | }; |
100 | |
116 | |
101 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
117 | inline bool operator ==(const shstr_tmp &a, const shstr_tmp &b) |
102 | { |
118 | { |
103 | return a.s == b.s; |
119 | return a.s == b.s; |
… | |
… | |
106 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
122 | inline bool operator !=(const shstr_tmp &a, const shstr_tmp &b) |
107 | { |
123 | { |
108 | return a.s != b.s; |
124 | return a.s != b.s; |
109 | } |
125 | } |
110 | |
126 | |
111 | inline int strlen (const shstr_tmp &sh) |
127 | inline int strlen (shstr_tmp sh) |
112 | { |
128 | { |
113 | return sh.length (); |
129 | return sh.length (); |
114 | } |
130 | } |
115 | |
|
|
116 | // undefined external reference to catch people using strcmp when they shouldn't |
|
|
117 | int strcmp (const shstr_tmp &a, const shstr_tmp &b); |
|
|
118 | |
131 | |
119 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
132 | static std::ostream &operator <<(std::ostream &o, shstr_tmp sh) |
120 | { |
133 | { |
121 | o.write (sh.s, sh.length ()); |
134 | o.write (sh.s, sh.length ()); |
122 | |
135 | |
… | |
… | |
155 | { |
168 | { |
156 | ++refcnt (); |
169 | ++refcnt (); |
157 | } |
170 | } |
158 | |
171 | |
159 | explicit shstr (const char *str) |
172 | explicit shstr (const char *str) |
160 | { |
|
|
161 | s = is_constant (str) && !str ? null () : intern (str); |
173 | : shstr_tmp (0, is_constant (str) && !str ? null () : intern (str)) |
|
|
174 | { |
162 | } |
175 | } |
163 | |
176 | |
164 | ~shstr () |
177 | ~shstr () |
165 | { |
178 | { |
166 | --refcnt (); |
179 | --refcnt (); |
… | |
… | |
199 | // temporary passing, basically a non-refcounted shstr |
212 | // temporary passing, basically a non-refcounted shstr |
200 | struct shstr_cmp |
213 | struct shstr_cmp |
201 | { |
214 | { |
202 | const char *s; |
215 | const char *s; |
203 | |
216 | |
|
|
217 | // initialies to the non-matching string (as opposed to the null string) |
|
|
218 | shstr_cmp () |
|
|
219 | { |
|
|
220 | s = 0; |
|
|
221 | } |
|
|
222 | |
204 | explicit shstr_cmp (const char *str) |
223 | shstr_cmp (const char *str) |
205 | : s (shstr::find (str)) |
224 | : s (shstr::find (str)) |
206 | { |
225 | { |
207 | } |
226 | } |
208 | |
227 | |
209 | shstr_cmp (shstr_tmp sh) |
228 | shstr_cmp (shstr_tmp sh) |
… | |
… | |
225 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
244 | inline bool operator ==(const shstr_tmp &a, const shstr_cmp &b) |
226 | { |
245 | { |
227 | return a.s == b.s; |
246 | return a.s == b.s; |
228 | } |
247 | } |
229 | |
248 | |
230 | #define def(str) extern const shstr shstr_ ## str; |
249 | #define def2(id,str) extern const shstr id; |
|
|
250 | #define def(id) def2(shstr_ ## id, # id) |
231 | # include "shstrinc.h" |
251 | # include "shstrinc.h" |
232 | #undef def |
252 | #undef def |
|
|
253 | #undef def2 |
|
|
254 | |
|
|
255 | // undefined external reference to catch people using str* functions when they shouldn't |
|
|
256 | //template<class any> void strcmp (const shstr_tmp &a, any b); |
|
|
257 | template<class any> void strstr (const shstr_tmp &a, any b); |
233 | |
258 | |
234 | #endif |
259 | #endif |
235 | |
260 | |