From d558476f41ec03ac152026d49c62f4081db50cf0 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Sun, 31 Jan 2016 19:08:45 +1000 Subject: [PATCH 1/3] Implement algorithm::hex_lower as lower-case alternative to algorithm::hex --- include/boost/algorithm/hex.hpp | 104 +++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 28 deletions(-) diff --git a/include/boost/algorithm/hex.hpp b/include/boost/algorithm/hex.hpp index 145a414..9f3763a 100644 --- a/include/boost/algorithm/hex.hpp +++ b/include/boost/algorithm/hex.hpp @@ -1,9 +1,9 @@ -/* +/* Copyright (c) Marshall Clow 2011-2012. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - + Thanks to Nevin for his comments/help. */ @@ -13,7 +13,7 @@ */ /// \file hex.hpp -/// \brief Convert sequence of integral types into a sequence of hexadecimal +/// \brief Convert sequence of integral types into a sequence of hexadecimal /// characters and back. Based on the MySQL functions HEX and UNHEX /// \author Marshall Clow @@ -33,17 +33,17 @@ namespace boost { namespace algorithm { -/*! - \struct hex_decode_error - \brief Base exception class for all hex decoding errors +/*! + \struct hex_decode_error + \brief Base exception class for all hex decoding errors */ /*! - \struct non_hex_input + \struct non_hex_input \brief Thrown when a non-hex value (0-9, A-F) encountered when decoding. Contains the offending character -*/ /*! - \struct not_enough_input +*/ /*! + \struct not_enough_input \brief Thrown when the input sequence unexpectedly ends - + */ struct hex_decode_error : virtual boost::exception, virtual std::exception {}; struct not_enough_input : virtual hex_decode_error {}; @@ -54,12 +54,12 @@ namespace detail { /// \cond DOXYGEN_HIDE template - OutputIterator encode_one ( T val, OutputIterator out ) { + OutputIterator encode_one ( T val, OutputIterator out, const char * hexDigits ) { const std::size_t num_hex_digits = 2 * sizeof ( T ); char res [ num_hex_digits ]; char *p = res + num_hex_digits; for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 ) - *--p = "0123456789ABCDEF" [ val & 0x0F ]; + *--p = hexDigits [ val & 0x0F ]; return std::copy ( res, res + num_hex_digits, out ); } @@ -106,12 +106,12 @@ namespace detail { typedef T value_type; }; - template + template bool iter_end ( Iterator current, Iterator last ) { return current == last; } - + template bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; } - + // What can we assume here about the inputs? // is std::iterator_traits::value_type always 'char' ? // Could it be wchar_t, say? Does it matter? @@ -124,11 +124,11 @@ namespace detail { // Need to make sure that we get can read that many chars here. for ( std::size_t i = 0; i < 2 * sizeof ( T ); ++i, ++first ) { - if ( pred ( first, last )) + if ( pred ( first, last )) BOOST_THROW_EXCEPTION (not_enough_input ()); res = ( 16 * res ) + hex_char_to_int (*first); } - + *out = res; return ++out; } @@ -138,7 +138,7 @@ namespace detail { /// \fn hex ( InputIterator first, InputIterator last, OutputIterator out ) /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. -/// +/// /// \param first The start of the input sequence /// \param last One past the end of the input sequence /// \param out An output iterator to the results into @@ -148,14 +148,31 @@ template typename boost::enable_if::value_type>, OutputIterator>::type hex ( InputIterator first, InputIterator last, OutputIterator out ) { for ( ; first != last; ++first ) - out = detail::encode_one ( *first, out ); + out = detail::encode_one ( *first, out, "0123456789ABCDEF" ); return out; } - + + +/// \fn hex_lower ( InputIterator first, InputIterator last, OutputIterator out ) +/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters. +/// +/// \param first The start of the input sequence +/// \param last One past the end of the input sequence +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if::value_type>, OutputIterator>::type +hex_lower ( InputIterator first, InputIterator last, OutputIterator out ) { + for ( ; first != last; ++first ) + out = detail::encode_one ( *first, out, "0123456789abcdef" ); + return out; + } + /// \fn hex ( const T *ptr, OutputIterator out ) /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. -/// +/// /// \param ptr A pointer to a 0-terminated sequence of data. /// \param out An output iterator to the results into /// \return The updated output iterator @@ -164,13 +181,30 @@ template typename boost::enable_if, OutputIterator>::type hex ( const T *ptr, OutputIterator out ) { while ( *ptr ) - out = detail::encode_one ( *ptr++, out ); + out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" ); return out; } + +/// \fn hex_lower ( const T *ptr, OutputIterator out ) +/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters. +/// +/// \param ptr A pointer to a 0-terminated sequence of data. +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if, OutputIterator>::type +hex_lower ( const T *ptr, OutputIterator out ) { + while ( *ptr ) + out = detail::encode_one ( *ptr++, out, "0123456789abcdef" ); + return out; + } + + /// \fn hex ( const Range &r, OutputIterator out ) /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. -/// +/// /// \param r The input range /// \param out An output iterator to the results into /// \return The updated output iterator @@ -182,9 +216,23 @@ hex ( const Range &r, OutputIterator out ) { } +/// \fn hex_lower ( const Range &r, OutputIterator out ) +/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters. +/// +/// \param r The input range +/// \param out An output iterator to the results into +/// \return The updated output iterator +/// \note Based on the MySQL function of the same name +template +typename boost::enable_if::value_type>, OutputIterator>::type +hex_lower ( const Range &r, OutputIterator out ) { + return hex_lower (boost::begin(r), boost::end(r), out); +} + + /// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out ) /// \brief Converts a sequence of hexadecimal characters into a sequence of integers. -/// +/// /// \param first The start of the input sequence /// \param last One past the end of the input sequence /// \param out An output iterator to the results into @@ -200,7 +248,7 @@ OutputIterator unhex ( InputIterator first, InputIterator last, OutputIterator o /// \fn unhex ( const T *ptr, OutputIterator out ) /// \brief Converts a sequence of hexadecimal characters into a sequence of integers. -/// +/// /// \param ptr A pointer to a null-terminated input sequence. /// \param out An output iterator to the results into /// \return The updated output iterator @@ -218,7 +266,7 @@ OutputIterator unhex ( const T *ptr, OutputIterator out ) { /// \fn OutputIterator unhex ( const Range &r, OutputIterator out ) /// \brief Converts a sequence of hexadecimal characters into a sequence of integers. -/// +/// /// \param r The input range /// \param out An output iterator to the results into /// \return The updated output iterator @@ -231,7 +279,7 @@ OutputIterator unhex ( const Range &r, OutputIterator out ) { /// \fn String hex ( const String &input ) /// \brief Converts a sequence of integral types into a hexadecimal sequence of characters. -/// +/// /// \param input A container to be converted /// \return A container with the encoded text template @@ -244,7 +292,7 @@ String hex ( const String &input ) { /// \fn String unhex ( const String &input ) /// \brief Converts a sequence of hexadecimal characters into a sequence of characters. -/// +/// /// \param input A container to be converted /// \return A container with the decoded text template From b7d46e65311ac35d56b87d3196b97ece7a21c6a6 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Sun, 31 Jan 2016 20:17:23 +1000 Subject: [PATCH 2/3] Another overload of algorithm::hex_lower as lower-case alternative to algorithm::hex --- include/boost/algorithm/hex.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/boost/algorithm/hex.hpp b/include/boost/algorithm/hex.hpp index 9f3763a..ad20a20 100644 --- a/include/boost/algorithm/hex.hpp +++ b/include/boost/algorithm/hex.hpp @@ -290,6 +290,21 @@ String hex ( const String &input ) { return output; } + +/// \fn String hex_lower ( const String &input ) +/// \brief Converts a sequence of integral types into a lower case hexadecimal sequence of characters. +/// +/// \param input A container to be converted +/// \return A container with the encoded text +template +String hex_lower ( const String &input ) { + String output; + output.reserve (input.size () * (2 * sizeof (typename String::value_type))); + (void) hex_lower (input, std::back_inserter (output)); + return output; + } + + /// \fn String unhex ( const String &input ) /// \brief Converts a sequence of hexadecimal characters into a sequence of characters. /// From 3cedd051fa6edb9db3b999f7fe71a8d83651753d Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Sun, 31 Jan 2016 20:18:22 +1000 Subject: [PATCH 3/3] Test coverage for algorithm::hex_lower, adapting existing coverage for algorithm::hex --- test/hex_test1.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/hex_test1.cpp b/test/hex_test1.cpp index d9f5364..31e3f9d 100644 --- a/test/hex_test1.cpp +++ b/test/hex_test1.cpp @@ -9,6 +9,7 @@ #include #include +#include #define BOOST_TEST_MAIN #include @@ -42,6 +43,31 @@ void test_to_hex ( const typename String::value_type ** tests ) { } } +template +void test_to_hex_lower ( const typename String::value_type ** tests ) { + for ( const typename String::value_type **p = tests; *p; p++ ) { + String arg, argh, one, two, three, four; + arg.assign ( *p ); + boost::algorithm::hex_lower ( *p, std::back_inserter ( one )); + boost::algorithm::hex_lower ( arg, std::back_inserter ( two )); + boost::algorithm::hex_lower ( arg.begin (), arg.end (), std::back_inserter ( three )); + four = boost::algorithm::hex_lower ( arg ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + argh = one; + one.clear (); two.clear (); three.clear (); four.clear (); + boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one )); + boost::algorithm::unhex ( argh, std::back_inserter ( two )); + boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three )); + four = boost::algorithm::unhex ( argh ); + BOOST_CHECK ( one == two ); + BOOST_CHECK ( one == three ); + BOOST_CHECK ( one == four ); + BOOST_CHECK ( one == arg ); + } + } + template void test_from_hex_success ( const typename String::value_type ** tests ) { @@ -61,6 +87,11 @@ void test_from_hex_success ( const typename String::value_type ** tests ) { boost::algorithm::hex ( argh, std::back_inserter ( two )); boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three )); four = boost::algorithm::hex ( argh ); + boost::algorithm::to_lower( arg ); + boost::algorithm::to_lower( one ); + boost::algorithm::to_lower( two ); + boost::algorithm::to_lower( three ); + boost::algorithm::to_lower( four ); BOOST_CHECK ( one == two ); BOOST_CHECK ( one == three ); BOOST_CHECK ( one == four ); @@ -113,6 +144,7 @@ const wchar_t *tohex_w [] = { const char *fromhex [] = { "20", "2122234556FF", + "2122234556ff", NULL // End of the list }; @@ -120,6 +152,7 @@ const char *fromhex [] = { const wchar_t *fromhex_w [] = { L"00101020", L"2122234556FF3456", + L"2122234556ff3456", NULL // End of the list }; @@ -129,6 +162,8 @@ const char *fromhex_fail [] = { "H", "234", "21222G4556FF", + "h", + "21222g4556ff", NULL // End of the list }; @@ -139,6 +174,8 @@ const wchar_t *fromhex_fail_w [] = { L"H", L"234", L"21222G4556FF", + L"h", + L"21222g4556ff", NULL // End of the list }; @@ -146,10 +183,12 @@ const wchar_t *fromhex_fail_w [] = { BOOST_AUTO_TEST_CASE( test_main ) { test_to_hex ( tohex ); + test_to_hex_lower ( tohex ); test_from_hex_success ( fromhex ); test_from_hex_failure ( fromhex_fail ); test_to_hex ( tohex_w ); + test_to_hex_lower ( tohex_w ); test_from_hex_success ( fromhex_w ); test_from_hex_failure ( fromhex_fail_w ); }