Refactor folly::to<>
[folly.git] / folly / FBString.h
index b5e9b2f822ec344bed31bc40087326060ff5d980..0c8273e5a19371b7f5db198e478ef45a31e53246 100644 (file)
@@ -362,7 +362,7 @@ public:
 #ifndef _LIBSTDCXX_FBSTRING
     SCOPE_EXIT {
       assert(this->size() == size);
-      assert(memcmp(this->data(), data, size * sizeof(Char)) == 0);
+      assert(size == 0 || memcmp(this->data(), data, size * sizeof(Char)) == 0);
     };
 #endif
 #endif
@@ -400,7 +400,9 @@ public:
       } else
 #endif
       {
-        fbstring_detail::pod_copy(data, data + size, small_);
+        if (size != 0) {
+          fbstring_detail::pod_copy(data, data + size, small_);
+        }
       }
       setSmallSize(size);
     } else {
@@ -1336,7 +1338,9 @@ public:
   }
 
   basic_fbstring& append(size_type n, value_type c) {
-    resize(size() + n, c);
+    Invariant checker(*this);
+    auto pData = store_.expand_noinit(n, /* expGrowth = */ true);
+    fbstring_detail::pod_fill(pData, pData + n, c);
     return *this;
   }
 
@@ -1375,7 +1379,9 @@ public:
     Invariant checker(*this);
 
     // s can alias this, we need to use pod_move.
-    if (size() >= n) {
+    if (n == 0) {
+      resize(0);
+    } else if (size() >= n) {
       fbstring_detail::pod_move(s, s + n, store_.mutable_data());
       resize(n);
       assert(size() == n);
@@ -1736,10 +1742,9 @@ public:
     enforce(pos <= size(), std::__throw_out_of_range, "");
     procrustes(n, size() - pos);
 
-    fbstring_detail::pod_copy(
-      data() + pos,
-      data() + pos + n,
-      s);
+    if (n != 0) {
+      fbstring_detail::pod_copy(data() + pos, data() + pos + n, s);
+    }
     return n;
   }