mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-04 16:26:30 +02:00
Revert "Merge pull request #14 from nigels-com/merge-hex_lower"
This reverts commit5412438df5
, reversing changes made toa09963bf93
.
This commit is contained in:
@ -1,9 +1,9 @@
|
|||||||
/*
|
/*
|
||||||
Copyright (c) Marshall Clow 2011-2012.
|
Copyright (c) Marshall Clow 2011-2012.
|
||||||
|
|
||||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
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)
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
Thanks to Nevin for his comments/help.
|
Thanks to Nevin for his comments/help.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -13,7 +13,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/// \file hex.hpp
|
/// \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
|
/// characters and back. Based on the MySQL functions HEX and UNHEX
|
||||||
/// \author Marshall Clow
|
/// \author Marshall Clow
|
||||||
|
|
||||||
@ -33,17 +33,17 @@
|
|||||||
|
|
||||||
namespace boost { namespace algorithm {
|
namespace boost { namespace algorithm {
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\struct hex_decode_error
|
\struct hex_decode_error
|
||||||
\brief Base exception class for all hex decoding errors
|
\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.
|
\brief Thrown when a non-hex value (0-9, A-F) encountered when decoding.
|
||||||
Contains the offending character
|
Contains the offending character
|
||||||
*/ /*!
|
*/ /*!
|
||||||
\struct not_enough_input
|
\struct not_enough_input
|
||||||
\brief Thrown when the input sequence unexpectedly ends
|
\brief Thrown when the input sequence unexpectedly ends
|
||||||
|
|
||||||
*/
|
*/
|
||||||
struct hex_decode_error : virtual boost::exception, virtual std::exception {};
|
struct hex_decode_error : virtual boost::exception, virtual std::exception {};
|
||||||
struct not_enough_input : virtual hex_decode_error {};
|
struct not_enough_input : virtual hex_decode_error {};
|
||||||
@ -54,12 +54,12 @@ namespace detail {
|
|||||||
/// \cond DOXYGEN_HIDE
|
/// \cond DOXYGEN_HIDE
|
||||||
|
|
||||||
template <typename T, typename OutputIterator>
|
template <typename T, typename OutputIterator>
|
||||||
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 );
|
const std::size_t num_hex_digits = 2 * sizeof ( T );
|
||||||
char res [ num_hex_digits ];
|
char res [ num_hex_digits ];
|
||||||
char *p = res + num_hex_digits;
|
char *p = res + num_hex_digits;
|
||||||
for ( std::size_t i = 0; i < num_hex_digits; ++i, val >>= 4 )
|
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 );
|
return std::copy ( res, res + num_hex_digits, out );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,12 +106,12 @@ namespace detail {
|
|||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Iterator>
|
template <typename Iterator>
|
||||||
bool iter_end ( Iterator current, Iterator last ) { return current == last; }
|
bool iter_end ( Iterator current, Iterator last ) { return current == last; }
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; }
|
bool ptr_end ( const T* ptr, const T* /*end*/ ) { return *ptr == '\0'; }
|
||||||
|
|
||||||
// What can we assume here about the inputs?
|
// What can we assume here about the inputs?
|
||||||
// is std::iterator_traits<InputIterator>::value_type always 'char' ?
|
// is std::iterator_traits<InputIterator>::value_type always 'char' ?
|
||||||
// Could it be wchar_t, say? Does it matter?
|
// 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.
|
// 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 ) {
|
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 ());
|
BOOST_THROW_EXCEPTION (not_enough_input ());
|
||||||
res = ( 16 * res ) + hex_char_to_int (*first);
|
res = ( 16 * res ) + hex_char_to_int (*first);
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = res;
|
*out = res;
|
||||||
return ++out;
|
return ++out;
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ namespace detail {
|
|||||||
|
|
||||||
/// \fn hex ( InputIterator first, InputIterator last, OutputIterator out )
|
/// \fn hex ( InputIterator first, InputIterator last, OutputIterator out )
|
||||||
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
||||||
///
|
///
|
||||||
/// \param first The start of the input sequence
|
/// \param first The start of the input sequence
|
||||||
/// \param last One past the end of the input sequence
|
/// \param last One past the end of the input sequence
|
||||||
/// \param out An output iterator to the results into
|
/// \param out An output iterator to the results into
|
||||||
@ -148,31 +148,14 @@ template <typename InputIterator, typename OutputIterator>
|
|||||||
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
|
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::value_type>, OutputIterator>::type
|
||||||
hex ( InputIterator first, InputIterator last, OutputIterator out ) {
|
hex ( InputIterator first, InputIterator last, OutputIterator out ) {
|
||||||
for ( ; first != last; ++first )
|
for ( ; first != last; ++first )
|
||||||
out = detail::encode_one ( *first, out, "0123456789ABCDEF" );
|
out = detail::encode_one ( *first, out );
|
||||||
return 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 InputIterator, typename OutputIterator>
|
|
||||||
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<InputIterator>::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 )
|
/// \fn hex ( const T *ptr, OutputIterator out )
|
||||||
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
/// \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 ptr A pointer to a 0-terminated sequence of data.
|
||||||
/// \param out An output iterator to the results into
|
/// \param out An output iterator to the results into
|
||||||
/// \return The updated output iterator
|
/// \return The updated output iterator
|
||||||
@ -181,30 +164,13 @@ template <typename T, typename OutputIterator>
|
|||||||
typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
|
typename boost::enable_if<boost::is_integral<T>, OutputIterator>::type
|
||||||
hex ( const T *ptr, OutputIterator out ) {
|
hex ( const T *ptr, OutputIterator out ) {
|
||||||
while ( *ptr )
|
while ( *ptr )
|
||||||
out = detail::encode_one ( *ptr++, out, "0123456789ABCDEF" );
|
out = detail::encode_one ( *ptr++, out );
|
||||||
return 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 T, typename OutputIterator>
|
|
||||||
typename boost::enable_if<boost::is_integral<T>, 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 )
|
/// \fn hex ( const Range &r, OutputIterator out )
|
||||||
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
||||||
///
|
///
|
||||||
/// \param r The input range
|
/// \param r The input range
|
||||||
/// \param out An output iterator to the results into
|
/// \param out An output iterator to the results into
|
||||||
/// \return The updated output iterator
|
/// \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 Range, typename OutputIterator>
|
|
||||||
typename boost::enable_if<boost::is_integral<typename detail::hex_iterator_traits<typename Range::iterator>::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 )
|
/// \fn unhex ( InputIterator first, InputIterator last, OutputIterator out )
|
||||||
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
||||||
///
|
///
|
||||||
/// \param first The start of the input sequence
|
/// \param first The start of the input sequence
|
||||||
/// \param last One past the end of the input sequence
|
/// \param last One past the end of the input sequence
|
||||||
/// \param out An output iterator to the results into
|
/// \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 )
|
/// \fn unhex ( const T *ptr, OutputIterator out )
|
||||||
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
||||||
///
|
///
|
||||||
/// \param ptr A pointer to a null-terminated input sequence.
|
/// \param ptr A pointer to a null-terminated input sequence.
|
||||||
/// \param out An output iterator to the results into
|
/// \param out An output iterator to the results into
|
||||||
/// \return The updated output iterator
|
/// \return The updated output iterator
|
||||||
@ -266,7 +218,7 @@ OutputIterator unhex ( const T *ptr, OutputIterator out ) {
|
|||||||
|
|
||||||
/// \fn OutputIterator unhex ( const Range &r, OutputIterator out )
|
/// \fn OutputIterator unhex ( const Range &r, OutputIterator out )
|
||||||
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
/// \brief Converts a sequence of hexadecimal characters into a sequence of integers.
|
||||||
///
|
///
|
||||||
/// \param r The input range
|
/// \param r The input range
|
||||||
/// \param out An output iterator to the results into
|
/// \param out An output iterator to the results into
|
||||||
/// \return The updated output iterator
|
/// \return The updated output iterator
|
||||||
@ -279,7 +231,7 @@ OutputIterator unhex ( const Range &r, OutputIterator out ) {
|
|||||||
|
|
||||||
/// \fn String hex ( const String &input )
|
/// \fn String hex ( const String &input )
|
||||||
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
/// \brief Converts a sequence of integral types into a hexadecimal sequence of characters.
|
||||||
///
|
///
|
||||||
/// \param input A container to be converted
|
/// \param input A container to be converted
|
||||||
/// \return A container with the encoded text
|
/// \return A container with the encoded text
|
||||||
template<typename String>
|
template<typename String>
|
||||||
@ -290,24 +242,9 @@ String hex ( const String &input ) {
|
|||||||
return output;
|
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<typename String>
|
|
||||||
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 )
|
/// \fn String unhex ( const String &input )
|
||||||
/// \brief Converts a sequence of hexadecimal characters into a sequence of characters.
|
/// \brief Converts a sequence of hexadecimal characters into a sequence of characters.
|
||||||
///
|
///
|
||||||
/// \param input A container to be converted
|
/// \param input A container to be converted
|
||||||
/// \return A container with the decoded text
|
/// \return A container with the decoded text
|
||||||
template<typename String>
|
template<typename String>
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
#include <boost/algorithm/hex.hpp>
|
#include <boost/algorithm/hex.hpp>
|
||||||
#include <boost/algorithm/string/case_conv.hpp>
|
|
||||||
|
|
||||||
#define BOOST_TEST_MAIN
|
#define BOOST_TEST_MAIN
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
@ -43,31 +42,6 @@ void test_to_hex ( const typename String::value_type ** tests ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename String>
|
|
||||||
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<typename String>
|
template<typename String>
|
||||||
void test_from_hex_success ( const typename String::value_type ** tests ) {
|
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, std::back_inserter ( two ));
|
||||||
boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
|
boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
|
||||||
four = boost::algorithm::hex ( argh );
|
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 == two );
|
||||||
BOOST_CHECK ( one == three );
|
BOOST_CHECK ( one == three );
|
||||||
BOOST_CHECK ( one == four );
|
BOOST_CHECK ( one == four );
|
||||||
@ -144,7 +113,6 @@ const wchar_t *tohex_w [] = {
|
|||||||
const char *fromhex [] = {
|
const char *fromhex [] = {
|
||||||
"20",
|
"20",
|
||||||
"2122234556FF",
|
"2122234556FF",
|
||||||
"2122234556ff",
|
|
||||||
NULL // End of the list
|
NULL // End of the list
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -152,7 +120,6 @@ const char *fromhex [] = {
|
|||||||
const wchar_t *fromhex_w [] = {
|
const wchar_t *fromhex_w [] = {
|
||||||
L"00101020",
|
L"00101020",
|
||||||
L"2122234556FF3456",
|
L"2122234556FF3456",
|
||||||
L"2122234556ff3456",
|
|
||||||
NULL // End of the list
|
NULL // End of the list
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -162,8 +129,6 @@ const char *fromhex_fail [] = {
|
|||||||
"H",
|
"H",
|
||||||
"234",
|
"234",
|
||||||
"21222G4556FF",
|
"21222G4556FF",
|
||||||
"h",
|
|
||||||
"21222g4556ff",
|
|
||||||
NULL // End of the list
|
NULL // End of the list
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -174,8 +139,6 @@ const wchar_t *fromhex_fail_w [] = {
|
|||||||
L"H",
|
L"H",
|
||||||
L"234",
|
L"234",
|
||||||
L"21222G4556FF",
|
L"21222G4556FF",
|
||||||
L"h",
|
|
||||||
L"21222g4556ff",
|
|
||||||
NULL // End of the list
|
NULL // End of the list
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -183,12 +146,10 @@ const wchar_t *fromhex_fail_w [] = {
|
|||||||
BOOST_AUTO_TEST_CASE( test_main )
|
BOOST_AUTO_TEST_CASE( test_main )
|
||||||
{
|
{
|
||||||
test_to_hex<std::string> ( tohex );
|
test_to_hex<std::string> ( tohex );
|
||||||
test_to_hex_lower<std::string> ( tohex );
|
|
||||||
test_from_hex_success<std::string> ( fromhex );
|
test_from_hex_success<std::string> ( fromhex );
|
||||||
test_from_hex_failure<std::string> ( fromhex_fail );
|
test_from_hex_failure<std::string> ( fromhex_fail );
|
||||||
|
|
||||||
test_to_hex<std::wstring> ( tohex_w );
|
test_to_hex<std::wstring> ( tohex_w );
|
||||||
test_to_hex_lower<std::wstring> ( tohex_w );
|
|
||||||
test_from_hex_success<std::wstring> ( fromhex_w );
|
test_from_hex_success<std::wstring> ( fromhex_w );
|
||||||
test_from_hex_failure<std::wstring> ( fromhex_fail_w );
|
test_from_hex_failure<std::wstring> ( fromhex_fail_w );
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user