1 | #ifndef SHSTR_H__ |
1 | #ifndef SHSTR_H__ |
2 | #define SHSTR_H__ |
2 | #define SHSTR_H__ |
3 | |
3 | |
|
|
4 | #include "util.h" |
|
|
5 | |
4 | extern int buf_overflow(const char *buf1, const char *buf2, int bufsize); |
6 | extern int buf_overflow (const char *buf1, const char *buf2, int bufsize); |
5 | |
7 | |
6 | struct shstr |
8 | struct shstr |
7 | { |
9 | { |
|
|
10 | static const char *null; |
|
|
11 | |
8 | const char *s; |
12 | const char *s; |
9 | |
13 | |
10 | int &refcnt () |
14 | static int &refcnt (const char *s) |
11 | { |
15 | { |
12 | return *((int *)s - 1); |
16 | return *((int *)s - 1); |
|
|
17 | } |
|
|
18 | |
|
|
19 | 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 | return length (s); |
13 | } |
32 | } |
14 | |
33 | |
15 | static const char *find (const char *s); |
34 | static const char *find (const char *s); |
16 | static const char *intern (const char *s); |
35 | static const char *intern (const char *s); |
17 | |
36 | |
18 | static void gc (); // garbage collect a few strings |
37 | static void gc (); // garbage collect a few strings |
19 | |
38 | |
20 | // this is used for informational messages and the like |
39 | // this is used for informational messages and the like |
21 | const char *operator &() const { return s ? s : "<nil>"; } |
40 | const char *operator &() const { return s; } |
22 | |
41 | |
23 | const char &operator [](int i) const { return s[i]; } |
42 | const char &operator [](int i) const { return s[i]; } |
24 | operator const char *() const { return s; } |
43 | operator const char *() const { return s == null ? 0 : s; } |
25 | |
|
|
26 | int length () const |
|
|
27 | { |
|
|
28 | return s ? *((int *)s - 2) : 0; |
|
|
29 | } |
|
|
30 | |
44 | |
31 | shstr () |
45 | shstr () |
32 | : s (0) |
46 | : s (null) |
33 | { |
47 | { |
34 | } |
48 | } |
35 | |
49 | |
36 | shstr (const shstr &sh) |
50 | shstr (const shstr &sh) |
37 | : s (sh.s) |
51 | : s (sh.s) |
38 | { |
52 | { |
39 | if (s) ++refcnt (); |
53 | ++refcnt (); |
40 | } |
54 | } |
41 | |
55 | |
42 | explicit shstr (const char *s) |
56 | explicit shstr (const char *s) |
43 | : s (intern (s)) |
57 | : s (intern (s)) |
44 | { |
58 | { |
45 | if (s) ++refcnt (); |
|
|
46 | } |
59 | } |
47 | |
60 | |
48 | ~shstr () |
61 | ~shstr () |
49 | { |
62 | { |
50 | if (s) --refcnt (); |
63 | --refcnt (); |
51 | } |
64 | } |
52 | |
65 | |
53 | const shstr &operator =(const shstr &sh) |
66 | const shstr &operator =(const shstr &sh) |
54 | { |
67 | { |
55 | if (s) --refcnt (); |
68 | --refcnt (); |
56 | s = sh.s; |
69 | s = sh.s; |
57 | if (s) ++refcnt (); |
70 | ++refcnt (); |
58 | |
71 | |
59 | return *this; |
72 | return *this; |
60 | } |
73 | } |
61 | |
74 | |
62 | const shstr &operator =(const char *str) |
75 | const shstr &operator =(const char *str) |
63 | { |
76 | { |
64 | if (s) --refcnt (); |
77 | --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 |
65 | s = intern (str); |
83 | s = intern (str); |
66 | |
84 | |
67 | return *this; |
85 | return *this; |
68 | } |
86 | } |
69 | }; |
87 | }; |
70 | |
88 | |
… | |
… | |
81 | inline bool operator !=(const shstr &a, const shstr &b) |
99 | inline bool operator !=(const shstr &a, const shstr &b) |
82 | { |
100 | { |
83 | return !(a == b); |
101 | return !(a == b); |
84 | } |
102 | } |
85 | |
103 | |
|
|
104 | extern const shstr undead_name; /* Used in hit_player() in main.c */ |
|
|
105 | |
86 | #endif |
106 | #endif |
87 | |
107 | |