]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move data type conversion utilities to util
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 7 Jul 2023 14:24:40 +0000 (16:24 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:42 +0000 (11:07 +0200)
src/util/conversion.hpp [new file with mode: 0644]
src/util/string.hpp
unittest/CMakeLists.txt
unittest/test_util_conversion.cpp [new file with mode: 0644]
unittest/test_util_string.cpp

diff --git a/src/util/conversion.hpp b/src/util/conversion.hpp
new file mode 100644 (file)
index 0000000..6592b35
--- /dev/null
@@ -0,0 +1,103 @@
+// Copyright (C) 2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <util/Bytes.hpp>
+
+#include <third_party/nonstd/span.hpp>
+
+#include <cstdint>
+#include <string>
+#include <string_view>
+
+namespace util {
+
+// --- Interface ---
+
+// Convert `data` to a `nonstd::span<const uint8_t>`.
+nonstd::span<const uint8_t> to_span(const void* data, size_t size);
+
+// Convert `value` to a `nonstd::span<const uint8_t>`.
+nonstd::span<const uint8_t> to_span(std::string_view value);
+
+// Convert `value` to a string. This function is used when joining
+// `std::string`s with `util::join`.
+template<typename T> std::string to_string(const T& value);
+
+// Convert `data` to a `std::string_view`.
+std::string_view to_string_view(nonstd::span<const uint8_t> data);
+
+// --- Inline implementations ---
+
+inline nonstd::span<const uint8_t>
+to_span(const void* data, size_t size)
+{
+  return {reinterpret_cast<const uint8_t*>(data), size};
+}
+
+inline nonstd::span<const uint8_t>
+to_span(std::string_view data)
+{
+  return to_span(data.data(), data.size());
+}
+
+template<typename T>
+inline std::string
+to_string(const T& t)
+{
+  using std::to_string;
+  return to_string(std::forward<T>(t));
+}
+
+template<>
+inline std::string
+to_string(const std::string& string)
+{
+  return std::string(string);
+}
+
+template<>
+inline std::string
+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)
+{
+  return std::string_view(reinterpret_cast<const char*>(data.data()),
+                          data.size());
+}
+
+} // namespace util
index 1ef58a139a19f9cb244a2f059de657e05119a21c..300faab852acb81b98a5be5cf008ddf9abfa84bb 100644 (file)
@@ -19,6 +19,7 @@
 #pragma once
 
 #include <util/Bytes.hpp>
+#include <util/conversion.hpp>
 
 #include <third_party/nonstd/expected.hpp>
 #include <third_party/nonstd/span.hpp>
@@ -150,19 +151,6 @@ bool starts_with(std::string_view string, std::string_view prefix);
 // Strip whitespace from left and right side of a string.
 [[nodiscard]] std::string strip_whitespace(std::string_view string);
 
-// Convert `data` to a `nonstd::span<const uint8_t>`.
-nonstd::span<const uint8_t> to_span(const void* data, size_t size);
-
-// Convert `value` to a `nonstd::span<const uint8_t>`.
-nonstd::span<const uint8_t> to_span(std::string_view value);
-
-// Convert `value` to a string. This function is used when joining
-// `std::string`s with `util::join`.
-template<typename T> std::string to_string(const T& value);
-
-// Convert `data` to a `std::string_view`.
-std::string_view to_string_view(nonstd::span<const uint8_t> data);
-
 // --- Inline implementations ---
 
 inline bool
@@ -207,59 +195,4 @@ starts_with(const std::string_view string, const std::string_view prefix)
   return string.substr(0, prefix.size()) == prefix;
 }
 
-inline nonstd::span<const uint8_t>
-to_span(const void* data, size_t size)
-{
-  return {reinterpret_cast<const uint8_t*>(data), size};
-}
-
-inline nonstd::span<const uint8_t>
-to_span(std::string_view data)
-{
-  return to_span(data.data(), data.size());
-}
-
-template<typename T>
-inline std::string
-to_string(const T& t)
-{
-  using std::to_string;
-  return to_string(std::forward<T>(t));
-}
-
-template<>
-inline std::string
-to_string(const std::string& string)
-{
-  return std::string(string);
-}
-
-template<>
-inline std::string
-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)
-{
-  return std::string_view(reinterpret_cast<const char*>(data.data()),
-                          data.size());
-}
-
 } // namespace util
index 062961f01003d02a6f39870543462ca6d8f984c3..208dd8bbb255641df3e55d582e3591bde4e337ce 100644 (file)
@@ -29,6 +29,7 @@ set(
   test_util_Tokenizer.cpp
   test_util_XXH3_128.cpp
   test_util_XXH3_64.cpp
+  test_util_conversion.cpp
   test_util_expected.cpp
   test_util_file.cpp
   test_util_path.cpp
diff --git a/unittest/test_util_conversion.cpp b/unittest/test_util_conversion.cpp
new file mode 100644 (file)
index 0000000..2daadaa
--- /dev/null
@@ -0,0 +1,48 @@
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <util/conversion.hpp>
+
+#include <third_party/doctest.h>
+
+#include <ostream> // https://github.com/doctest/doctest/issues/618
+#include <vector>
+
+TEST_SUITE_BEGIN("util");
+
+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")
+{
+  uint8_t bytes[] = {'f', 'o', 'o'};
+  char str[] = "foo";
+
+  CHECK(util::to_string_view(nonstd::span(bytes)) == std::string(str));
+}
+
+TEST_SUITE_END();
index 3710204803ebc7b9f5afab9c62f1e422bf8ca1b5..12e4822facbc9f43242a49605284b87aaad10b40 100644 (file)
@@ -522,24 +522,4 @@ TEST_CASE("util::strip_whitespace")
   CHECK(util::strip_whitespace("  x  y  ") == "x  y");
 }
 
-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")
-{
-  uint8_t bytes[] = {'f', 'o', 'o'};
-  char str[] = "foo";
-
-  CHECK(util::to_string_view(nonstd::span(bytes)) == std::string(str));
-}
-
 TEST_SUITE_END();