]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::to_string instantiations for Bytes and span
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 5 Oct 2022 18:58:01 +0000 (20:58 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 5 Oct 2022 19:18:02 +0000 (21:18 +0200)
src/util/string.hpp
unittest/test_util_string.cpp

index d9c7fe59575d1e01d109bf7c653f48792cb4b8a5..0e48bc4e31aaebddc27d92271e5b4881ec4de9f2 100644 (file)
@@ -18,6 +18,8 @@
 
 #pragma once
 
+#include <util/Bytes.hpp>
+
 #include <third_party/nonstd/expected.hpp>
 #include <third_party/nonstd/span.hpp>
 
@@ -195,6 +197,20 @@ to_string(const std::string_view& sv)
   return std::string(sv);
 }
 
+template<>
+inline std::string
+to_string(const nonstd::span<const uint8_t>& bytes)
+{
+  return std::string(to_string_view(bytes));
+}
+
+template<>
+inline std::string
+to_string(const util::Bytes& bytes)
+{
+  return std::string(to_string_view(bytes));
+}
+
 inline std::string_view
 to_string_view(nonstd::span<const uint8_t> data)
 {
index 2a45fbcd9c425f28e7dda78e027073301ab5c58b..626966359f26162cd96235dbaff8f739e72f610b 100644 (file)
@@ -289,10 +289,14 @@ TEST_CASE("util::strip_whitespace")
 
 TEST_CASE("util::to_string")
 {
+  const uint8_t bytes[] = {'f', 'o', 'o'};
   const char str[] = "foo";
 
   CHECK(util::to_string(std::string(str)) == std::string(str));
   CHECK(util::to_string(std::string_view(str)) == std::string(str));
+  CHECK(util::to_string(nonstd::span<const uint8_t>(bytes))
+        == std::string(str));
+  CHECK(util::to_string(util::Bytes(bytes, 3)) == std::string(str));
 }
 
 TEST_CASE("util::to_string_view")