# include <unistd.h>
#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()
{
}
Hash&
-Hash::hash(const void* data, size_t size, HashType hash_type)
+Hash::hash(nonstd::span<const uint8_t> data, HashType hash_type)
{
- std::string_view buffer(static_cast<const char*>(data), size);
- hash_buffer(buffer);
+ hash_buffer(data);
switch (hash_type) {
case HashType::binary:
- add_debug_text(
- util::format_base16({static_cast<const uint8_t*>(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;
}
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;
}
Hash::hash_fd(int fd)
{
return util::read_fd(
- fd, [this](const void* data, size_t size) { hash(data, size); });
+ fd, [this](nonstd::span<const uint8_t> data) { hash(data); });
}
nonstd::expected<void, std::string>
}
void
-Hash::hash_buffer(std::string_view buffer)
+Hash::hash_buffer(nonstd::span<const uint8_t> buffer)
{
blake3_hasher_update(&m_hasher, buffer.data(), buffer.size());
if (!buffer.empty() && m_debug_binary) {
}
}
+void
+Hash::hash_buffer(std::string_view buffer)
+{
+ hash_buffer(util::to_span(buffer));
+}
+
void
Hash::add_debug_text(std::string_view text)
{
#include "third_party/blake3/blake3.h"
#include <third_party/nonstd/expected.hpp>
+#include <third_party/nonstd/span.hpp>
#include <array>
#include <cstdint>
// 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<const uint8_t> 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.
FILE* m_debug_binary = nullptr;
FILE* m_debug_text = nullptr;
+ void hash_buffer(nonstd::span<const uint8_t> buffer);
void hash_buffer(std::string_view buffer);
};
#include <atomic>
#include <type_traits>
+#include <vector>
// 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
key.st_size = stat.size();
Hash hash;
- hash.hash(&key, sizeof(Key));
+ hash.hash(nonstd::span<const uint8_t>(reinterpret_cast<const uint8_t*>(&key),
+ sizeof(key)));
digest = hash.digest();
return true;
}
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<const uint8_t> data) {
+ util::write_fd(fd_out, data.data(), data.size());
});
}
#include <optional>
#include <string>
#include <thread>
+#include <vector>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
if (path == "-") {
std::vector<uint8_t> 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<const uint8_t> data) {
+ output.insert(output.end(), data.begin(), data.end());
});
if (!result) {
return nonstd::make_unexpected(
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<const uint8_t> data) {
+ checksum.update(data);
});
const auto digest = checksum.digest();
PRINT(stdout, "{}\n", util::format_base16(digest));
#include <util/file.hpp>
#include <util/path.hpp>
+#include <vector>
+
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <fstream>
#include <locale>
#include <type_traits>
+#include <vector>
namespace util {
break;
}
if (n > 0) {
- data_receiver(buffer, n);
+ data_receiver({buffer, static_cast<size_t>(n)});
}
}
if (n == -1) {
// 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);
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 nonstd::span<const uint8_t>(
- reinterpret_cast<const uint8_t*>(data.data()), data.size());
+ return to_span(data.data(), data.size());
}
template<typename T>
-// 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.
//
#pragma once
-#include <cstddef>
+#include <third_party/nonstd/span.hpp>
+
#include <cstdint>
#include <functional>
-#include <vector>
namespace util {
-using DataReceiver = std::function<void(const uint8_t* data, size_t size)>;
+using DataReceiver = std::function<void(nonstd::span<const uint8_t> data)>;
} // namespace util
-// 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.
//
#include "third_party/doctest.h"
+#include <string>
+#include <vector>
+
using TestUtil::TestContext;
TEST_SUITE_BEGIN("AtomicFile");
#include "third_party/doctest.h"
+#include <string>
+#include <vector>
+
using TestUtil::TestContext;
TEST_SUITE_BEGIN("Depfile");
#include <fcntl.h>
#include <optional>
+#include <string>
+#include <vector>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#include "third_party/doctest.h"
#include <optional>
+#include <string>
+#include <vector>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
-// 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.
//
#include <third_party/doctest.h>
#include <iostream> // macOS bug: https://github.com/onqtam/doctest/issues/126
+#include <string>
+#include <vector>
using core::Statistic;
using core::Statistics;
-// 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.
//
#include <algorithm>
#include <string>
+#include <vector>
using TestUtil::TestContext;
-// 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.
//
#include "third_party/doctest.h"
#include <ostream> // https://github.com/doctest/doctest/issues/618
+#include <string>
+#include <vector>
TEST_CASE("util::Tokenizer")
{
#include <third_party/doctest.h>
#include <cstring>
+#include <string>
#include <string_view>
+#include <vector>
using TestUtil::TestContext;