1 /* Copyright (c) 2015-2020. 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>
30 /** Create a C++ string from a C-style format
34 XBT_PUBLIC std::string string_printf(const char* fmt, ...);
36 /** Create a C++ string from a C-style format
40 XBT_PUBLIC std::string string_vprintf(const char* fmt, va_list ap);
44 /** POD structure representation of a string
51 /** A std::string-like with well-known representation
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
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
63 * * the pre-C++11 is a pointer to a ref-counted
64 * string-representation (with support for COW);
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.
69 class XBT_PUBLIC string {
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;
89 string(const char* s, size_t size)
96 str.data = new char[str.len + 1];
97 std::copy_n(s, str.len, str.data);
98 str.data[str.len] = '\0';
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))
109 explicit string(std::string const& s) : string(s.c_str(), s.size()) {}
112 void assign(const char* s, size_t size)
114 if (str.data != &NUL) {
121 str.data = new char[str.len + 1];
122 std::copy_n(s, str.len, str.data);
123 str.data[str.len] = '\0';
128 string& operator=(const char* s)
130 assign(s, std::strlen(s));
133 string& operator=(string const& s)
136 assign(s.c_str(), s.size());
139 string& operator=(std::string const& s)
141 assign(s.c_str(), s.size());
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 */}
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)
159 throw std::out_of_range("Out of range");
162 const_reference at(size_type i) const
165 throw std::out_of_range("Out of range");
168 reference operator[](size_type i)
172 const_reference operator[](size_type i) const
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()); }
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)
196 bool equals(const char* data, std::size_t len) const
198 return this->size() == len
199 && std::memcmp(this->c_str(), data, len) == 0;
202 bool operator==(string const& that) const
204 return this->equals(that.c_str(), that.size());
206 bool operator==(std::string const& that) const
208 return this->equals(that.c_str(), that.size());
210 bool operator==(const char* that) const
212 return this->equals(that, std::strlen(that));
216 bool operator!=(X const& that) const
218 return not (*this == that);
222 int compare(const char* data, std::size_t len) const
224 size_t n = std::min(this->size(), len);
225 int res = memcmp(this->c_str(), data, n);
228 else if (this->size() == len)
230 else if (this->size() < len)
235 int compare(string const& that) const
237 return this->compare(that.c_str(), that.size());
239 int compare(std::string const& that) const
241 return this->compare(that.c_str(), that.size());
243 int compare(const char* that) const
245 return this->compare(that, std::strlen(that));
248 // Define < <= >= > in term of compare():
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;
260 bool operator>(X const& that) const
262 return this->compare(that) > 0;
265 bool operator>=(X const& that) const
267 return this->compare(that) >= 0;
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)
292 bool operator>(std::string const& a, string const& b)
297 bool operator>=(std::string const& a, string const& b)
304 typedef std::string string;