1 |
root |
1.2 |
#ifndef SHSTR_H__ |
2 |
|
|
#define SHSTR_H__ |
3 |
|
|
|
4 |
root |
1.13 |
#include <sstream> |
5 |
|
|
|
6 |
root |
1.8 |
#include "util.h" |
7 |
|
|
|
8 |
|
|
extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
9 |
root |
1.5 |
|
10 |
|
|
struct shstr |
11 |
|
|
{ |
12 |
root |
1.8 |
static const char *null; |
13 |
|
|
|
14 |
root |
1.5 |
const char *s; |
15 |
root |
1.4 |
|
16 |
root |
1.7 |
static int &refcnt (const char *s) |
17 |
root |
1.5 |
{ |
18 |
|
|
return *((int *)s - 1); |
19 |
|
|
} |
20 |
|
|
|
21 |
root |
1.7 |
static int &length (const char *s) |
22 |
|
|
{ |
23 |
|
|
return *((int *)s - 2); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
int &refcnt () const |
27 |
|
|
{ |
28 |
|
|
return refcnt (s); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
int length () const |
32 |
|
|
{ |
33 |
root |
1.8 |
return length (s); |
34 |
root |
1.7 |
} |
35 |
|
|
|
36 |
root |
1.5 |
static const char *find (const char *s); |
37 |
|
|
static const char *intern (const char *s); |
38 |
|
|
|
39 |
|
|
static void gc (); // garbage collect a few strings |
40 |
|
|
|
41 |
root |
1.6 |
// this is used for informational messages and the like |
42 |
root |
1.8 |
const char *operator &() const { return s; } |
43 |
root |
1.5 |
|
44 |
root |
1.6 |
const char &operator [](int i) const { return s[i]; } |
45 |
root |
1.8 |
operator const char *() const { return s == null ? 0 : s; } |
46 |
root |
1.5 |
|
47 |
|
|
shstr () |
48 |
root |
1.8 |
: s (null) |
49 |
root |
1.5 |
{ |
50 |
|
|
} |
51 |
|
|
|
52 |
root |
1.6 |
shstr (const shstr &sh) |
53 |
root |
1.5 |
: s (sh.s) |
54 |
|
|
{ |
55 |
root |
1.8 |
++refcnt (); |
56 |
root |
1.5 |
} |
57 |
|
|
|
58 |
|
|
explicit shstr (const char *s) |
59 |
|
|
: s (intern (s)) |
60 |
|
|
{ |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
~shstr () |
64 |
|
|
{ |
65 |
root |
1.8 |
--refcnt (); |
66 |
root |
1.5 |
} |
67 |
|
|
|
68 |
|
|
const shstr &operator =(const shstr &sh) |
69 |
|
|
{ |
70 |
root |
1.8 |
--refcnt (); |
71 |
root |
1.5 |
s = sh.s; |
72 |
root |
1.8 |
++refcnt (); |
73 |
root |
1.5 |
|
74 |
|
|
return *this; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
const shstr &operator =(const char *str) |
78 |
|
|
{ |
79 |
root |
1.8 |
--refcnt (); |
80 |
|
|
|
81 |
|
|
// this optimises the important case of str == constant 0 |
82 |
|
|
if (is_constant (str)) |
83 |
|
|
s = str ? intern (str) : null; |
84 |
|
|
else |
85 |
|
|
s = intern (str); |
86 |
root |
1.1 |
|
87 |
root |
1.5 |
return *this; |
88 |
|
|
} |
89 |
root |
1.10 |
|
90 |
|
|
bool operator ==(const shstr &b) |
91 |
|
|
{ |
92 |
|
|
return s == b.s; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
bool operator !=(const shstr &b) |
96 |
|
|
{ |
97 |
|
|
return !(*this == b); |
98 |
|
|
} |
99 |
root |
1.5 |
}; |
100 |
root |
1.1 |
|
101 |
root |
1.5 |
inline int strlen (const shstr &sh) |
102 |
|
|
{ |
103 |
|
|
return sh.length (); |
104 |
|
|
} |
105 |
root |
1.2 |
|
106 |
root |
1.12 |
inline int strcmp (const shstr &a, const shstr &b) |
107 |
|
|
{ |
108 |
|
|
// TODO: use this to find all the occurences of people using strcmp |
109 |
|
|
// all uses should be bogus, as we should be never interested in |
110 |
|
|
// comparing shstr's alphabetically |
111 |
|
|
#if 0 |
112 |
|
|
extern void do_not_use_strcmp_to_compare_shstr_values (); |
113 |
|
|
do_not_use_strcmp_to_compare_shstr_values (); |
114 |
|
|
#endif |
115 |
|
|
return a != b; |
116 |
|
|
} |
117 |
|
|
|
118 |
root |
1.13 |
std::ostream &operator <<(std::ostream &o, const shstr &sh) |
119 |
|
|
{ |
120 |
|
|
o.write (sh.s, sh.length ()); |
121 |
|
|
return o; |
122 |
|
|
} |
123 |
|
|
|
124 |
root |
1.10 |
// only good for mass comparisons to shstr objects |
125 |
|
|
struct shstr_cmp |
126 |
|
|
{ |
127 |
|
|
const char *s; |
128 |
|
|
|
129 |
|
|
explicit shstr_cmp (const char *s) |
130 |
|
|
: s (shstr::find (s)) |
131 |
|
|
{ |
132 |
|
|
} |
133 |
|
|
|
134 |
root |
1.11 |
shstr_cmp (const shstr_cmp &sh) |
135 |
|
|
: s (sh.s) |
136 |
|
|
{ |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
shstr_cmp &operator =(const shstr_cmp sh) { s = sh.s; return *this; } |
140 |
root |
1.10 |
operator const char *() const { return s; } |
141 |
|
|
}; |
142 |
|
|
|
143 |
|
|
inline bool operator ==(const shstr_cmp &a, const shstr &b) |
144 |
root |
1.5 |
{ |
145 |
|
|
return a.s == b.s; |
146 |
|
|
} |
147 |
root |
1.4 |
|
148 |
root |
1.10 |
inline bool operator ==(const shstr &a, const shstr_cmp &b) |
149 |
root |
1.4 |
{ |
150 |
root |
1.10 |
return b == a; |
151 |
root |
1.5 |
} |
152 |
root |
1.4 |
|
153 |
root |
1.9 |
extern const shstr undead_name; /* Used in hit_player() in main.c */ |
154 |
|
|
|
155 |
root |
1.2 |
#endif |
156 |
|
|
|