1 | #ifndef SHSTR_H__ |
1 | #ifndef SHSTR_H__ |
2 | #define SHSTR_H__ |
2 | #define SHSTR_H__ |
3 | |
3 | |
|
|
4 | #include <sstream> |
|
|
5 | |
|
|
6 | #include "util.h" |
|
|
7 | |
4 | extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); |
8 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
5 | |
9 | |
6 | struct shstr |
10 | struct shstr |
7 | { |
11 | { |
|
|
12 | static const char *null; |
|
|
13 | |
8 | const char *s; |
14 | const char *s; |
9 | |
15 | |
10 | static int &refcnt (const char *s) |
16 | static int &refcnt (const char *s) |
11 | { |
17 | { |
12 | return *((int *)s - 1); |
18 | return *((int *)s - 1); |
… | |
… | |
22 | return refcnt (s); |
28 | return refcnt (s); |
23 | } |
29 | } |
24 | |
30 | |
25 | int length () const |
31 | int length () const |
26 | { |
32 | { |
27 | return s ? length (s) : 0; |
33 | return length (s); |
28 | } |
34 | } |
29 | |
35 | |
30 | static const char *find (const char *s); |
36 | static const char *find (const char *s); |
31 | static const char *intern (const char *s); |
37 | static const char *intern (const char *s); |
32 | |
38 | |
33 | static void gc (); // garbage collect a few strings |
39 | static void gc (); // garbage collect a few strings |
34 | |
40 | |
35 | // this is used for informational messages and the like |
41 | // this is used for informational messages and the like |
36 | const char *operator &() const { return s ? s : "<nil>"; } |
42 | const char *operator &() const { return s; } |
37 | |
43 | |
38 | const char &operator [](int i) const { return s[i]; } |
44 | const char &operator [](int i) const { return s[i]; } |
39 | operator const char *() const { return s; } |
45 | operator const char *() const { return s == null ? 0 : s; } |
40 | |
46 | |
41 | shstr () |
47 | shstr () |
42 | : s (0) |
48 | : s (null) |
43 | { |
49 | { |
44 | } |
50 | } |
45 | |
51 | |
46 | shstr (const shstr &sh) |
52 | shstr (const shstr &sh) |
47 | : s (sh.s) |
53 | : s (sh.s) |
48 | { |
54 | { |
49 | if (s) ++refcnt (); |
55 | ++refcnt (); |
50 | } |
56 | } |
51 | |
57 | |
52 | explicit shstr (const char *s) |
58 | explicit shstr (const char *s) |
53 | : s (intern (s)) |
59 | : s (intern (s)) |
54 | { |
60 | { |
55 | } |
61 | } |
56 | |
62 | |
57 | ~shstr () |
63 | ~shstr () |
58 | { |
64 | { |
59 | if (s) --refcnt (); |
65 | --refcnt (); |
60 | } |
66 | } |
61 | |
67 | |
62 | const shstr &operator =(const shstr &sh) |
68 | const shstr &operator =(const shstr &sh) |
63 | { |
69 | { |
64 | if (s) --refcnt (); |
70 | --refcnt (); |
65 | s = sh.s; |
71 | s = sh.s; |
66 | if (s) ++refcnt (); |
72 | ++refcnt (); |
67 | |
73 | |
68 | return *this; |
74 | return *this; |
69 | } |
75 | } |
70 | |
76 | |
71 | const shstr &operator =(const char *str) |
77 | const shstr &operator =(const char *str) |
72 | { |
78 | { |
73 | if (s) --refcnt (); |
79 | --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 |
74 | s = intern (str); |
85 | s = intern (str); |
75 | |
86 | |
76 | return *this; |
87 | return *this; |
|
|
88 | } |
|
|
89 | |
|
|
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); |
77 | } |
98 | } |
78 | }; |
99 | }; |
79 | |
100 | |
80 | inline int strlen (const shstr &sh) |
101 | inline int strlen (const shstr &sh) |
81 | { |
102 | { |
82 | return sh.length (); |
103 | return sh.length (); |
83 | } |
104 | } |
84 | |
105 | |
|
|
106 | 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 | static std::ostream &operator <<(std::ostream &o, const shstr &sh) |
|
|
119 | { |
|
|
120 | o.write (sh.s, sh.length ()); |
|
|
121 | return o; |
|
|
122 | } |
|
|
123 | |
|
|
124 | // 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 | 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 | operator const char *() const { return s; } |
|
|
141 | }; |
|
|
142 | |
85 | inline bool operator ==(const shstr &a, const shstr &b) |
143 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
86 | { |
144 | { |
87 | return a.s == b.s; |
145 | return a.s == b.s; |
88 | } |
146 | } |
89 | |
147 | |
90 | inline bool operator !=(const shstr &a, const shstr &b) |
148 | inline bool operator ==(const shstr &a, const shstr_cmp &b) |
91 | { |
149 | { |
92 | return !(a == b); |
150 | return b == a; |
93 | } |
151 | } |
|
|
152 | |
|
|
153 | extern const shstr undead_name; /* Used in hit_player() in main.c */ |
94 | |
154 | |
95 | #endif |
155 | #endif |
96 | |
156 | |