test/fd: Split out some utility functions

Change-Id: Ic87a40a82111676988344702db60ca96ef74a0de
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/fd/line.cpp b/test/fd/line.cpp
index 2913a25..25bad75 100644
--- a/test/fd/line.cpp
+++ b/test/fd/line.cpp
@@ -1,14 +1,10 @@
-#include <sys/mman.h>
+#include "util.hpp"
 
 #include <stdplus/exception.hpp>
 #include <stdplus/fd/gmock.hpp>
 #include <stdplus/fd/line.hpp>
-#include <stdplus/fd/managed.hpp>
-#include <stdplus/fd/ops.hpp>
-#include <stdplus/raw.hpp>
-#include <stdplus/util/cexec.hpp>
 
-#include <string_view>
+#include <string>
 
 #include <gtest/gtest.h>
 
@@ -17,14 +13,6 @@
 namespace fd
 {
 
-ManagedFd makeMemfd(std::string_view contents)
-{
-    auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
-    write(fd, contents);
-    lseek(fd, 0, Whence::Set);
-    return fd;
-}
-
 TEST(LineReader, Empty)
 {
     auto fd = makeMemfd("");
@@ -70,17 +58,6 @@
 
 using testing::_;
 
-inline auto readSv(std::string_view s)
-{
-    return [s](std::span<std::byte> buf) {
-        if (s.size())
-        {
-            memcpy(buf.data(), s.data(), s.size());
-        }
-        return buf.subspan(0, s.size());
-    };
-}
-
 TEST(LineReader, Nonblock)
 {
     FdMock fd;
diff --git a/test/fd/util.hpp b/test/fd/util.hpp
new file mode 100644
index 0000000..ff34d00
--- /dev/null
+++ b/test/fd/util.hpp
@@ -0,0 +1,30 @@
+#include <sys/mman.h>
+
+#include <stdplus/fd/managed.hpp>
+#include <stdplus/fd/ops.hpp>
+#include <stdplus/util/cexec.hpp>
+
+#include <algorithm>
+#include <string_view>
+
+namespace stdplus::fd
+{
+
+inline ManagedFd makeMemfd(std::string_view contents)
+{
+    auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
+    write(fd, contents);
+    lseek(fd, 0, Whence::Set);
+    return fd;
+}
+
+constexpr auto readSv(std::string_view s) noexcept
+{
+    return [s](std::span<std::byte> buf) {
+        auto end = std::copy_n(reinterpret_cast<const std::byte*>(s.data()),
+                               std::min(buf.size(), s.size()), buf.begin());
+        return std::span(buf.begin(), end);
+    };
+}
+
+} // namespace stdplus::fd