1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_XBT_STRING_HPP
7 #define SIMGRID_XBT_STRING_HPP
9 #include <simgrid/config.h>
23 #include <xbt/sysdep.h>
32 /** POD structure representation of a string
39 /** A std::string-like with well-known representation
41 * HACK, this is a (incomplete) replacement for `std::string`.
42 * It has a fixed POD representation (`simgrid::xbt::string_data`)
43 * which can be used to easily read the string content from another
46 * The internal representation of a `std::string` is private.
47 * We could add some code to read this for a given implementation.
48 * However, even if we focus on GNU libstdc++ with Itanium ABI
49 * GNU libstdc++ currently has two different ABIs
51 * * the pre-C++11 is a pointer to a ref-counted
52 * string-representation (with support for COW);
54 * * the [C++11-conforming implementation](https://gcc.gnu.org/gcc-5/changes.html)
55 * does not use refcouting/COW but has a small string optimization.
57 class XBT_PUBLIC string {
63 typedef std::size_t size_type;
64 typedef char& reference;
65 typedef const char& const_reference;
66 typedef char* iterator;
67 typedef const char* const_iterator;
77 string(const char* s, size_t size)
84 str.data = new char[str.len + 1];
85 std::copy_n(s, str.len, str.data);
86 str.data[str.len] = '\0';
89 string() : string(&NUL, 0) {}
90 explicit string(const char* s) : string(s, strlen(s)) {}
91 string(string const& s) : string(s.c_str(), s.size()) {}
95 str.data = s.str.data;
99 explicit string(std::string const& s) : string(s.c_str(), s.size()) {}
102 void assign(const char* s, size_t size)
104 if (str.data != &NUL) {
111 str.data = new char[str.len + 1];
112 std::copy_n(s, str.len, str.data);
113 str.data[str.len] = '\0';
118 string& operator=(const char* s)
120 assign(s, std::strlen(s));
123 string& operator=(string const& s)
126 assign(s.c_str(), s.size());
129 string& operator=(std::string const& s)
131 assign(s.c_str(), s.size());
136 size_t size() const { return str.len; }
137 size_t length() const { return str.len; }
138 bool empty() const { return str.len != 0; }
139 void shrink_to_fit() { /* Being there, but doing nothing */}
142 char* data() { return str.data; }
143 const char* data() const { return str.data; }
144 char* c_str() { return str.data; }
145 const char* c_str() const { return str.data; };
146 reference at(size_type i)
149 throw std::out_of_range("Out of range");
152 const_reference at(size_type i) const
155 throw std::out_of_range("Out of range");
158 reference operator[](size_type i)
162 const_reference operator[](size_type i) const
167 static string_data& to_string_data(string& s) { return s.str; }
168 operator std::string() const { return std::string(this->c_str(), this->size()); }
171 iterator begin() { return data(); }
172 iterator end() { return data() + size(); }
173 const_iterator begin() const { return data(); }
174 const_iterator end() const { return data() + size(); }
175 const_iterator cbegin() const { return data(); }
176 const_iterator cend() const { return data() + size(); }
177 // (Missing, reverse iterators)
186 bool equals(const char* data, std::size_t len) const
188 return this->size() == len
189 && std::memcmp(this->c_str(), data, len) == 0;
192 bool operator==(string const& that) const
194 return this->equals(that.c_str(), that.size());
196 bool operator==(std::string const& that) const
198 return this->equals(that.c_str(), that.size());
200 bool operator==(const char* that) const
202 return this->equals(that, std::strlen(that));
206 bool operator!=(X const& that) const
208 return not (*this == that);
212 int compare(const char* data, std::size_t len) const
214 size_t n = std::min(this->size(), len);
215 int res = memcmp(this->c_str(), data, n);
218 else if (this->size() == len)
220 else if (this->size() < len)
225 int compare(string const& that) const
227 return this->compare(that.c_str(), that.size());
229 int compare(std::string const& that) const
231 return this->compare(that.c_str(), that.size());
233 int compare(const char* that) const
235 return this->compare(that, std::strlen(that));
238 // Define < <= >= > in term of compare():
240 bool operator<(X const& that) const
242 return this->compare(that) < 0;
245 bool operator<=(X const& that) const
247 return this->compare(that) <= 0;
250 bool operator>(X const& that) const
252 return this->compare(that) > 0;
255 bool operator>=(X const& that) const
257 return this->compare(that) >= 0;
262 bool operator==(std::string const& a, string const& b)
267 bool operator!=(std::string const& a, string const& b)
272 bool operator<(std::string const& a, string const& b)
277 bool operator<=(std::string const& a, string const& b)
282 bool operator>(std::string const& a, string const& b)
287 bool operator>=(std::string const& a, string const& b)
294 typedef std::string string;
298 /** Create a C++ string from a C-style format
302 XBT_PUBLIC std::string string_printf(const char* fmt, ...);
304 /** Create a C++ string from a C-style format
308 XBT_PUBLIC std::string string_vprintf(const char* fmt, va_list ap);