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.10 by root, Tue Sep 12 18:15:34 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
99inline int strlen (const shstr &sh) 109inline int strlen (const shstr &sh)
100{ 110{
101 return sh.length (); 111 return sh.length ();
102} 112}
103 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
104// only good for mass comparisons to shstr objects 132// only good for mass comparisons to shstr objects
105struct shstr_cmp 133struct shstr_cmp
106{ 134{
107 const char *s; 135 const char *s;
108 136
109 explicit shstr_cmp (const char *s) 137 explicit shstr_cmp (const char *s)
110 : s (shstr::find (s)) 138 : s (shstr::find (s))
111 { 139 {
112 } 140 }
113 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; }
114 operator const char *() const { return s; } 148 operator const char *() const { return s; }
115}; 149};
116 150
117inline bool operator ==(const shstr_cmp &a, const shstr &b) 151inline bool operator ==(const shstr_cmp &a, const shstr &b)
118{ 152{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines