Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
These lines have no utility (anymore?)
[simgrid.git] / include / xbt / string.hpp
index 6bcf4c7..912fe8d 100644 (file)
@@ -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,14 +8,14 @@
 
 #include <simgrid_config.h>
 
-#include <string>
 #include <cstdarg>
+#include <cstdlib>
+#include <string>
 
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
 
 #include <stdexcept>
 #include <cstddef>
-#include <cstdlib>
 #include <cstring>
 #include <iterator>
 
@@ -27,7 +26,7 @@
 namespace simgrid {
 namespace xbt {
 
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
 
 /** POD structure representation of a string
  */
@@ -88,10 +87,8 @@ public:
       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)
   {
@@ -105,15 +102,15 @@ public:
   // Assign
   void assign(const char* s, size_t size)
   {
-    if (string_data::data != &NUL)
+    if (string_data::data != &NUL) {
       std::free(string_data::data);
-    if (size == 0) {
-      string_data::len = 0;
       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);
+      std::memcpy(string_data::data, s, string_data::len);
       string_data::data[string_data::len] = '\0';
     }
   }
@@ -121,7 +118,7 @@ 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 const& s)
@@ -167,10 +164,7 @@ public:
     return data()[i];
   }
   // Conversion
-  operator std::string() const
-  {
-    return std::string(this->c_str(), this->size());
-  }
+  operator std::string() const { return std::string(this->c_str(), this->size()); }
 
   // Iterators
   iterator begin()               { return data(); }
@@ -185,7 +179,7 @@ public:
   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
@@ -210,7 +204,7 @@ public:
   template<class X>
   bool operator!=(X const& that) const
   {
-    return !((*this) == that);
+    return not (*this == that);
   }
 
   // Compare:
@@ -300,9 +294,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);
+
 }
 }