From ac70f76c5d05d1b6779953c8c8a3a8cc9471199e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 22 Nov 2017 17:04:48 -0800 Subject: [PATCH] Generalize ConvertBits --- src/test/convertbits_tests.cpp | 12 ++++++------ src/utilstrencodings.h | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/convertbits_tests.cpp b/src/test/convertbits_tests.cpp index 3b288dba0..a8908ebfc 100644 --- a/src/test/convertbits_tests.cpp +++ b/src/test/convertbits_tests.cpp @@ -16,8 +16,8 @@ BOOST_AUTO_TEST_CASE(convertbits_deterministic) std::vector input(32, i); std::vector data; std::vector output; - ConvertBits<8, 5, true>(data, input.begin(), input.end()); - ConvertBits<5, 8, false>(output, data.begin(), data.end()); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end()); + ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end()); BOOST_CHECK_EQUAL(data.size(), 52); BOOST_CHECK_EQUAL(output.size(), 32); BOOST_CHECK(input == output); @@ -27,8 +27,8 @@ BOOST_AUTO_TEST_CASE(convertbits_deterministic) std::vector input(43, i); std::vector data; std::vector output; - ConvertBits<8, 5, true>(data, input.begin(), input.end()); - ConvertBits<5, 8, false>(output, data.begin(), data.end()); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end()); + ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end()); BOOST_CHECK_EQUAL(data.size(), 69); BOOST_CHECK_EQUAL(output.size(), 43); BOOST_CHECK(input == output); @@ -41,8 +41,8 @@ BOOST_AUTO_TEST_CASE(convertbits_random) auto input = libzcash::random_uint256(); std::vector data; std::vector output; - ConvertBits<8, 5, true>(data, input.begin(), input.end()); - ConvertBits<5, 8, false>(output, data.begin(), data.end()); + ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, input.begin(), input.end()); + ConvertBits<5, 8, false>([&](unsigned char c) { output.push_back(c); }, data.begin(), data.end()); BOOST_CHECK_EQUAL(data.size(), 52); BOOST_CHECK_EQUAL(output.size(), 32); BOOST_CHECK(input == uint256(output)); diff --git a/src/utilstrencodings.h b/src/utilstrencodings.h index a5d0e3c8a..32c1baef6 100644 --- a/src/utilstrencodings.h +++ b/src/utilstrencodings.h @@ -135,7 +135,7 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); /** Convert from one power-of-2 number base to another. */ template -bool ConvertBits(O& out, I it, I end) { +bool ConvertBits(const O& outfn, I it, I end) { size_t acc = 0; size_t bits = 0; constexpr size_t maxv = (1 << tobits) - 1; @@ -145,12 +145,12 @@ bool ConvertBits(O& out, I it, I end) { bits += frombits; while (bits >= tobits) { bits -= tobits; - out.push_back((acc >> bits) & maxv); + outfn((acc >> bits) & maxv); } ++it; } if (pad) { - if (bits) out.push_back((acc << (tobits - bits)) & maxv); + if (bits) outfn((acc << (tobits - bits)) & maxv); } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) { return false; }