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