#ifndef SHSTR_H__ #define SHSTR_H__ extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); struct shstr { const char *s; int &refcnt () { return *((int *)s - 1); } static const char *find (const char *s); static const char *intern (const char *s); 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 [](int i) const { return s[i]; } operator const char *() const { return s; } int length () const { return s ? *((int *)s - 2) : 0; } shstr () : s (0) { } shstr (const shstr &sh) : s (sh.s) { if (s) ++refcnt (); } explicit shstr (const char *s) : s (intern (s)) { if (s) ++refcnt (); } ~shstr () { if (s) --refcnt (); } const shstr &operator =(const shstr &sh) { if (s) --refcnt (); s = sh.s; if (s) ++refcnt (); return *this; } const shstr &operator =(const char *str) { if (s) --refcnt (); s = intern (str); return *this; } }; inline int strlen (const shstr &sh) { return sh.length (); } inline bool operator ==(const shstr &a, const shstr &b) { return a.s == b.s; } inline bool operator !=(const shstr &a, const shstr &b) { return !(a == b); } #endif