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 
33 class dstringt final
34 {
35 public:
36  // this is safe for static objects
37  #ifdef __GNUC__
38  constexpr
39  #endif
40  dstringt():no(0)
41  {
42  }
43 
44  // this is safe for static objects
45  #ifdef __GNUC__
46  constexpr
47  #endif
49  {
50  return dstringt(no);
51  }
52 
53  #if 0
54  // This conversion allows the use of dstrings
55  // in switch ... case statements.
56  constexpr operator int() const { return no; }
57  #endif
58 
59  // this one is not safe for static objects
60  // NOLINTNEXTLINE(runtime/explicit)
61  dstringt(const char *s):no(get_string_container()[s])
62  {
63  }
64 
65  // this one is not safe for static objects
66  // NOLINTNEXTLINE(runtime/explicit)
67  dstringt(const std::string &s):no(get_string_container()[s])
68  {
69  }
70 
71  // access
72 
73  bool empty() const
74  {
75  return no==0; // string 0 is exactly the empty string
76  }
77 
78  char operator[](size_t i) const
79  {
80  return as_string()[i];
81  }
82 
83  // the pointer is guaranteed to be stable
84  const char *c_str() const
85  {
86  return as_string().c_str();
87  }
88 
89  size_t size() const
90  {
91  return as_string().size();
92  }
93 
94  // ordering -- not the same as lexicographical ordering
95 
96  bool operator< (const dstringt &b) const { return no<b.no; }
97 
98  // comparison with same type
99 
100  bool operator==(const dstringt &b) const
101  { return no==b.no; } // really fast equality testing
102 
103  bool operator!=(const dstringt &b) const
104  { return no!=b.no; } // really fast equality testing
105 
106  // comparison with other types
107 
108  bool operator==(const char *b) const { return as_string()==b; }
109  bool operator!=(const char *b) const { return as_string()!=b; }
110 
111  bool operator==(const std::string &b) const { return as_string()==b; }
112  bool operator!=(const std::string &b) const { return as_string()!=b; }
113  bool operator<(const std::string &b) const { return as_string()<b; }
114  bool operator>(const std::string &b) const { return as_string()>b; }
115  bool operator<=(const std::string &b) const { return as_string()<=b; }
116  bool operator>=(const std::string &b) const { return as_string()>=b; }
117 
118  int compare(const dstringt &b) const
119  {
120  if(no==b.no)
121  return 0; // equal
122  return as_string().compare(b.as_string());
123  }
124 
125  // modifying
126 
127  void clear()
128  { no=0; }
129 
130  void swap(dstringt &b)
131  { unsigned t=no; no=b.no; b.no=t; }
132 
134  { no=b.no; return *this; }
135 
136  // output
137 
138  std::ostream &operator<<(std::ostream &out) const;
139 
140  // non-standard
141 
142  unsigned get_no() const
143  {
144  return no;
145  }
146 
147  size_t hash() const
148  {
149  return no;
150  }
151 
152 private:
153  #ifdef __GNUC__
154  constexpr
155  #endif
156  explicit dstringt(unsigned _no):no(_no)
157  {
158  }
159 
160  unsigned no;
161 
162  // the reference returned is guaranteed to be stable
163  const std::string &as_string() const
164  { return get_string_container().get_string(no); }
165 };
166 
167 // the reference returned is guaranteed to be stable
168 inline const std::string &as_string(const dstringt &s)
169 { return get_string_container().get_string(s.get_no()); }
170 
171 // NOLINTNEXTLINE(readability/identifiers)
173 {
174  size_t operator()(const dstringt &s) const { return s.hash(); }
175 };
176 
177 inline size_t hash_string(const dstringt &s)
178 {
179  return s.hash();
180 }
181 
182 inline std::ostream &operator<<(std::ostream &out, const dstringt &a)
183 {
184  return a.operator<<(out);
185 }
186 
187 // NOLINTNEXTLINE [allow specialisation within 'std']
188 namespace std
189 {
191 template <>
192 struct hash<dstringt> // NOLINT(readability/identifiers)
193 {
194  size_t operator()(const dstringt &dstring) const
195  {
196  return dstring.hash();
197  }
198 };
199 }
200 
201 #endif // CPROVER_UTIL_DSTRING_H
bool operator!=(const char *b) const
Definition: dstring.h:109
bool operator>(const std::string &b) const
Definition: dstring.h:114
string_containert & get_string_container()
Get a reference to the global string container.
bool operator==(const dstringt &b) const
Definition: dstring.h:100
size_t operator()(const dstringt &dstring) const
Definition: dstring.h:194
bool operator<=(const std::string &b) const
Definition: dstring.h:115
bool operator<(const std::string &b) const
Definition: dstring.h:113
dstringt & operator=(const dstringt &b)
Definition: dstring.h:133
const std::string & as_string(const dstringt &s)
Definition: dstring.h:168
STL namespace.
Container for C-Strings.
std::ostream & operator<<(std::ostream &out) const
Definition: dstring.cpp:16
bool operator!=(const std::string &b) const
Definition: dstring.h:112
bool operator>=(const std::string &b) const
Definition: dstring.h:116
bool operator<(const dstringt &b) const
Definition: dstring.h:96
dstringt(const std::string &s)
Definition: dstring.h:67
char operator[](size_t i) const
Definition: dstring.h:78
dstringt(const char *s)
Definition: dstring.h:61
std::ostream & operator<<(std::ostream &out, const dstringt &a)
Definition: dstring.h:182
dstringt()
Definition: dstring.h:40
size_t size() const
Definition: dstring.h:89
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
bool operator==(const std::string &b) const
Definition: dstring.h:111
bool operator!=(const dstringt &b) const
Definition: dstring.h:103
void swap(dstringt &b)
Definition: dstring.h:130
void clear()
Definition: dstring.h:127
size_t hash_string(const dstringt &s)
Definition: dstring.h:177
int compare(const dstringt &b) const
Definition: dstring.h:118
const std::string & as_string() const
Definition: dstring.h:163
static dstringt make_from_table_index(unsigned no)
Definition: dstring.h:48
const char * c_str() const
Definition: dstring.h:84
dstringt(unsigned _no)
Definition: dstring.h:156
bool operator==(const char *b) const
Definition: dstring.h:108
size_t operator()(const dstringt &s) const
Definition: dstring.h:174
unsigned get_no() const
Definition: dstring.h:142
bool empty() const
Definition: dstring.h:73
size_t hash() const
Definition: dstring.h:147
const std::string & get_string(size_t no) const
unsigned no
Definition: dstring.h:160