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 | |
… | |
… | |
27 | } |
29 | } |
28 | |
30 | |
29 | int length () const |
31 | int length () const |
30 | { |
32 | { |
31 | return length (s); |
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; |
32 | } |
42 | } |
33 | |
43 | |
34 | static const char *find (const char *s); |
44 | static const char *find (const char *s); |
35 | static const char *intern (const char *s); |
45 | static const char *intern (const char *s); |
36 | |
46 | |
… | |
… | |
99 | inline int strlen (const shstr &sh) |
109 | inline int strlen (const shstr &sh) |
100 | { |
110 | { |
101 | return sh.length (); |
111 | return sh.length (); |
102 | } |
112 | } |
103 | |
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 | |
104 | // only good for mass comparisons to shstr objects |
132 | // only good for mass comparisons to shstr objects |
105 | struct shstr_cmp |
133 | struct shstr_cmp |
106 | { |
134 | { |
107 | const char *s; |
135 | const char *s; |
108 | |
136 | |
109 | explicit shstr_cmp (const char *s) |
137 | explicit shstr_cmp (const char *s) |
110 | : s (shstr::find (s)) |
138 | : s (shstr::find (s)) |
111 | { |
139 | { |
112 | } |
140 | } |
113 | |
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; } |
114 | operator const char *() const { return s; } |
148 | operator const char *() const { return s; } |
115 | }; |
149 | }; |
116 | |
150 | |
117 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
151 | inline bool operator ==(const shstr_cmp &a, const shstr &b) |
118 | { |
152 | { |