1 /* Copyright (c) 2015-2018. 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 : private string_data {
62 typedef std::size_t size_type;
63 typedef char& reference;
64 typedef const char& const_reference;
65 typedef char* iterator;
66 typedef const char* const_iterator;
71 if (string_data::data != &NUL)
72 delete[] string_data::data;
76 string(const char* s, size_t size)
80 string_data::data = &NUL;
82 string_data::len = size;
83 string_data::data = new char[string_data::len + 1];
84 std::copy_n(s, string_data::len, string_data::data);
85 string_data::data[string_data::len] = '\0';
88 string() : string(&NUL, 0) {}
89 explicit string(const char* s) : string(s, strlen(s)) {}
90 string(string const& s) : string(s.c_str(), s.size()) {}
93 string_data::len = s.string_data::len;
94 string_data::data = s.string_data::data;
95 s.string_data::len = 0;
96 s.string_data::data = &NUL;
98 explicit string(std::string const& s) : string(s.c_str(), s.size()) {}
101 void assign(const char* s, size_t size)
103 if (string_data::data != &NUL) {
104 delete[] string_data::data;
105 string_data::data = nullptr;
106 string_data::len = 0;
109 string_data::len = size;
110 string_data::data = new char[string_data::len + 1];
111 std::copy_n(s, string_data::len, string_data::data);
112 string_data::data[string_data::len] = '\0';
117 string& operator=(const char* s)
119 assign(s, std::strlen(s));
122 string& operator=(string const& s)
124 assign(s.c_str(), s.size());
127 string& operator=(std::string const& s)
129 assign(s.c_str(), s.size());
134 size_t size() const { return len; }
135 size_t length() const { return len; }
136 bool empty() const { return len != 0; }
137 void shrink_to_fit() { /* Being there, but doing nothing */}
140 char* data() { return string_data::data; }
141 const char* data() const { return string_data::data; }
142 char* c_str() { return string_data::data; }
143 const char* c_str() const { return string_data::data; };
144 reference at(size_type i)
147 throw std::out_of_range("Out of range");
150 const_reference at(size_type i) const
153 throw std::out_of_range("Out of range");
156 reference operator[](size_type i)
160 const_reference operator[](size_type i) const
165 static string_data& to_string_data(string& s) { return s; }
166 operator std::string() const { return std::string(this->c_str(), this->size()); }
169 iterator begin() { return data(); }
170 iterator end() { return data() + size(); }
171 const_iterator begin() const { return data(); }
172 const_iterator end() const { return data() + size(); }
173 const_iterator cbegin() const { return data(); }
174 const_iterator cend() const { return data() + size(); }
175 // (Missing, reverse iterators)
180 string_data::len = 0;
181 string_data::data = &NUL;
184 bool equals(const char* data, std::size_t len) const
186 return this->size() == len
187 && std::memcmp(this->c_str(), data, len) == 0;
190 bool operator==(string const& that) const
192 return this->equals(that.c_str(), that.size());
194 bool operator==(std::string const& that) const
196 return this->equals(that.c_str(), that.size());
198 bool operator==(const char* that) const
200 return this->equals(that, std::strlen(that));
204 bool operator!=(X const& that) const
206 return not (*this == that);
210 int compare(const char* data, std::size_t len) const
212 size_t n = std::min(this->size(), len);
213 int res = memcmp(this->c_str(), data, n);
216 else if (this->size() == len)
218 else if (this->size() < len)
223 int compare(string const& that) const
225 return this->compare(that.c_str(), that.size());
227 int compare(std::string const& that) const
229 return this->compare(that.c_str(), that.size());
231 int compare(const char* that) const
233 return this->compare(that, std::strlen(that));
236 // Define < <= >= > in term of compare():
238 bool operator<(X const& that) const
240 return this->compare(that) < 0;
243 bool operator<=(X const& that) const
245 return this->compare(that) <= 0;
248 bool operator>(X const& that) const
250 return this->compare(that) > 0;
253 bool operator>=(X const& that) const
255 return this->compare(that) >= 0;
260 bool operator==(std::string const& a, string const& b)
265 bool operator!=(std::string const& a, string const& b)
270 bool operator<(std::string const& a, string const& b)
275 bool operator<=(std::string const& a, string const& b)
280 bool operator>(std::string const& a, string const& b)
285 bool operator>=(std::string const& a, string const& b)
292 typedef std::string string;
296 /** Create a C++ string from a C-style format
300 XBT_PUBLIC std::string string_printf(const char* fmt, ...);
302 /** Create a C++ string from a C-style format
306 XBT_PUBLIC std::string string_vprintf(const char* fmt, va_list ap);