From: Joel Rosdahl Date: Fri, 7 Jul 2023 13:35:33 +0000 (+0200) Subject: refactor: Use nonstd::span for data+size in more places X-Git-Tag: v4.9~147 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0de51002f3aee2688f89484ae8246ef6e6d9dc4a;p=thirdparty%2Fccache.git refactor: Use nonstd::span for data+size in more places --- diff --git a/src/Hash.cpp b/src/Hash.cpp index ca68e0eb2..63a0f76bd 100644 --- a/src/Hash.cpp +++ b/src/Hash.cpp @@ -34,7 +34,7 @@ # include #endif -const std::string_view HASH_DELIMITER("\000cCaChE\000", 8); +const uint8_t HASH_DELIMITER[] = {0, 'c', 'C', 'a', 'C', 'h', 'E', 0}; Hash::Hash() { @@ -77,19 +77,17 @@ Hash::hash_delimiter(std::string_view type) } Hash& -Hash::hash(const void* data, size_t size, HashType hash_type) +Hash::hash(nonstd::span data, HashType hash_type) { - std::string_view buffer(static_cast(data), size); - hash_buffer(buffer); + hash_buffer(data); switch (hash_type) { case HashType::binary: - add_debug_text( - util::format_base16({static_cast(data), size})); + add_debug_text(util::format_base16(data)); break; case HashType::text: - add_debug_text(buffer); + add_debug_text(util::to_string_view(data)); break; } @@ -97,10 +95,17 @@ Hash::hash(const void* data, size_t size, HashType hash_type) return *this; } +Hash& +Hash::hash(const char* data, size_t size) +{ + hash(util::to_span({data, size}), HashType::text); + return *this; +} + Hash& Hash::hash(std::string_view data) { - hash(data.data(), data.length()); + hash(util::to_span(data), HashType::text); return *this; } @@ -116,7 +121,7 @@ nonstd::expected Hash::hash_fd(int fd) { return util::read_fd( - fd, [this](const void* data, size_t size) { hash(data, size); }); + fd, [this](nonstd::span data) { hash(data); }); } nonstd::expected @@ -132,7 +137,7 @@ Hash::hash_file(const std::string& path) } void -Hash::hash_buffer(std::string_view buffer) +Hash::hash_buffer(nonstd::span buffer) { blake3_hasher_update(&m_hasher, buffer.data(), buffer.size()); if (!buffer.empty() && m_debug_binary) { @@ -140,6 +145,12 @@ Hash::hash_buffer(std::string_view buffer) } } +void +Hash::hash_buffer(std::string_view buffer) +{ + hash_buffer(util::to_span(buffer)); +} + void Hash::add_debug_text(std::string_view text) { diff --git a/src/Hash.hpp b/src/Hash.hpp index a27ed88a6..de614d1c1 100644 --- a/src/Hash.hpp +++ b/src/Hash.hpp @@ -20,6 +20,7 @@ #include "third_party/blake3/blake3.h" #include +#include #include #include @@ -67,10 +68,16 @@ public: // verbatim to the text input file. // // In both cases a newline character is added as well. - Hash& - hash(const void* data, size_t size, HashType hash_type = HashType::text); + Hash& hash(nonstd::span data, + HashType hash_type = HashType::text); - // Add a string to the hash. + // Add string data to the hash. + // + // If hash debugging is enabled, the string is written to the text input file + // followed by a newline. + Hash& hash(const char* data, size_t size); + + // Add string data to the hash. // // If hash debugging is enabled, the string is written to the text input file // followed by a newline. @@ -102,5 +109,6 @@ private: FILE* m_debug_binary = nullptr; FILE* m_debug_text = nullptr; + void hash_buffer(nonstd::span buffer); void hash_buffer(std::string_view buffer); }; diff --git a/src/InodeCache.cpp b/src/InodeCache.cpp index 3d18f37af..c4ceecd05 100644 --- a/src/InodeCache.cpp +++ b/src/InodeCache.cpp @@ -43,6 +43,7 @@ #include #include +#include // The inode cache resides on a file that is mapped into shared memory by // running processes. It is implemented as a two level structure, where the top @@ -300,7 +301,8 @@ InodeCache::hash_inode(const std::string& path, key.st_size = stat.size(); Hash hash; - hash.hash(&key, sizeof(Key)); + hash.hash(nonstd::span(reinterpret_cast(&key), + sizeof(key))); digest = hash.digest(); return true; } diff --git a/src/Util.cpp b/src/Util.cpp index 3f769cf55..ec01a5d7a 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -335,8 +335,8 @@ common_dir_prefix_length(std::string_view dir, std::string_view path) void copy_fd(int fd_in, int fd_out) { - util::read_fd(fd_in, [=](const void* data, size_t size) { - util::write_fd(fd_out, data, size); + util::read_fd(fd_in, [=](nonstd::span data) { + util::write_fd(fd_out, data.data(), data.size()); }); } diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 051b9fa16..b32afc5bb 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #ifdef HAVE_UNISTD_H # include @@ -185,8 +186,8 @@ read_from_path_or_stdin(const std::string& path) if (path == "-") { std::vector output; const auto result = - util::read_fd(STDIN_FILENO, [&](const uint8_t* data, size_t size) { - output.insert(output.end(), data, data + size); + util::read_fd(STDIN_FILENO, [&](nonstd::span data) { + output.insert(output.end(), data.begin(), data.end()); }); if (!result) { return nonstd::make_unexpected( @@ -578,8 +579,8 @@ process_main_options(int argc, const char* const* argv) util::XXH3_128 checksum; Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY)); if (fd) { - util::read_fd(*fd, [&checksum](const uint8_t* data, size_t size) { - checksum.update({data, size}); + util::read_fd(*fd, [&checksum](nonstd::span data) { + checksum.update(data); }); const auto digest = checksum.digest(); PRINT(stdout, "{}\n", util::format_base16(digest)); diff --git a/src/execute.cpp b/src/execute.cpp index ce8dca933..2b3b499a0 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -35,6 +35,8 @@ #include #include +#include + #ifdef HAVE_UNISTD_H # include #endif diff --git a/src/util/file.cpp b/src/util/file.cpp index 57a6b560f..7b80b584b 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -52,6 +52,7 @@ #include #include #include +#include namespace util { @@ -85,7 +86,7 @@ read_fd(int fd, DataReceiver data_receiver) break; } if (n > 0) { - data_receiver(buffer, n); + data_receiver({buffer, static_cast(n)}); } } if (n == -1) { diff --git a/src/util/string.hpp b/src/util/string.hpp index 32e13e0b0..1ef58a139 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -150,6 +150,9 @@ 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`. +nonstd::span to_span(const void* data, size_t size); + // Convert `value` to a `nonstd::span`. nonstd::span to_span(std::string_view value); @@ -204,11 +207,16 @@ starts_with(const std::string_view string, const std::string_view prefix) return string.substr(0, prefix.size()) == prefix; } +inline nonstd::span +to_span(const void* data, size_t size) +{ + return {reinterpret_cast(data), size}; +} + inline nonstd::span to_span(std::string_view data) { - return nonstd::span( - reinterpret_cast(data.data()), data.size()); + return to_span(data.data(), data.size()); } template diff --git a/src/util/types.hpp b/src/util/types.hpp index ba7683961..3cdc7febb 100644 --- a/src/util/types.hpp +++ b/src/util/types.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Joel Rosdahl and other contributors +// Copyright (C) 2022-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -18,13 +18,13 @@ #pragma once -#include +#include + #include #include -#include namespace util { -using DataReceiver = std::function; +using DataReceiver = std::function data)>; } // namespace util diff --git a/unittest/test_AtomicFile.cpp b/unittest/test_AtomicFile.cpp index f4005ba54..bd0c9cc33 100644 --- a/unittest/test_AtomicFile.cpp +++ b/unittest/test_AtomicFile.cpp @@ -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. // @@ -24,6 +24,9 @@ #include "third_party/doctest.h" +#include +#include + using TestUtil::TestContext; TEST_SUITE_BEGIN("AtomicFile"); diff --git a/unittest/test_Depfile.cpp b/unittest/test_Depfile.cpp index c7712b2df..1915399e4 100644 --- a/unittest/test_Depfile.cpp +++ b/unittest/test_Depfile.cpp @@ -25,6 +25,9 @@ #include "third_party/doctest.h" +#include +#include + using TestUtil::TestContext; TEST_SUITE_BEGIN("Depfile"); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 3907d0d52..a5b6b00df 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -31,6 +31,8 @@ #include #include +#include +#include #ifdef HAVE_UNISTD_H # include diff --git a/unittest/test_ccache.cpp b/unittest/test_ccache.cpp index 96c3dbb93..8e1f510e1 100644 --- a/unittest/test_ccache.cpp +++ b/unittest/test_ccache.cpp @@ -28,6 +28,8 @@ #include "third_party/doctest.h" #include +#include +#include #ifdef HAVE_UNISTD_H # include diff --git a/unittest/test_core_Statistics.cpp b/unittest/test_core_Statistics.cpp index 39262596b..8fe6d6cfe 100644 --- a/unittest/test_core_Statistics.cpp +++ b/unittest/test_core_Statistics.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2021 Joel Rosdahl and other contributors +// Copyright (C) 2011-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -24,6 +24,8 @@ #include #include // macOS bug: https://github.com/onqtam/doctest/issues/126 +#include +#include using core::Statistic; using core::Statistics; diff --git a/unittest/test_storage_local_util.cpp b/unittest/test_storage_local_util.cpp index 40b3e6f83..5c3cce2d5 100644 --- a/unittest/test_storage_local_util.cpp +++ b/unittest/test_storage_local_util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2022 Joel Rosdahl and other contributors +// Copyright (C) 2021-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -27,6 +27,7 @@ #include #include +#include using TestUtil::TestContext; diff --git a/unittest/test_util_Tokenizer.cpp b/unittest/test_util_Tokenizer.cpp index 1ea0ad4f6..d6eeabed9 100644 --- a/unittest/test_util_Tokenizer.cpp +++ b/unittest/test_util_Tokenizer.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2022 Joel Rosdahl and other contributors +// Copyright (C) 2021-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -21,6 +21,8 @@ #include "third_party/doctest.h" #include // https://github.com/doctest/doctest/issues/618 +#include +#include TEST_CASE("util::Tokenizer") { diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index 77e5c0bec..8b8683535 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -25,7 +25,9 @@ #include #include +#include #include +#include using TestUtil::TestContext;