Logo AND Algorithmique Numérique Distribuée

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