]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move format_base16/format_base32hex to util
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 7 Jul 2023 08:29:40 +0000 (10:29 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:36 +0000 (11:07 +0200)
13 files changed:
src/Digest.hpp
src/Hash.cpp
src/Util.cpp
src/Util.hpp
src/core/CacheEntry.cpp
src/core/mainoptions.cpp
src/storage/remote/HttpStorage.cpp
src/storage/remote/RedisStorage.cpp
src/util/string.cpp
src/util/string.hpp
unittest/test_Util.cpp
unittest/test_util_XXH3_128.cpp
unittest/test_util_string.cpp

index 189b537692603d66090f676a25f63c54c903d024..6ab8b9764a031c005c65677bfc7373a0663be9b8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include "Util.hpp"
+#include <util/string.hpp>
 
 #include "third_party/fmt/core.h"
 
@@ -76,9 +76,9 @@ Digest::to_string() const
   // allow for up to four uniform cache levels. The rest are encoded as
   // lowercase base32hex digits without padding characters.
   const size_t base16_bytes = 2;
-  return Util::format_base16(m_bytes, base16_bytes)
-         + Util::format_base32hex(m_bytes + base16_bytes,
-                                  size() - base16_bytes);
+  return util::format_base16({m_bytes, base16_bytes})
+         + util::format_base32hex(
+           {m_bytes + base16_bytes, size() - base16_bytes});
 }
 
 inline bool
index 442369e91b2ce52da8653bae4f5f54769ffb8138..be1c16bf416a4c218ef4028a2856ac417e54ffdb 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -24,6 +24,7 @@
 
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
+#include <util/string.hpp>
 
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -84,7 +85,7 @@ Hash::hash(const void* data, size_t size, HashType hash_type)
   switch (hash_type) {
   case HashType::binary:
     add_debug_text(
-      Util::format_base16(static_cast<const uint8_t*>(data), size));
+      util::format_base16({static_cast<const uint8_t*>(data), size}));
     break;
 
   case HashType::text:
index e00502fa541a4c4da4c1fbc775bdaa62f78556ac..3f769cf5582c7a2f13ba1609881d47d469cc5688 100644 (file)
 
 #include <limits.h> // NOLINT: PATH_MAX is defined in limits.h
 
-extern "C" {
-#include "third_party/base32hex.h"
-}
-
 #ifdef HAVE_DIRENT_H
 #  include <dirent.h>
 #endif
@@ -543,29 +539,6 @@ format_argv_for_logging(const char* const* argv)
   return result;
 }
 
-std::string
-format_base16(const uint8_t* data, size_t size)
-{
-  static const char digits[] = "0123456789abcdef";
-  std::string result;
-  result.resize(2 * size);
-  for (size_t i = 0; i < size; ++i) {
-    result[i * 2] = digits[data[i] >> 4];
-    result[i * 2 + 1] = digits[data[i] & 0xF];
-  }
-  return result;
-}
-
-std::string
-format_base32hex(const uint8_t* data, size_t size)
-{
-  const size_t bytes_to_reserve = size * 8 / 5 + 1;
-  std::string result(bytes_to_reserve, 0);
-  const size_t actual_size = base32hex(&result[0], data, size);
-  result.resize(actual_size);
-  return result;
-}
-
 void
 ensure_dir_exists(std::string_view dir)
 {
index d1a4796fc6422b394c03efb399f9928b1b743ae2..13e8bd2c257142c07f03e9ec44fc1b6351a7d958 100644 (file)
@@ -135,14 +135,6 @@ int fallocate(int fd, long new_size);
 // not intended to be machine parsable. `argv` must be terminated by a nullptr.
 std::string format_argv_for_logging(const char* const* argv);
 
-// Format a hexadecimal string representing `size` bytes of `data`. The returned
-// string will be `2 * size` long.
-std::string format_base16(const uint8_t* data, size_t size);
-
-// Format a lowercase base32hex string representing `size` bytes of `data`. No
-// padding characters will be added.
-std::string format_base32hex(const uint8_t* data, size_t size);
-
 // Return current working directory (CWD) as returned from getcwd(3) (i.e.,
 // normalized path without symlink parts). Returns the empty string on error.
 std::string get_actual_cwd();
index fb1c1e3c18dfb21ce5628da0c100d1ee0168c41c..26595edcc530b153232451c9eb55029f9119c57d 100644 (file)
@@ -226,10 +226,9 @@ CacheEntry::verify_checksum() const
   const auto actual = checksum.digest();
 
   if (actual != m_checksum) {
-    throw core::Error(
-      FMT("Incorrect checksum (actual {}, expected {})",
-          Util::format_base16(actual.data(), actual.size()),
-          Util::format_base16(m_checksum.data(), m_checksum.size())));
+    throw core::Error(FMT("Incorrect checksum (actual {}, expected {})",
+                          util::format_base16(actual),
+                          util::format_base16(m_checksum)));
   }
 }
 
