ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/shstr.h
(Generate patch)

Comparing deliantra/server/include/shstr.h (file contents):
Revision 1.8 by root, Sun Sep 3 23:33:01 2006 UTC vs.
Revision 1.15 by root, Mon May 14 21:52:32 2007 UTC

1#ifndef SHSTR_H__ 1#ifndef SHSTR_H__
2#define SHSTR_H__ 2#define SHSTR_H__
3
4#include <sstream>
3 5
4#include "util.h" 6#include "util.h"
5 7
6extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); 8extern int buf_overflow (const char *buf1, const char *buf2, int bufsize);
7 9
27 } 29 }
28 30
29 int length () const 31 int length () const
30 { 32 {
31 return length (s); 33 return length (s);
34 }
35
36 // returns wether this shared string begins with the given prefix,
37 // used mainly for searched when users give only the start of a name.
38 bool begins_with (const char *prefix) const
39 {
40 int plen = strlen (prefix);
41 return !strncasecmp (s, prefix, plen) && length () >= plen;
32 } 42 }
33 43
34 static const char *find (const char *s); 44 static const char *find (const char *s);
35 static const char *intern (const char *s); 45 static const char *intern (const char *s);
36 46
74 84
75 const shstr &operator =(const char *str) 85 const shstr &operator =(const char *str)
76 { 86 {
77 --refcnt (); 87 --refcnt ();
78 88
79#if 0
80 // this optimises the important case of str == constant 0 89 // this optimises the important case of str == constant 0
81 if (is_constant (str)) 90 if (is_constant (str))
82 s = str ? intern (str) : null; 91 s = str ? intern (str) : null;
83 else 92 else
84#endif
85 s = intern (str); 93 s = intern (str);
86 94
87 return *this; 95 return *this;
96 }
97
98 bool operator ==(const shstr &b)
99 {
100 return s == b.s;
101 }
102
103 bool operator !=(const shstr &b)
104 {
105 return !(*this == b);
88 } 106 }
89}; 107};
90 108
91inline int strlen (const shstr &sh) 109inline int strlen (const shstr &sh)
92{ 110{
93 return sh.length (); 111 return sh.length ();
94} 112}
95 113
114inline int strcmp (const shstr &a, const shstr &b)
115{
116 // TODO: use this to find all the occurences of people using strcmp
117 // all uses should be bogus, as we should be never interested in
118 // comparing shstr's alphabetically
119#if 0
120 extern void do_not_use_strcmp_to_compare_shstr_values ();
121 do_not_use_strcmp_to_compare_shstr_values ();
122#endif
123 return a != b;
124}
125
126static std::ostream &operator <<(std::ostream &o, const shstr &sh)
127{
128 o.write (sh.s, sh.length ());
129 return o;
130}
131
132// only good for mass comparisons to shstr objects
133struct shstr_cmp
134{
135 const char *s;
136
137 explicit shstr_cmp (const char *s)
138 : s (shstr::find (s))
139 {
140 }
141
142 shstr_cmp (const shstr_cmp &sh)
143 : s (sh.s)
144 {
145 }
146
147 shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; }
148 operator const char *() const { return s; }
149};
150
96inline bool operator ==(const shstr &a, const shstr &b) 151inline bool operator ==(const shstr_cmp &a, const shstr &b)
97{ 152{
98 return a.s == b.s; 153 return a.s == b.s;
99} 154}
100 155
101inline bool operator !=(const shstr &a, const shstr &b) 156inline bool operator ==(const shstr &a, const shstr_cmp &b)
102{ 157{
103 return !(a == b); 158 return b == a;
104} 159}
160
161extern const shstr undead_name; /* Used in hit_player() in main.c */
105 162
106#endif 163#endif
107 164

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines