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>
23 #include <xbt/sysdep.h>
32 /** POD structure representation of a string
39 /** A std::string with well-known representation
41 * This is a (incomplete) drop-in 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 XBT_PUBLIC_CLASS string : private string_data {
58 static const char NUL;
62 typedef std::size_t size_type;
63 typedef std::ptrdiff_t difference_type;
64 typedef char& reference;
65 typedef const char& const_reference;
66 typedef char* pointer;
67 typedef const char* const_pointer;
68 typedef char* iterator;
69 typedef const char* const_iterator;
74 if (string_data::data != &NUL)
75 std::free(string_data::data);
79 string(const char* s, size_t size)
83 string_data::data = const_cast<char*>(&NUL);
85 string_data::len = size;
86 string_data::data = static_cast<char*>(std::malloc(string_data::len + 1));
87 memcpy(string_data::data, s, string_data::len);
88 string_data::data[string_data::len] = '\0';
91 string() : string (nullptr, 0) {}
93 : string(s, s == nullptr ? 0 : strlen(s))
95 string(string const& s) : string(s.c_str(), s.size()) {}
98 string_data::len = s.string_data::len;
99 string_data::data = s.string_data::data;
100 s.string_data::len = 0;
101 s.string_data::data = const_cast<char*>(&NUL);
103 string(std::string const& s) : string(s.c_str(), s.size()) {}
106 void assign(const char* s, size_t size)
108 if (string_data::data != &NUL)
109 std::free(string_data::data);
111 string_data::len = 0;
112 string_data::data = nullptr;
114 string_data::len = size;
115 string_data::data = (char*) std::malloc(string_data::len + 1);
116 memcpy(string_data::data, s, string_data::len);
117 string_data::data[string_data::len] = '\0';
122 string& operator=(const char* s)
124 assign(s, s == nullptr ? 0 : std::strlen(s));
127 string& operator=(string& s)
129 assign(s.c_str(), s.size());
132 string& operator=(std::string& s)
134 assign(s.c_str(), s.size());
139 size_t size() const { return len; }
140 size_t length() const { return len; }
141 bool empty() const { return len != 0; }
142 void shrink_to_fit() {}
145 char* data() { return string_data::data; }
146 const char* data() const { return string_data::data; }
147 char* c_str() { return string_data::data; }
148 const char* c_str() const { return string_data::data; };
149 reference at(size_type i)
152 throw std::out_of_range("Out of range");
155 const_reference at(size_type i) const
158 throw std::out_of_range("Out of range");
161 reference operator[](size_type i)
165 const_reference operator[](size_type i) const
170 operator std::string() const
172 return std::string(this->c_str(), this->size());
176 iterator begin() { return data(); }
177 iterator end() { return data() + size(); }
178 const_iterator begin() const { return data(); }
179 const_iterator end() const { return data() + size(); }
180 const_iterator cbegin() const { return data(); }
181 const_iterator cend() const { return data() + size(); }
182 // (Missing, reverse iterators)
187 string_data::len = 0;
188 string_data::data = (char*) &NUL;
192 int compare(string const& that) const
194 size_t n = std::min(this->size(), that.size());
195 int res = memcmp(this->c_str(), that.c_str(), n);
198 else if (this->size() == that.size())
200 else if (this->size() < that.size())
205 bool operator==(string const& that) const
207 return this->size() == that.size()
208 && std::memcmp(this->c_str(), that.c_str(), this->size()) == 0;
210 bool operator!=(string const& that) const
212 return !(*this == that);
214 bool operator<(string const& that) const
216 return compare(that) < 0;
218 bool operator<=(string const& that) const
220 return compare(that) <= 0;
222 bool operator>(string const& that) const
224 return compare(that) > 0;
226 bool operator>=(string const& that) const
228 return compare(that) >= 0;
231 // Compare with std::string
232 bool operator==(std::string const& that) const
234 return this->size() == that.size()
235 && std::memcmp(this->c_str(), that.c_str(), this->size()) == 0;
237 bool operator!=(std::string const& that) const
239 return !(*this == that);
241 bool operator<(std::string const& that) const
243 return compare(that) < 0;
245 bool operator<=(std::string const& that) const
247 return compare(that) <= 0;
249 bool operator>(std::string const& that) const
251 return compare(that) > 0;
253 bool operator>=(std::string const& that) const
255 return 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 std::string string_vprintf(const char *fmt, va_list ap);
297 std::string string_printf(const char *fmt, ...);