From a53822e323285a8ea4ae6bb01e7ca15780a0edb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Tue, 27 Aug 2024 10:09:54 +0200 Subject: [PATCH 1/2] hash-util: make the code work with boost-1.86 ... where `TEng::digest_type` expands to an array of chars rather than an array of ints. This caused each byte in the resulting hash string to be prepended by 3 zero bytes, which was detected by the CI on macOS: ``` --- /Users/runner/work/csdiff/csdiff/tests/csgrep/0036-csgrep-json-stdout.txt 2024-08-26 16:59:02 +++ - 2024-08-26 17:00:30 @@ -4,7 +4,7 @@ "checker": "SHELLCHECK_WARNING", "language": "shell", "tool": "shellcheck", - "hash_v1": "b6311c1fdc52c47d4279cd6650af36e6f8299960", + "hash_v1": "000000b6000000310000001c0000001f000000dc00000052000000c40000007d0000004200000079000000cd0000006600000050000000af00000036000000e6000000f8000000290000009900000060", "key_event_idx": 0, "events": [ { ``` --- src/lib/hash-util.hh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/hash-util.hh b/src/lib/hash-util.hh index 3f41fc6f..40953123 100644 --- a/src/lib/hash-util.hh +++ b/src/lib/hash-util.hh @@ -31,13 +31,15 @@ std::string hexHashStr(const TSrc &src) TEng eng; eng.process_bytes(src.data(), src.size()); - // export the hash as an array of unsigned int - typename TEng::digest_type dst; + // export the hash as an array + using TDst = typename TEng::digest_type; + TDst dst; eng.get_digest(dst); - // convert the hash to a vector of unsigned int + // convert the hash to a vector static const size_t len = sizeof(dst) / sizeof(dst[0]); - const std::vector hash(dst, dst + len); + using TElem = typename std::remove_extent::type; + const std::vector hash(dst, dst + len); // convert the hash to a hex string std::string result; From a6c08edc13805151832be0e66852a6e9e161d910 Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Tue, 27 Aug 2024 10:21:39 +0200 Subject: [PATCH 2/2] hash-util: use the `hex_lower` algorithm ... to optimize out the extra traversal step to lower the output string. Closes: https://github.com/csutils/csdiff/pull/200 --- src/lib/hash-util.hh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib/hash-util.hh b/src/lib/hash-util.hh index 40953123..157d774c 100644 --- a/src/lib/hash-util.hh +++ b/src/lib/hash-util.hh @@ -21,7 +21,6 @@ #include #include -#include /// compute TEng hash of `src` and return it as hex-encoded string template @@ -43,9 +42,6 @@ std::string hexHashStr(const TSrc &src) // convert the hash to a hex string std::string result; - boost::algorithm::hex(hash.begin(), hash.end(), back_inserter(result)); - - // convert uppercase letters to lowercase - boost::algorithm::to_lower(result); + boost::algorithm::hex_lower(hash.begin(), hash.end(), back_inserter(result)); return result; }