diff --git a/include/boost/algorithm/hex.hpp b/include/boost/algorithm/hex.hpp index ad20a20..145a414 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, const char * hexDigits ) { + OutputIterator encode_one ( T val, OutputIterator out ) { 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 = hexDigits [ val & 0x0F ]; + *--p = "0123456789ABCDEF" [ 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,31 +148,14 @@ 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, "0123456789ABCDEF" ); + out = detail::encode_one ( *first, out ); 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 @@ -181,30 +164,13 @@ template typename boost::enable_if, OutputIterator>::type hex ( const T *ptr, OutputIterator out ) { while ( *ptr ) - out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" ); + out = detail::encode_one ( *ptr++, out ); 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 @@ -216,23 +182,9 @@ 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 @@ -248,7 +200,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 @@ -266,7 +218,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 @@ -279,7 +231,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 @@ -290,24 +242,9 @@ 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. -/// +/// /// \param input A container to be converted /// \return A container with the decoded text template diff --git a/test/hex_test1.cpp b/test/hex_test1.cpp index 31e3f9d..d9f5364 100644 --- a/test/hex_test1.cpp +++ b/test/hex_test1.cpp @@ -9,7 +9,6 @@ #include #include -#include #define BOOST_TEST_MAIN #include @@ -43,31 +42,6 @@ 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 ) { @@ -87,11 +61,6 @@ 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 ); @@ -144,7 +113,6 @@ const wchar_t *tohex_w [] = { const char *fromhex [] = { "20", "2122234556FF", - "2122234556ff", NULL // End of the list }; @@ -152,7 +120,6 @@ const char *fromhex [] = { const wchar_t *fromhex_w [] = { L"00101020", L"2122234556FF3456", - L"2122234556ff3456", NULL // End of the list }; @@ -162,8 +129,6 @@ const char *fromhex_fail [] = { "H", "234", "21222G4556FF", - "h", - "21222g4556ff", NULL // End of the list }; @@ -174,8 +139,6 @@ const wchar_t *fromhex_fail_w [] = { L"H", L"234", L"21222G4556FF", - L"h", - L"21222g4556ff", NULL // End of the list }; @@ -183,12 +146,10 @@ 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 ); }