-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdstring.h
276 lines (220 loc) · 6.29 KB
/
dstring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*******************************************************************\
Module: Container for C-Strings
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Container for C-Strings
#ifndef CPROVER_UTIL_DSTRING_H
#define CPROVER_UTIL_DSTRING_H
#include <iosfwd>
#include <string>
#include "magic.h"
#include "string_container.h"
template <typename T>
struct diagnostics_helpert;
/// \ref dstringt has one field, an unsigned integer \ref no which is an index
/// into a static table of strings. This makes it expensive to create a new
/// string(because you have to look through the whole table to see if it is
/// already there, and add it if it isn't) but very cheap to compare strings
/// (just compare the two integers). It also means that when you have lots of
/// copies of the same string you only have to store the whole string once,
/// which saves space.
///
/// `irep_idt` is typedef-ed to \ref dstringt in irep.h.
///
///
/// Note: Marked final to disable inheritance. No virtual destructor, so
/// runtime-polymorphic use would be unsafe.
class dstringt final
{
public:
// this is safe for static objects
#ifdef __GNUC__
constexpr
#endif
dstringt():no(0)
{
}
// this is safe for static objects
#ifdef __GNUC__
constexpr
#endif
static dstringt make_from_table_index(unsigned no)
{
return dstringt(no);
}
#if 0
// This conversion allows the use of dstrings
// in switch ... case statements.
constexpr operator int() const { return no; }
#endif
// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const char *s):no(get_string_container()[s])
{
}
// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const std::string &s):no(get_string_container()[s])
{
}
dstringt(const dstringt &) = default;
/// Move constructor. There is no need and no point in actually destroying the
/// source object \p other, this is effectively just a copy constructor.
#ifdef __GNUC__
constexpr
#endif
dstringt(dstringt &&other)
: no(other.no)
{
}
// access
bool empty() const
{
return no==0; // string 0 is exactly the empty string
}
/// equivalent of as_string().starts_with(s)
bool starts_with(const char *s) const
{
for(const char *t = c_str(); *s != 0; s++, t++)
if(*t != *s)
return false;
return true;
}
/// equivalent of as_string().starts_with(s)
bool starts_with(const std::string &prefix) const
{
return as_string().compare(0, prefix.size(), prefix) == 0;
}
char operator[](size_t i) const
{
return as_string()[i];
}
// the pointer is guaranteed to be stable
const char *c_str() const
{
return as_string().c_str();
}
size_t size() const
{
return as_string().size();
}
// ordering -- not the same as lexicographical ordering
bool operator< (const dstringt &b) const { return no<b.no; }
// comparison with same type
bool operator==(const dstringt &b) const
{ return no==b.no; } // really fast equality testing
bool operator!=(const dstringt &b) const
{ return no!=b.no; } // really fast equality testing
// comparison with other types
bool operator==(const char *b) const { return as_string()==b; }
bool operator!=(const char *b) const { return as_string()!=b; }
bool operator==(const std::string &b) const { return as_string()==b; }
bool operator!=(const std::string &b) const { return as_string()!=b; }
bool operator<(const std::string &b) const { return as_string()<b; }
bool operator>(const std::string &b) const { return as_string()>b; }
bool operator<=(const std::string &b) const { return as_string()<=b; }
bool operator>=(const std::string &b) const { return as_string()>=b; }
int compare(const dstringt &b) const
{
if(no==b.no)
return 0; // equal
return as_string().compare(b.as_string());
}
// modifying
void clear()
{ no=0; }
void swap(dstringt &b)
{ unsigned t=no; no=b.no; b.no=t; }
dstringt &operator=(const dstringt &b)
{ no=b.no; return *this; }
/// Move assignment. There is no need and no point in actually destroying the
/// source object \p other, this is effectively just an assignment.
dstringt &operator=(dstringt &&other)
{
no = other.no;
return *this;
}
// output
std::ostream &operator<<(std::ostream &out) const;
// non-standard
unsigned get_no() const
{
return no;
}
size_t hash() const
{
return no;
}
// iterators for the underlying string
std::string::const_iterator begin() const
{
return as_string().begin();
}
std::string::const_iterator end() const
{
return as_string().end();
}
private:
#ifdef __GNUC__
constexpr
#endif
explicit dstringt(unsigned _no):no(_no)
{
}
unsigned no;
// the reference returned is guaranteed to be stable
const std::string &as_string() const
{ return get_string_container().get_string(no); }
};
// the reference returned is guaranteed to be stable
inline const std::string &as_string(const dstringt &s)
{ return get_string_container().get_string(s.get_no()); }
// NOLINTNEXTLINE(readability/identifiers)
struct dstring_hash
{
size_t operator()(const dstringt &s) const { return s.hash(); }
};
inline size_t hash_string(const dstringt &s)
{
return s.hash();
}
inline std::ostream &operator<<(std::ostream &out, const dstringt &a)
{
return a.operator<<(out);
}
// NOLINTNEXTLINE [allow specialisation within 'std']
namespace std
{
/// Default hash function of `dstringt` for use with STL containers.
template <>
struct hash<dstringt> // NOLINT(readability/identifiers)
{
size_t operator()(const dstringt &dstring) const
{
return dstring.hash();
}
};
}
template <>
struct diagnostics_helpert<dstringt>
{
static std::string diagnostics_as_string(const dstringt &dstring)
{
return as_string(dstring);
}
};
dstringt get_dstring_number(std::size_t);
/// equivalent to dstringt(std::to_string(value)), i.e., produces a string
/// from a number
template <typename T>
static inline
typename std::enable_if<std::is_integral<T>::value, dstringt>::type
to_dstring(T value)
{
if(value >= 0 && value <= static_cast<T>(DSTRING_NUMBERS_MAX))
return get_dstring_number(value);
else
return std::to_string(value);
}
#endif // CPROVER_UTIL_DSTRING_H