index 2789876ff1d458c5ad77fd98e3e4aa1728cb591a..f9cd2cf80f610891e4e4fe726f66ab88b7eb3ef0 100644 (file)
@@ -582,8 +582,7 @@ process_main_options(int argc, const char* const* argv)
           checksum.update({data, size});
         });
         const auto digest = checksum.digest();
-        PRINT(
-          stdout, "{}\n", Util::format_base16(digest.data(), digest.size()));
+        PRINT(stdout, "{}\n", util::format_base16(digest));
       } else {
         PRINT(stderr, "Error: Failed to checksum {}\n", arg);
       }
index e055de156533db7b49d99cbe12a77f2578b9e64f..3512a436510ca4cf79c8f8e41b1490347d8ad164 100644 (file)
@@ -259,7 +259,7 @@ HttpStorageBackend::get_entry_path(const Digest& key) const
     // Mimic hex representation of a SHA256 hash value.
     const auto sha256_hex_size = 64;
     static_assert(Digest::size() == 20, "Update below if digest size changes");
-    std::string hex_digits = Util::format_base16(key.bytes(), key.size());
+    std::string hex_digits = util::format_base16({key.bytes(), key.size()});
     hex_digits.append(hex_digits.data(), sha256_hex_size - hex_digits.size());
     LOG("Translated key {} to Bazel layout ac/{}", key.to_string(), hex_digits);
     return FMT("{}ac/{}", m_url_path, hex_digits);
index 5021b6b11a88783d41dcaafcae5a68856409c0c7..34a57484bfb31d792aa8336e1a399679e879cef1 100644 (file)
@@ -26,6 +26,8 @@
 #include <util/expected.hpp>
 #include <util/string.hpp>
 
+#include <sys/time.h>
+
 // Ignore "ISO C++ forbids flexible array member ‘buf’" warning from -Wpedantic.
 #ifdef __GNUC__
 #  pragma GCC diagnostic push
index 48d4b8b0f951077671061a975380b63b432a6857..1f90934d471b8859190ba73b21e0e46d8d60620c 100644 (file)
 #include <assertions.hpp>
 #include <fmtmacros.hpp>
 
