/usr/include/xbt/string.hpp is in libsimgrid-dev 3.18+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#ifndef SIMGRID_XBT_STRING_HPP
#define SIMGRID_XBT_STRING_HPP
#include <simgrid_config.h>
#include <cstdarg>
#include <cstdlib>
#include <string>
#if SIMGRID_HAVE_MC
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <stdexcept>
#include <xbt/sysdep.h>
#endif
namespace simgrid {
namespace xbt {
#if SIMGRID_HAVE_MC
/** POD structure representation of a string
*/
struct string_data {
char* data;
std::size_t len;
};
/** A std::string-like with well-known representation
*
* HACK, this is a (incomplete) replacement for `std::string`.
* It has a fixed POD representation (`simgrid::xbt::string_data`)
* which can be used to easily read the string content from another
* process.
*
* The internal representation of a `std::string` is private.
* We could add some code to read this for a given implementation.
* However, even if we focus on GNU libstdc++ with Itanium ABI
* GNU libstdc++ currently has two different ABIs
*
* * the pre-C++11 is a pointer to a ref-counted
* string-representation (with support for COW);
*
* * the [C++11-conforming implementation](https://gcc.gnu.org/gcc-5/changes.html)
* does not use refcouting/COW but has a small string optimization.
*/
XBT_PUBLIC_CLASS string : private string_data {
static char NUL;
public:
// Types
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef char& reference;
typedef const char& const_reference;
typedef char* pointer;
typedef const char* const_pointer;
typedef char* iterator;
typedef const char* const_iterator;
// Dtor
~string()
{
if (string_data::data != &NUL)
delete[] string_data::data;
}
// Ctors
string(const char* s, size_t size)
{
if (size == 0) {
string_data::len = 0;
string_data::data = &NUL;
} else {
string_data::len = size;
string_data::data = new char[string_data::len + 1];
std::copy_n(s, string_data::len, string_data::data);
string_data::data[string_data::len] = '\0';
}
}
string() : string(&NUL, 0) {}
explicit string(const char* s) : string(s, strlen(s)) {}
string(string const& s) : string(s.c_str(), s.size()) {}
string(string&& s)
{
string_data::len = s.string_data::len;
string_data::data = s.string_data::data;
s.string_data::len = 0;
s.string_data::data = &NUL;
}
explicit string(std::string const& s) : string(s.c_str(), s.size()) {}
// Assign
void assign(const char* s, size_t size)
{
if (string_data::data != &NUL) {
delete[] string_data::data;
string_data::data = nullptr;
string_data::len = 0;
}
if (size != 0) {
string_data::len = size;
string_data::data = new char[string_data::len + 1];
std::copy_n(s, string_data::len, string_data::data);
string_data::data[string_data::len] = '\0';
}
}
// Copy
string& operator=(const char* s)
{
assign(s, std::strlen(s));
return *this;
}
string& operator=(string const& s)
{
assign(s.c_str(), s.size());
return *this;
}
string& operator=(std::string const& s)
{
assign(s.c_str(), s.size());
return *this;
}
// Capacity
size_t size() const { return len; }
size_t length() const { return len; }
bool empty() const { return len != 0; }
void shrink_to_fit() { /* Being there, but doing nothing */}
// Alement access
char* data() { return string_data::data; }
const char* data() const { return string_data::data; }
char* c_str() { return string_data::data; }
const char* c_str() const { return string_data::data; };
reference at(size_type i)
{
if (i >= size())
throw std::out_of_range("Out of range");
return data()[i];
}
const_reference at(size_type i) const
{
if (i >= size())
throw std::out_of_range("Out of range");
return data()[i];
}
reference operator[](size_type i)
{
return data()[i];
}
const_reference operator[](size_type i) const
{
return data()[i];
}
// Conversion
static string_data& to_string_data(string& s) { return s; }
operator std::string() const { return std::string(this->c_str(), this->size()); }
// Iterators
iterator begin() { return data(); }
iterator end() { return data() + size(); }
const_iterator begin() const { return data(); }
const_iterator end() const { return data() + size(); }
const_iterator cbegin() const { return data(); }
const_iterator cend() const { return data() + size(); }
// (Missing, reverse iterators)
// Operations
void clear()
{
string_data::len = 0;
string_data::data = &NUL;
}
bool equals(const char* data, std::size_t len) const
{
return this->size() == len
&& std::memcmp(this->c_str(), data, len) == 0;
}
bool operator==(string const& that) const
{
return this->equals(that.c_str(), that.size());
}
bool operator==(std::string const& that) const
{
return this->equals(that.c_str(), that.size());
}
bool operator==(const char* that) const
{
return this->equals(that, std::strlen(that));
}
template<class X>
bool operator!=(X const& that) const
{
return not (*this == that);
}
// Compare:
int compare(const char* data, std::size_t len) const
{
size_t n = std::min(this->size(), len);
int res = memcmp(this->c_str(), data, n);
if (res != 0)
return res;
else if (this->size() == len)
return 0;
else if (this->size() < len)
return -1;
else
return 1;
}
int compare(string const& that) const
{
return this->compare(that.c_str(), that.size());
}
int compare(std::string const& that) const
{
return this->compare(that.c_str(), that.size());
}
int compare(const char* that) const
{
return this->compare(that, std::strlen(that));
}
// Define < <= >= > in term of compare():
template<class X>
bool operator<(X const& that) const
{
return this->compare(that) < 0;
}
template<class X>
bool operator<=(X const& that) const
{
return this->compare(that) <= 0;
}
template<class X>
bool operator>(X const& that) const
{
return this->compare(that) > 0;
}
template<class X>
bool operator>=(X const& that) const
{
return this->compare(that) >= 0;
}
};
inline
bool operator==(std::string const& a, string const& b)
{
return b == a;
}
inline
bool operator!=(std::string const& a, string const& b)
{
return b != a;
}
inline
bool operator<(std::string const& a, string const& b)
{
return b > a;
}
inline
bool operator<=(std::string const& a, string const& b)
{
return b >= a;
}
inline
bool operator>(std::string const& a, string const& b)
{
return b < a;
}
inline
bool operator>=(std::string const& a, string const& b)
{
return b <= a;
}
#else
typedef std::string string;
#endif
/** Create a C++ string from a C-style format
*
* @ingroup XBT_str
*/
XBT_PUBLIC(std::string) string_printf(const char* fmt, ...);
/** Create a C++ string from a C-style format
*
* @ingroup XBT_str
*/
XBT_PUBLIC(std::string) string_vprintf(const char* fmt, va_list ap);
}
}
#endif
|