X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/fd769219531a772284d75320187a0ef9f8118afd..3297da9f47ce18371941b2b48a2f4018b4793ced:/include/xbt/string.hpp diff --git a/include/xbt/string.hpp b/include/xbt/string.hpp index 71f75458fc..9400d3d3bf 100644 --- a/include/xbt/string.hpp +++ b/include/xbt/string.hpp @@ -1,26 +1,34 @@ -/* Copyright (c) 2015. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2015-2018. 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 SIMGRIC_XBT_STRING_HPP -#define SIMGRIC_XBT_STRING_HPP +#ifndef SIMGRID_XBT_STRING_HPP +#define SIMGRID_XBT_STRING_HPP -#if HAVE_MC +#include -#include -#include +#include #include -#include #include + +#if SIMGRID_HAVE_MC + +#include +#include +#include #include +#include #include +#endif + namespace simgrid { namespace xbt { +#if SIMGRID_HAVE_MC + /** POD structure representation of a string */ struct string_data { @@ -28,9 +36,9 @@ struct string_data { std::size_t len; }; -/** A std::string with well-known representation +/** A std::string-like with well-known representation * - * This is a (incomplete) drop-in replacement for `std::string`. + * 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. @@ -46,17 +54,14 @@ struct string_data { * * 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 const char NUL; -public: +class XBT_PUBLIC 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; @@ -64,7 +69,7 @@ public: ~string() { if (string_data::data != &NUL) - std::free(string_data::data); + delete[] string_data::data; } // Ctors @@ -72,40 +77,38 @@ public: { if (size == 0) { string_data::len = 0; - string_data::data = const_cast(&NUL); + string_data::data = &NUL; } else { string_data::len = size; - string_data::data = static_cast(std::malloc(string_data::len + 1)); - memcpy(string_data::data, s, string_data::len); + 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 (nullptr, 0) {} - string(const char* s) - : string(s, s == nullptr ? 0 : strlen(s)) - {} + 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 = const_cast(&NUL); + s.string_data::data = &NUL; } - string(std::string const& s) : string(s.c_str(), s.size()) {} + 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) - std::free(string_data::data); - if (size == 0) { - string_data::len = 0; + if (string_data::data != &NUL) { + delete[] string_data::data; string_data::data = nullptr; - } else { + string_data::len = 0; + } + if (size != 0) { string_data::len = size; - string_data::data = (char*) std::malloc(string_data::len + 1); - memcpy(string_data::data, s, string_data::len); + 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'; } } @@ -113,15 +116,15 @@ public: // Copy string& operator=(const char* s) { - assign(s, s == nullptr ? 0 : std::strlen(s)); + assign(s, std::strlen(s)); return *this; } - string& operator=(string& s) + string& operator=(string const& s) { assign(s.c_str(), s.size()); return *this; } - string& operator=(std::string& s) + string& operator=(std::string const& s) { assign(s.c_str(), s.size()); return *this; @@ -131,7 +134,7 @@ public: size_t size() const { return len; } size_t length() const { return len; } bool empty() const { return len != 0; } - void shrink_to_fit() {} + void shrink_to_fit() { /* Being there, but doing nothing */} // Alement access char* data() { return string_data::data; } @@ -159,10 +162,8 @@ public: return data()[i]; } // Conversion - operator std::string() const - { - return std::string(this->c_str(), this->size()); - } + 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(); } @@ -177,74 +178,81 @@ public: void clear() { string_data::len = 0; - string_data::data = (char*) &NUL; + string_data::data = &NUL; } - // Compare - int compare(string const& that) const + bool equals(const char* data, std::size_t len) const { - size_t n = std::min(this->size(), that.size()); - int res = memcmp(this->c_str(), that.c_str(), n); - if (res != 0) - return res; - else if (this->size() == that.size()) - return 0; - else if (this->size() < that.size()) - return -1; - else - return 1; + return this->size() == len + && std::memcmp(this->c_str(), data, len) == 0; } + bool operator==(string const& that) const { - return this->size() == that.size() - && std::memcmp(this->c_str(), that.c_str(), this->size()) == 0; + return this->equals(that.c_str(), that.size()); } - bool operator!=(string const& that) const + bool operator==(std::string const& that) const { - return !(*this == that); + return this->equals(that.c_str(), that.size()); } - bool operator<(string const& that) const + bool operator==(const char* that) const { - return compare(that) < 0; + return this->equals(that, std::strlen(that)); } - bool operator<=(string const& that) const + + template + bool operator!=(X const& that) const { - return compare(that) <= 0; + return not (*this == that); } - bool operator>(string const& that) const + + // Compare: + int compare(const char* data, std::size_t len) const { - return compare(that) > 0; + 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; } - bool operator>=(string const& that) const + int compare(string const& that) const { - return compare(that) >= 0; + return this->compare(that.c_str(), that.size()); } - - // Compare with std::string - bool operator==(std::string const& that) const + int compare(std::string const& that) const { - return this->size() == that.size() - && std::memcmp(this->c_str(), that.c_str(), this->size()) == 0; + return this->compare(that.c_str(), that.size()); } - bool operator!=(std::string const& that) const + int compare(const char* that) const { - return !(*this == that); + return this->compare(that, std::strlen(that)); } - bool operator<(std::string const& that) const + + // Define < <= >= > in term of compare(): + template + bool operator<(X const& that) const { - return compare(that) < 0; + return this->compare(that) < 0; } - bool operator<=(std::string const& that) const + template + bool operator<=(X const& that) const { - return compare(that) <= 0; + return this->compare(that) <= 0; } - bool operator>(std::string const& that) const + template + bool operator>(X const& that) const { - return compare(that) > 0; + return this->compare(that) > 0; } - bool operator>=(std::string const& that) const + template + bool operator>=(X const& that) const { - return compare(that) >= 0; + return this->compare(that) >= 0; } }; @@ -279,21 +287,24 @@ bool operator>=(std::string const& a, string const& b) return b <= a; } -} -} - #else -#include +typedef std::string string; -namespace simgrid { -namespace xbt { +#endif -typedef std::string string; +/** 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 - -#endif