X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/6e9c859b6c65157cf6d11a777ed1ceb9224704eb..d656f9465e7b1c36319f86eeafdcec58fe9551a4:/include/xbt/string.hpp?ds=sidebyside diff --git a/include/xbt/string.hpp b/include/xbt/string.hpp index 2fcd61ae51..c0126b4da6 100644 --- a/include/xbt/string.hpp +++ b/include/xbt/string.hpp @@ -1,5 +1,4 @@ -/* Copyright (c) 2015. The SimGrid Team. - * All rights reserved. */ +/* 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. */ @@ -9,16 +8,17 @@ #include -#include #include +#include +#include -#if HAVE_MC +#if SIMGRID_HAVE_MC -#include +#include #include -#include #include #include +#include #include @@ -27,7 +27,7 @@ namespace simgrid { namespace xbt { -#if HAVE_MC +#if SIMGRID_HAVE_MC /** POD structure representation of a string */ @@ -57,7 +57,6 @@ struct string_data { XBT_PUBLIC_CLASS string : private string_data { static const char NUL; public: - // Types typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; @@ -72,7 +71,7 @@ public: ~string() { if (string_data::data != &NUL) - std::free(string_data::data); + delete[] string_data::data; } // Ctors @@ -83,8 +82,8 @@ public: string_data::data = const_cast(&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'; } } @@ -104,14 +103,14 @@ public: void assign(const char* s, size_t size) { if (string_data::data != &NUL) { - std::free(string_data::data); + delete[] string_data::data; string_data::data = nullptr; string_data::len = 0; } if (size != 0) { string_data::len = size; - string_data::data = (char*) std::malloc(string_data::len + 1); - std::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'; } } @@ -165,10 +164,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(); } @@ -208,7 +205,7 @@ public: template bool operator!=(X const& that) const { - return !((*this) == that); + return not (*this == that); } // Compare: @@ -298,9 +295,18 @@ typedef std::string string; #endif -std::string string_vprintf(const char *fmt, va_list ap); +/** Create a C++ string from a C-style format + * + * @ingroup XBT_str +*/ std::string string_printf(const char *fmt, ...); +/** Create a C++ string from a C-style format + * + * @ingroup XBT_str +*/ +std::string string_vprintf(const char *fmt, va_list ap); + } }