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