From 7cdb08746e0bc294dd2cd349c47a3c6cbf65a70d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 17 May 2025 09:28:41 +1200 Subject: [PATCH] fix hash - uint64_t --- src/generator.cpp | 31 ++++++++++++++++++------------- test.sh | 8 ++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/generator.cpp b/src/generator.cpp index 40d6676..59a9051 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -15,6 +15,16 @@ static std::string sanitize(const std::string& name) { return out; } + +static uint64_t fnv1a_64(const void* data, size_t len) { + const uint8_t* p = static_cast(data); + uint64_t h = 0xcbf29ce484222325ULL; + for (size_t i = 0; i < len; ++i) + h = (h ^ p[i]) * 0x100000001b3ULL; + return h; +} + + void generate_file_code(const std::string& source, const std::string& destfolder, bool silent) { fs::path src(source); fs::path dest(destfolder); @@ -26,7 +36,7 @@ void generate_file_code(const std::string& source, const std::string& destfolder std::ostringstream oss; oss << in.rdbuf(); std::string filedata = oss.str(); - std::string hash = hash_data(filedata); + uint64_t hash = fnv1a_64(filedata.data(), filedata.size()); // Write HPP std::ofstream hpp(dest / hppname); hpp << "#pragma once\n#include \nnamespace " << ns << " {\nbool recreate_file(std::string destination_folder);\n}\n"; @@ -65,16 +75,13 @@ static uint64_t fnv1a_64(const void* data, size_t len) { bool recreate_file(std::string destination_folder) { namespace fs = std::filesystem; fs::path outpath = fs::path(destination_folder) / ")cpp" << src.filename().string() << R"cpp("; - std::string existing_hash; + uint64_t existing_hash; if (fs::exists(outpath)) { std::ifstream in(outpath, std::ios::binary); std::ostringstream oss; oss << in.rdbuf(); std::string data = oss.str(); - uint64_t h = fnv1a_64(data.data(), data.size()); - std::ostringstream hex; - hex << std::hex << std::setw(16) << std::setfill('0') << h; - existing_hash = hex.str(); + existing_hash = fnv1a_64(data.data(), data.size()); } bool needs_write = !fs::exists(outpath) || existing_hash != file_hash; if (needs_write) { @@ -106,6 +113,7 @@ void walk_dir(const fs::path& dir, F&& f) { } } + void generate_folder_code(const std::string& source, const std::string& destfolder, bool silent) { fs::path src(source); fs::path dest(destfolder); @@ -181,7 +189,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) { std::ostringstream oss; oss << in.rdbuf(); std::string filedata = oss.str(); - std::string hash = hash_data(filedata); + uint64_t hash = fnv1a_64(filedata.data(), filedata.size()); std::string rel = fs::relative(file, src).string(); std::string var = sanitize(rel); cpp << "static const unsigned char data_" << var << "[] = {"; @@ -194,7 +202,7 @@ static uint64_t fnv1a_64(const void* data, size_t len) { // Reset to decimal format cpp << std::dec; cpp << "static const size_t len_" << var << " = " << filedata.size() << ";\n"; - cpp << "static const char* hash_" << var << " = \"" << hash << "\";\n"; + cpp << "static uint64_t hash_" << var << " = \"" << hash << "\";\n"; cpp << "static const char* rel_" << var << " = \"" << rel << "\";\n"; } // Write recreate_tree using heredoc style @@ -209,15 +217,12 @@ bool recreate_tree(std::string destination_folder) { cpp << R"cpp( { fs::path outpath = fs::path(destination_folder) / )cpp" << "rel_" << var << R"cpp(; fs::create_directories(outpath.parent_path()); - std::string existing_hash; + uint64_t existing_hash; if (fs::exists(outpath)) { std::ifstream in(outpath, std::ios::binary); std::ostringstream oss; oss << in.rdbuf(); std::string data = oss.str(); - uint64_t h = fnv1a_64(data.data(), data.size()); - std::ostringstream hex; - hex << std::hex << std::setw(16) << std::setfill('0') << h; - existing_hash = hex.str(); + existing_hash = fnv1a_64(data.data(), data.size()); } bool needs_write = !fs::exists(outpath) || existing_hash != )cpp" << "hash_" << var << R"cpp(; if (needs_write) { diff --git a/test.sh b/test.sh index ab2293a..fe64865 100755 --- a/test.sh +++ b/test.sh @@ -30,5 +30,13 @@ g++ -std=c++17 -I $TEST_DIR -o $TEST_DIR/testexe $TEST_DIR/main.cpp $TEST_DIR/_s $TEST_DIR/testexe +# second time should be no changes (hash should match) +$TEST_DIR/testexe + + +# third time just two files changed +rm $TEST_DIR/temp/generator.cpp +echo "whee!" >> $TEST_DIR/temp/hash.cpp + $TEST_DIR/testexe