1 | #ifndef SHSTR_H__ |
1 | #ifndef SHSTR_H__ |
2 | #define SHSTR_H__ |
2 | #define SHSTR_H__ |
|
|
3 | |
|
|
4 | #include <sstream> |
3 | |
5 | |
4 | #include "util.h" |
6 | #include "util.h" |
5 | |
7 | |
6 | 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); |
7 | |
9 | |
… | |
… | |
74 | |
76 | |
75 | const shstr &operator =(const char *str) |
77 | const shstr &operator =(const char *str) |
76 | { |
78 | { |
77 | --refcnt (); |
79 | --refcnt (); |
78 | |
80 | |
79 | #if 0 |
|
|
80 | // this optimises the important case of str == constant 0 |
81 | // this optimises the important case of str == constant 0 |
81 | if (is_constant (str)) |
82 | if (is_constant (str)) |
82 | s = str ? intern (str) : null; |
83 | s = str ? intern (str) : null; |
83 | else |
84 | else |
84 | #endif |
|
|
85 | s = intern (str); |
85 | s = intern (str); |
86 | |
86 | |
87 | 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); |
88 | } |
98 | } |
89 | }; |
99 | }; |
90 | |
100 | |
91 | inline int strlen (const shstr &sh) |
101 | inline int strlen (const shstr &sh) |
92 | { |
102 | { |
93 | return sh.length (); |
103 | return sh.length (); |
94 | } |
104 | } |
95 | |
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 | 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 | |
96 | inline bool operator ==(const shstr &a, const shstr &b) |
143 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
97 | { |
144 | { |
98 | return a.s == b.s; |
145 | return a.s == b.s; |
99 | } |
146 | } |
100 | |
147 | |
101 | inline bool operator !=(const shstr &a, const shstr &b) |
148 | inline bool operator ==(const shstr &a, const shstr_cmp &b) |
102 | { |
149 | { |
103 | return !(a == b); |
150 | return b == a; |
104 | } |
151 | } |
|
|
152 | |
|
|
153 | extern const shstr undead_name; /* Used in hit_player() in main.c */ |
105 | |
154 | |
106 | #endif |
155 | #endif |
107 | |
156 | |