1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_XBT_STRING_HPP
8 #define SIMGRID_XBT_STRING_HPP
10 #include <simgrid_config.h>
24 #include <xbt/sysdep.h>
33 /** POD structure representation of a string
40 /** A std::string-like with well-known representation
42 * HACK, this is a (incomplete) replacement for `std::string`.
43 * It has a fixed POD representation (`simgrid::xbt::string_data`)
44 * which can be used to easily read the string content from another
47 * The internal representation of a `std::string` is private.
48 * We could add some code to read this for a given implementation.
49 * However, even if we focus on GNU libstdc++ with Itanium ABI
50 * GNU libstdc++ currently has two different ABIs
52 * * the pre-C++11 is a pointer to a ref-counted
53 * string-representation (with support for COW);
55 * * the [C++11-conforming implementation](https://gcc.gnu.org/gcc-5/changes.html)
56 * does not use refcouting/COW but has a small string optimization.
58 XBT_PUBLIC_CLASS string : private string_data {
59 static const char NUL;
63 typedef std::size_t size_type;
64 typedef std::ptrdiff_t difference_type;
65 typedef char& reference;
66 typedef const char& const_reference;
67 typedef char* pointer;
68 typedef const char* const_pointer;
69 typedef char* iterator;
70 typedef const char* const_iterator;
75 if (string_data::data != &NUL)
76 std::free(string_data::data);
80 string(const char* s, size_t size)
84 string_data::data = const_cast<char*>(&NUL);
86 string_data::len = size;
87 string_data::data = static_cast<char*>(std::malloc(string_data::len + 1));
88 memcpy(string_data::data, s, string_data::len);
89 string_data::data[string_data::len] = '\0';
92 string() : string (const_cast<char*>(&NUL), 0) {}
93 string(const char* s) : string(s, strlen(s)) {}
94 string(string const& s) : string(s.c_str(), s.size()) {}
97 string_data::len = s.string_data::len;
98 string_data::data = s.string_data::data;
99 s.string_data::len = 0;
100 s.string_data::data = const_cast<char*>(&NUL);
102 string(std::string const& s) : string(s.c_str(), s.size()) {}
105 void assign(const char* s, size_t size)
107 if (string_data::data != &NUL) {
108 std::free(string_data::data);
109 string_data::data = nullptr;
110 string_data::len = 0;
113 string_data::len = size;
114 string_data::data = (char*) std::malloc(string_data::len + 1);
115 std::memcpy(string_data::data, s, string_data::len);
116 string_data::data[string_data::len] = '\0';
121 string& operator=(const char* s)
123 assign(s, std::strlen(s));
126 string& operator=(string const& s)
128 assign(s.c_str(), s.size());
131 string& operator=(std::string const& s)
133 assign(s.c_str(), s.size());
138 size_t size() const { return len; }
139 size_t length() const { return len; }
140 bool empty() const { return len != 0; }
141 void shrink_to_fit() {}
144 char* data() { return string_data::data; }
145 const char* data() const { return string_data::data; }
146 char* c_str() { return string_data::data; }
147 const char* c_str() const { return string_data::data; };
148 reference at(size_type i)
151 throw std::out_of_range("Out of range");
154 const_reference at(size_type i) const
157 throw std::out_of_range("Out of range");
160 reference operator[](size_type i)
164 const_reference operator[](size_type i) const
169 operator std::string() const
171 return std::string(this->c_str(), this->size());
175 iterator begin() { return data(); }
176 iterator end() { return data() + size(); }
177 const_iterator begin() const { return data(); }
178 const_iterator end() const { return data() + size(); }
179 const_iterator cbegin() const { return data(); }
180 const_iterator cend() const { return data() + size(); }
181 // (Missing, reverse iterators)
186 string_data::len = 0;
187 string_data::data = const_cast<char*>(&NUL);
190 bool equals(const char* data, std::size_t len) const
192 return this->size() == len
193 && std::memcmp(this->c_str(), data, len) == 0;
196 bool operator==(string const& that) const
198 return this->equals(that.c_str(), that.size());
200 bool operator==(std::string const& that) const
202 return this->equals(that.c_str(), that.size());
204 bool operator==(const char* that) const
206 return this->equals(that, std::strlen(that));
210 bool operator!=(X const& that) const
212 return !((*this) == that);
216 int compare(const char* data, std::size_t len) const
218 size_t n = std::min(this->size(), len);
219 int res = memcmp(this->c_str(), data, n);
222 else if (this->size() == len)
224 else if (this->size() < len)
229 int compare(string const& that) const
231 return this->compare(that.c_str(), that.size());
233 int compare(std::string const& that) const
235 return this->compare(that.c_str(), that.size());
237 int compare(const char* that) const
239 return this->compare(that, std::strlen(that));
242 // Define < <= >= > in term of compare():
244 bool operator<(X const& that) const
246 return this->compare(that) < 0;
249 bool operator<=(X const& that) const
251 return this->compare(that) <= 0;
254 bool operator>(X const& that) const
256 return this->compare(that) > 0;
259 bool operator>=(X const& that) const
261 return this->compare(that) >= 0;
266 bool operator==(std::string const& a, string const& b)
271 bool operator!=(std::string const& a, string const& b)
276 bool operator<(std::string const& a, string const& b)
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)
298 typedef std::string string;
302 /** Create a C++ string from a C-style format
306 std::string string_printf(const char *fmt, ...);
308 /** Create a C++ string from a C-style format
312 std::string string_vprintf(const char *fmt, va_list ap);