cprover
dstring.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Container for C-Strings
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_UTIL_DSTRING_H
13 #define CPROVER_UTIL_DSTRING_H
14 
15 #include <iosfwd>
16 
17 #include "string_container.h"
18 
19 // Marked final to disable inheritance.
20 // No virtual destructor, so runtime-polymorphic use would be unsafe.
21 class dstringt final
22 {
23 public:
24  // this is safe for static objects
25  #ifdef __GNUC__
26  constexpr
27  #endif
28  dstringt():no(0)
29  {
30  }
31 
32  // this is safe for static objects
33  #ifdef __GNUC__
34  constexpr
35  #endif
37  {
38  return dstringt(no);
39  }
40 
41  #if 0
42  // This conversion allows the use of dstrings
43  // in switch ... case statements.
44  constexpr operator int() const { return no; }
45  #endif
46 
47  // this one is not safe for static objects
48  // NOLINTNEXTLINE(runtime/explicit)
49  dstringt(const char *s):no(get_string_container()[s])
50  {
51  }
52 
53  // this one is not safe for static objects
54  // NOLINTNEXTLINE(runtime/explicit)
55  dstringt(const std::string &s):no(get_string_container()[s])
56  {
57  }
58 
59  // access
60 
61  bool empty() const
62  {
63  return no==0; // string 0 is exactly the empty string
64  }
65 
66  char operator[](size_t i) const
67  {
68  return as_string()[i];
69  }
70 
71  // the pointer is guaranteed to be stable
72  const char *c_str() const
73  {
74  return as_string().c_str();
75  }
76 
77  size_t size() const
78  {
79  return as_string().size();
80  }
81 
82  // ordering -- not the same as lexicographical ordering
83 
84  bool operator< (const dstringt &b) const { return no<b.no; }
85 
86  // comparison with same type
87 
88  bool operator==(const dstringt &b) const
89  { return no==b.no; } // really fast equality testing
90 
91  bool operator!=(const dstringt &b) const
92  { return no!=b.no; } // really fast equality testing
93 
94  // comparison with other types
95 
96  bool operator==(const char *b) const { return as_string()==b; }
97  bool operator!=(const char *b) const { return as_string()!=b; }
98 
99  bool operator==(const std::string &b) const { return as_string()==b; }
100  bool operator!=(const std::string &b) const { return as_string()!=b; }
101  bool operator<(const std::string &b) const { return as_string()<b; }
102  bool operator>(const std::string &b) const { return as_string()>b; }
103  bool operator<=(const std::string &b) const { return as_string()<=b; }
104  bool operator>=(const std::string &b) const { return as_string()>=b; }
105 
106  int compare(const dstringt &b) const
107  {
108  if(no==b.no)
109  return 0; // equal
110  return as_string().compare(b.as_string());
111  }
112 
113  // modifying
114 
115  void clear()
116  { no=0; }
117 
118  void swap(dstringt &b)
119  { unsigned t=no; no=b.no; b.no=t; }
120 
122  { no=b.no; return *this; }
123 
124  // output
125 
126  std::ostream &operator<<(std::ostream &out) const
127  {
128  return out << as_string();
129  }
130 
131  // non-standard
132 
133  unsigned get_no() const
134  {
135  return no;
136  }
137 
138  size_t hash() const
139  {
140  return no;
141  }
142 
143 private:
144  #ifdef __GNUC__
145  constexpr
146  #endif
147  explicit dstringt(unsigned _no):no(_no)
148  {
149  }
150 
151  unsigned no;
152 
153  // the reference returned is guaranteed to be stable
154  const std::string &as_string() const
155  { return get_string_container().get_string(no); }
156 };
157 
158 // the reference returned is guaranteed to be stable
159 inline const std::string &as_string(const dstringt &s)
160 { return get_string_container().get_string(s.get_no()); }
161 
162 // NOLINTNEXTLINE(readability/identifiers)
164 {
165  size_t operator()(const dstringt &s) const { return s.hash(); }
166 };
167 
168 inline size_t hash_string(const dstringt &s)
169 {
170  return s.hash();
171 }
172 
173 inline std::ostream &operator<<(std::ostream &out, const dstringt &a)
174 {
175  return a.operator<<(out);
176 }
177 
178 // NOLINTNEXTLINE [allow specialisation within 'std']
179 namespace std
180 {
182 template <>
183 struct hash<dstringt> // NOLINT(readability/identifiers)
184 {
185  size_t operator()(const dstringt &dstring) const
186  {
187  return dstring.hash();
188  }
189 };
190 }
191 
192 #endif // CPROVER_UTIL_DSTRING_H
bool operator!=(const char *b) const
Definition: dstring.h:97
bool operator>(const std::string &b) const
Definition: dstring.h:102
string_containert & get_string_container()
Get a reference to the global string container.
bool operator==(const dstringt &b) const
Definition: dstring.h:88
size_t operator()(const dstringt &dstring) const
Definition: dstring.h:185
bool operator<=(const std::string &b) const
Definition: dstring.h:103
bool operator<(const std::string &b) const
Definition: dstring.h:101
dstringt & operator=(const dstringt &b)
Definition: dstring.h:121
const std::string & as_string(const dstringt &s)
Definition: dstring.h:159
STL namespace.
Container for C-Strings.
bool operator!=(const std::string &b) const
Definition: dstring.h:100
bool operator>=(const std::string &b) const
Definition: dstring.h:104
bool operator<(const dstringt &b) const
Definition: dstring.h:84
dstringt(const std::string &s)
Definition: dstring.h:55
char operator[](size_t i) const
Definition: dstring.h:66
dstringt(const char *s)
Definition: dstring.h:49
std::ostream & operator<<(std::ostream &out, const dstringt &a)
Definition: dstring.h:173
dstringt()
Definition: dstring.h:28
size_t size() const
Definition: dstring.h:77
bool operator==(const std::string &b) const
Definition: dstring.h:99
bool operator!=(const dstringt &b) const
Definition: dstring.h:91
void swap(dstringt &b)
Definition: dstring.h:118
void clear()
Definition: dstring.h:115
size_t hash_string(const dstringt &s)
Definition: dstring.h:168
std::ostream & operator<<(std::ostream &out) const
Definition: dstring.h:126
int compare(const dstringt &b) const
Definition: dstring.h:106
const std::string & as_string() const
Definition: dstring.h:154
static dstringt make_from_table_index(unsigned no)
Definition: dstring.h:36
const char * c_str() const
Definition: dstring.h:72
dstringt(unsigned _no)
Definition: dstring.h:147
bool operator==(const char *b) const
Definition: dstring.h:96
size_t operator()(const dstringt &s) const
Definition: dstring.h:165
unsigned get_no() const
Definition: dstring.h:133
bool empty() const
Definition: dstring.h:61
size_t hash() const
Definition: dstring.h:138
const std::string & get_string(size_t no) const
unsigned no
Definition: dstring.h:151