+extern "C" {
+#include <third_party/base32hex.h>
+}
+
 #include <algorithm>
 #include <cctype>
 #include <iostream>
 
 namespace util {
 
+std::string
+format_base16(nonstd::span<const uint8_t> data)
+{
+  static const char digits[] = "0123456789abcdef";
+  std::string result;
+  result.resize(2 * data.size());
+  for (size_t i = 0; i < data.size(); ++i) {
+    result[i * 2] = digits[data[i] >> 4];
+    result[i * 2 + 1] = digits[data[i] & 0xF];
+  }
+  return result;
+}
+
+std::string
+format_base32hex(nonstd::span<const uint8_t> data)
+{
+  const size_t bytes_to_reserve = data.size() * 8 / 5 + 1;
+  std::string result(bytes_to_reserve, 0);
+  const size_t actual_size = base32hex(&result[0], data.data(), data.size());
+  result.resize(actual_size);
+  return result;
+}
+
 std::string
 format_human_readable_diff(int64_t diff, SizeUnitPrefixType prefix_type)
 {
index 6e1fcf4d7906b86dcd126403e3801db87527a536..c8e17c0dceab4df57319dd5664781cd3ea835ed6 100644 (file)
@@ -41,6 +41,14 @@ enum class SizeUnitPrefixType { binary, decimal };
 // Return true if `suffix` is a suffix of `string`.
 bool ends_with(std::string_view string, std::string_view suffix);
 
+// Format a hexadecimal string representing `data`. The returned string will be
+// `2 * data.size()` long.
+std::string format_base16(nonstd::span<const uint8_t> data);
+
+// Format a lowercase base32hex string representing `data`. No padding
+// characters will be added.
+std::string format_base32hex(nonstd::span<const uint8_t> data);
+
 // Format `diff` as a human-readable string.
 std::string format_human_readable_diff(int64_t diff,
                                        SizeUnitPrefixType prefix_type);
index ae320edf3d073b64f03d19f8c61a94a9b322585b..3907d0d528576f96d41a6e667ec09cbcd9c66cf7 100644 (file)
@@ -234,30 +234,6 @@ TEST_CASE("Util::format_argv_for_logging")
   CHECK(Util::format_argv_for_logging(argv_2) == "foo bar");
 }
 
-TEST_CASE("Util::format_base16")
-{
-  uint8_t none[] = "";
-  uint8_t text[4] = "foo"; // incl. NUL
-  uint8_t data[4] = {0, 1, 2, 3};
-
-  CHECK(Util::format_base16(none, 0) == "");
-  CHECK(Util::format_base16(text, sizeof(text)) == "666f6f00");
-  CHECK(Util::format_base16(data, sizeof(data)) == "00010203");
-}
-
-TEST_CASE("Util::format_base32hex")
-{
-  // Test vectors (without padding) from RFC 4648.
-  const uint8_t input[] = {'f', 'o', 'o', 'b', 'a', 'r'};
-  CHECK(Util::format_base32hex(input, 0) == "");
-  CHECK(Util::format_base32hex(input, 1) == "co");
-  CHECK(Util::format_base32hex(input, 2) == "cpng");
-  CHECK(Util::format_base32hex(input, 3) == "cpnmu");
-  CHECK(Util::format_base32hex(input, 4) == "cpnmuog");
-  CHECK(Util::format_base32hex(input, 5) == "cpnmuoj1");
-  CHECK(Util::format_base32hex(input, 6) == "cpnmuoj1e8");
-}
-
 TEST_CASE("Util::get_extension")
 {
   CHECK(Util::get_extension("") == "");
index 23b4118804af2c2cf236a0f7dfc5b33fd0e908ac..a940069d7eabcf5c6576784c5d01b3c1d35ed58a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2011-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -27,22 +27,22 @@ TEST_CASE("util::XXH3_128")
 {
   util::XXH3_128 checksum;
   auto digest = checksum.digest();
-  CHECK(Util::format_base16(digest.data(), 16)
+  CHECK(util::format_base16({digest.data(), 16})
         == "99aa06d3014798d86001c324468d497f");
 
   checksum.update(util::to_span("foo"));
   digest = checksum.digest();
-  CHECK(Util::format_base16(digest.data(), 16)
+  CHECK(util::format_base16({digest.data(), 16})
         == "79aef92e83454121ab6e5f64077e7d8a");
 
   checksum.update(util::to_span("t"));
   digest = checksum.digest();
-  CHECK(Util::format_base16(digest.data(), 16)
+  CHECK(util::format_base16({digest.data(), 16})
         == "e6045075b5bf1ae7a3e4c87775e6c97f");
 
   checksum.reset();
   digest = checksum.digest();
-  CHECK(Util::format_base16(digest.data(), 16)
+  CHECK(util::format_base16({digest.data(), 16})
         == "99aa06d3014798d86001c324468d497f");
 }
 
index d6afeab7f5c3486cc66f453c3bde96be5f50de45..3710204803ebc7b9f5afab9c62f1e422bf8ca1b5 100644 (file)
@@ -39,6 +39,30 @@ operator==(std::pair<std::string_view, std::optional<std::string_view>> left,
 
 TEST_SUITE_BEGIN("util");
 
+TEST_CASE("util::format_base16")
+{
+  uint8_t none[] = "";
+  uint8_t text[4] = "foo"; // incl. NUL
+  uint8_t data[4] = {0, 1, 2, 3};
+
+  CHECK(util::format_base16({none, 0}) == "");
+  CHECK(util::format_base16({text, sizeof(text)}) == "666f6f00");
+  CHECK(util::format_base16({data, sizeof(data)}) == "00010203");
+}
+
+TEST_CASE("util::format_base32hex")
+{
+  // Test vectors (without padding) from RFC 4648.
+  const uint8_t input[] = {'f', 'o', 'o', 'b', 'a', 'r'};
+  CHECK(util::format_base32hex({input, 0}) == "");
+  CHECK(util::format_base32hex({input, 1}) == "co");
+  CHECK(util::format_base32hex({input, 2}) == "cpng");
+  CHECK(util::format_base32hex({input, 3}) == "cpnmu");
+  CHECK(util::format_base32hex({input, 4}) == "cpnmuog");
+  CHECK(util::format_base32hex({input, 5}) == "cpnmuoj1");
+  CHECK(util::format_base32hex({input, 6}) == "cpnmuoj1e8");
+}
+
 TEST_CASE("util::ends_with")
 {
   CHECK(util::ends_with("", ""));