--- deliantra/server/include/shstr.h 2006/09/03 09:00:09 1.7 +++ deliantra/server/include/shstr.h 2007/05/14 21:52:32 1.15 @@ -1,10 +1,16 @@ #ifndef SHSTR_H__ #define SHSTR_H__ -extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); +#include + +#include "util.h" + +extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); struct shstr { + static const char *null; + const char *s; static int &refcnt (const char *s) @@ -24,7 +30,15 @@ int length () const { - return s ? length (s) : 0; + return length (s); + } + + // returns wether this shared string begins with the given prefix, + // used mainly for searched when users give only the start of a name. + bool begins_with (const char *prefix) const + { + int plen = strlen (prefix); + return !strncasecmp (s, prefix, plen) && length () >= plen; } static const char *find (const char *s); @@ -33,20 +47,20 @@ static void gc (); // garbage collect a few strings // this is used for informational messages and the like - const char *operator &() const { return s ? s : ""; } + const char *operator &() const { return s; } const char &operator [](int i) const { return s[i]; } - operator const char *() const { return s; } + operator const char *() const { return s == null ? 0 : s; } shstr () - : s (0) + : s (null) { } shstr (const shstr &sh) : s (sh.s) { - if (s) ++refcnt (); + ++refcnt (); } explicit shstr (const char *s) @@ -56,25 +70,40 @@ ~shstr () { - if (s) --refcnt (); + --refcnt (); } const shstr &operator =(const shstr &sh) { - if (s) --refcnt (); + --refcnt (); s = sh.s; - if (s) ++refcnt (); + ++refcnt (); return *this; } const shstr &operator =(const char *str) { - if (s) --refcnt (); - s = intern (str); + --refcnt (); + + // this optimises the important case of str == constant 0 + if (is_constant (str)) + s = str ? intern (str) : null; + else + s = intern (str); return *this; } + + bool operator ==(const shstr &b) + { + return s == b.s; + } + + bool operator !=(const shstr &b) + { + return !(*this == b); + } }; inline int strlen (const shstr &sh) @@ -82,15 +111,54 @@ return sh.length (); } -inline bool operator ==(const shstr &a, const shstr &b) +inline int strcmp (const shstr &a, const shstr &b) +{ + // TODO: use this to find all the occurences of people using strcmp + // all uses should be bogus, as we should be never interested in + // comparing shstr's alphabetically +#if 0 + extern void do_not_use_strcmp_to_compare_shstr_values (); + do_not_use_strcmp_to_compare_shstr_values (); +#endif + return a != b; +} + +static std::ostream &operator <<(std::ostream &o, const shstr &sh) +{ + o.write (sh.s, sh.length ()); + return o; +} + +// only good for mass comparisons to shstr objects +struct shstr_cmp +{ + const char *s; + + explicit shstr_cmp (const char *s) + : s (shstr::find (s)) + { + } + + shstr_cmp (const shstr_cmp &sh) + : s (sh.s) + { + } + + shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } + operator const char *() const { return s; } +}; + +inline bool operator ==(const shstr_cmp &a, const shstr &b) { return a.s == b.s; } -inline bool operator !=(const shstr &a, const shstr &b) +inline bool operator ==(const shstr &a, const shstr_cmp &b) { - return !(a == b); + return b == a; } +extern const shstr undead_name; /* Used in hit_player() in main.c */ + #endif