-/* 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. */
#include <simgrid_config.h>
-#include <string>
#include <cstdarg>
+#include <cstdlib>
+#include <string>
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
-#include <stdexcept>
+#include <algorithm>
#include <cstddef>
-#include <cstdlib>
#include <cstring>
#include <iterator>
+#include <stdexcept>
#include <xbt/sysdep.h>
namespace simgrid {
namespace xbt {
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
/** POD structure representation of a string
*/
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;
~string()
{
if (string_data::data != &NUL)
- std::free(string_data::data);
+ delete[] string_data::data;
}
// Ctors
string_data::data = const_cast<char*>(&NUL);
} else {
string_data::len = size;
- string_data::data = static_cast<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';
}
}
- string() : string (nullptr, 0) {}
- string(const char* s)
- : string(s, s == nullptr ? 0 : strlen(s))
- {}
+ string() : string (const_cast<char*>(&NUL), 0) {}
+ string(const char* s) : string(s, strlen(s)) {}
string(string const& s) : string(s.c_str(), s.size()) {}
string(string&& s)
{
// 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';
}
}
// Copy
string& operator=(const char* s)
{
- assign(s, s == nullptr ? 0 : std::strlen(s));
+ assign(s, std::strlen(s));
return *this;
}
string& operator=(string const& s)
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(); }
void clear()
{
string_data::len = 0;
- string_data::data = (char*) &NUL;
+ string_data::data = const_cast<char*>(&NUL);
}
bool equals(const char* data, std::size_t len) const
template<class X>
bool operator!=(X const& that) const
{
- return !((*this) == that);
+ return not (*this == that);
}
// Compare:
#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);
+
}
}