Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / include / xbt / string.hpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_XBT_STRING_HPP
7 #define SIMGRID_XBT_STRING_HPP
8
9 #include <simgrid_config.h>
10
11 #include <cstdarg>
12 #include <cstdlib>
13 #include <string>
14
15 #if SIMGRID_HAVE_MC
16
17 #include <stdexcept>
18 #include <cstddef>
19 #include <cstring>
20 #include <iterator>
21
22 #include <xbt/sysdep.h>
23
24 #endif
25
26 namespace simgrid {
27 namespace xbt {
28
29 #if SIMGRID_HAVE_MC
30
31 /** POD structure representation of a string
32  */
33 struct string_data {
34   char* data;
35   std::size_t len;
36 };
37
38 /** A std::string-like with well-known representation
39  *
40  *  HACK, this is a (incomplete) replacement for `std::string`.
41  *  It has a fixed POD representation (`simgrid::xbt::string_data`)
42  *  which can be used to easily read the string content from another
43  *  process.
44  *
45  *  The internal representation of a `std::string` is private.
46  *  We could add some code to read this for a given implementation.
47  *  However, even if we focus on GNU libstdc++ with Itanium ABI
48  *  GNU libstdc++ currently has two different ABIs
49  *
50  *  * the pre-C++11 is a pointer to a ref-counted
51  *    string-representation (with support for COW);
52  *
53  *  * the [C++11-conforming implementation](https://gcc.gnu.org/gcc-5/changes.html)
54  *    does not use refcouting/COW but has a small string optimization.
55  */
56 XBT_PUBLIC_CLASS string : private string_data {
57   static const char NUL;
58 public:
59
60   // Types
61   typedef std::size_t size_type;
62   typedef std::ptrdiff_t difference_type;
63   typedef char& reference;
64   typedef const char& const_reference;
65   typedef char* pointer;
66   typedef const char* const_pointer;
67   typedef char* iterator;
68   typedef const char* const_iterator;
69
70   // Dtor
71   ~string()
72   {
73     if (string_data::data != &NUL)
74       std::free(string_data::data);
75   }
76
77   // Ctors
78   string(const char* s, size_t size)
79   {
80     if (size == 0) {
81       string_data::len = 0;
82       string_data::data = const_cast<char*>(&NUL);
83     } else {
84       string_data::len = size;
85       string_data::data = static_cast<char*>(std::malloc(string_data::len + 1));
86       memcpy(string_data::data, s, string_data::len);
87       string_data::data[string_data::len] = '\0';
88     }
89   }
90   string() : string (const_cast<char*>(&NUL), 0) {}
91   string(const char* s) : string(s, strlen(s)) {}
92   string(string const& s) : string(s.c_str(), s.size()) {}
93   string(string&& s)
94   {
95     string_data::len = s.string_data::len;
96     string_data::data = s.string_data::data;
97     s.string_data::len = 0;
98     s.string_data::data = const_cast<char*>(&NUL);
99   }
100   string(std::string const& s) : string(s.c_str(), s.size()) {}
101
102   // Assign
103   void assign(const char* s, size_t size)
104   {
105     if (string_data::data != &NUL) {
106       std::free(string_data::data);
107       string_data::data = nullptr;
108       string_data::len = 0;
109     }
110     if (size != 0) {
111       string_data::len = size;
112       string_data::data = (char*) std::malloc(string_data::len + 1);
113       std::memcpy(string_data::data, s, string_data::len);
114       string_data::data[string_data::len] = '\0';
115     }
116   }
117
118   // Copy
119   string& operator=(const char* s)
120   {
121     assign(s, std::strlen(s));
122     return *this;
123   }
124   string& operator=(string const& s)
125   {
126     assign(s.c_str(), s.size());
127     return *this;
128   }
129   string& operator=(std::string const& s)
130   {
131     assign(s.c_str(), s.size());
132     return *this;
133   }
134
135   // Capacity
136   size_t size() const   { return len; }
137   size_t length() const { return len; }
138   bool empty() const    { return len != 0; }
139   void shrink_to_fit() {}
140
141   // Alement access
142   char* data()              { return string_data::data; }
143   const char* data()  const { return string_data::data; }
144   char* c_str()             { return string_data::data; }
145   const char* c_str() const { return string_data::data; };
146   reference at(size_type i)
147   {
148     if (i >= size())
149       throw std::out_of_range("Out of range");
150     return data()[i];
151   }
152   const_reference at(size_type i) const
153   {
154     if (i >= size())
155       throw std::out_of_range("Out of range");
156     return data()[i];
157   }
158   reference operator[](size_type i)
159   {
160     return data()[i];
161   }
162   const_reference operator[](size_type i) const
163   {
164     return data()[i];
165   }
166   // Conversion
167   operator std::string() const { return std::string(this->c_str(), this->size()); }
168
169   // Iterators
170   iterator begin()               { return data(); }
171   iterator end()                 { return data() + size(); }
172   const_iterator begin() const   { return data(); }
173   const_iterator end() const     { return data() + size(); }
174   const_iterator cbegin() const  { return data(); }
175   const_iterator cend() const    { return data() + size(); }
176   // (Missing, reverse iterators)
177
178   // Operations
179   void clear()
180   {
181     string_data::len = 0;
182     string_data::data = const_cast<char*>(&NUL);
183   }
184
185   bool equals(const char* data, std::size_t len) const
186   {
187     return this->size() == len
188       && std::memcmp(this->c_str(), data, len) == 0;
189   }
190
191   bool operator==(string const& that) const
192   {
193     return this->equals(that.c_str(), that.size());
194   }
195   bool operator==(std::string const& that) const
196   {
197     return this->equals(that.c_str(), that.size());
198   }
199   bool operator==(const char* that) const
200   {
201     return this->equals(that, std::strlen(that));
202   }
203
204   template<class X>
205   bool operator!=(X const& that) const
206   {
207     return not (*this == that);
208   }
209
210   // Compare:
211   int compare(const char* data, std::size_t len) const
212   {
213     size_t n = std::min(this->size(), len);
214     int res = memcmp(this->c_str(), data, n);
215     if (res != 0)
216       return res;
217     else if (this->size() == len)
218       return 0;
219     else if (this->size() < len)
220       return -1;
221     else
222       return 1;
223   }
224   int compare(string const& that) const
225   {
226     return this->compare(that.c_str(), that.size());
227   }
228   int compare(std::string const& that) const
229   {
230     return this->compare(that.c_str(), that.size());
231   }
232   int compare(const char* that) const
233   {
234     return this->compare(that, std::strlen(that));
235   }
236
237   // Define < <= >= > in term of compare():
238   template<class X>
239   bool operator<(X const& that) const
240   {
241     return this->compare(that) < 0;
242   }
243   template<class X>
244   bool operator<=(X const& that) const
245   {
246     return this->compare(that) <= 0;
247   }
248   template<class X>
249   bool operator>(X const& that) const
250   {
251     return this->compare(that) > 0;
252   }
253   template<class X>
254   bool operator>=(X const& that) const
255   {
256     return this->compare(that) >= 0;
257   }
258 };
259
260 inline
261 bool operator==(std::string const& a, string const& b)
262 {
263   return b == a;
264 }
265 inline
266 bool operator!=(std::string const& a, string const& b)
267 {
268   return b != a;
269 }
270 inline
271 bool operator<(std::string const& a, string const& b)
272 {
273   return b > a;
274 }
275 inline
276 bool operator<=(std::string const& a, string const& b)
277 {
278   return b >= a;
279 }
280 inline
281 bool operator>(std::string const& a, string const& b)
282 {
283   return b < a;
284 }
285 inline
286 bool operator>=(std::string const& a, string const& b)
287 {
288   return b <= a;
289 }
290
291 #else
292
293 typedef std::string string;
294
295 #endif
296
297 /** Create a C++ string from a C-style format
298  *
299  * @ingroup XBT_str
300 */
301 std::string string_printf(const char *fmt, ...);
302
303 /** Create a C++ string from a C-style format
304  *
305  * @ingroup XBT_str
306 */
307 std::string string_vprintf(const char *fmt, va_list ap);
308
309 }
310 }
311
312 #endif