1 /* Copyright (c) 2015-2021. 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 using size_type = std::size_t;
76 using reference = char&;
77 using const_reference = const char&;
78 using iterator = char*;
79 using const_iterator = const char*;
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(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 size_t copy(char* s, size_t len, size_t pos = 0) const
199 throw std::out_of_range(string_printf("xbt::string::copy with pos > size() (%zu > %zu)", pos, str.len));
200 size_t count = std::min(len, str.len - pos);
201 std::copy_n(str.data + pos, count, s);
205 bool equals(const char* data, std::size_t len) const
207 return this->size() == len
208 && std::memcmp(this->c_str(), data, len) == 0;
211 bool operator==(string const& that) const
213 return this->equals(that.c_str(), that.size());
215 bool operator==(std::string const& that) const
217 return this->equals(that.c_str(), that.size());
219 bool operator==(const char* that) const
221 return this->equals(that, std::strlen(that));
225 bool operator!=(X const& that) const
227 return not (*this == that);
231 int compare(const char* data, std::size_t len) const
233 size_t n = std::min(this->size(), len);
234 int res = memcmp(this->c_str(), data, n);
237 else if (this->size() == len)
239 else if (this->size() < len)
244 int compare(string const& that) const
246 return this->compare(that.c_str(), that.size());
248 int compare(std::string const& that) const
250 return this->compare(that.c_str(), that.size());
252 int compare(const char* that) const
254 return this->compare(that, std::strlen(that));
257 // Define < <= >= > in term of compare():
259 bool operator<(X const& that) const
261 return this->compare(that) < 0;
264 bool operator<=(X const& that) const
266 return this->compare(that) <= 0;
269 bool operator>(X const& that) const
271 return this->compare(that) > 0;
274 bool operator>=(X const& that) const
276 return this->compare(that) >= 0;
281 bool operator==(std::string const& a, string const& b)
286 bool operator!=(std::string const& a, string const& b)
291 bool operator<(std::string const& a, string const& b)
296 bool operator<=(std::string const& a, string const& b)
301 bool operator>(std::string const& a, string const& b)
306 bool operator>=(std::string const& a, string const& b)
313 typedef std::string string;