Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define xbt::string::copy().
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 29 Oct 2020 21:40:52 +0000 (22:40 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 29 Oct 2020 21:41:37 +0000 (22:41 +0100)
include/xbt/string.hpp

index bd174b8..5278929 100644 (file)
@@ -193,6 +193,15 @@ public:
     str.data = &NUL;
   }
 
+  size_t copy(char* s, size_t len, size_t pos = 0) const
+  {
+    if (pos > str.len)
+      throw std::out_of_range(string_printf("xbt::string::copy with pos > size() (%zu > %zu)", pos, str.len));
+    size_t count = std::min(len, str.len - pos);
+    std::copy_n(str.data + pos, count, s);
+    return count;
+  }
+
   bool equals(const char* data, std::size_t len) const
   {
     return this->size() == len