add uuid/md5_name_generator, sha1_name_generator

This commit is contained in:
bolero-MURAKAMI 2012-08-08 22:27:06 +09:00
parent f05cd35fe8
commit 3955daf23b
6 changed files with 208 additions and 88 deletions

View file

@ -12,7 +12,6 @@
#include <sprout/iterator/bytes_iterator.hpp>
#include <sprout/operation/fixed/set.hpp>
#include <sprout/bit/rotate.hpp>
#include <sprout/bit/reverse.hpp>
namespace sprout {
static_assert(CHAR_BIT == 8, "CHAR_BIT == 8");

View file

@ -3,45 +3,77 @@
#include <cstddef>
#include <climits>
#include <type_traits>
#include <sprout/config.hpp>
namespace sprout {
namespace detail {
template<typename T, typename Enable = void>
class default_big_endian_traits;
template<typename T>
class default_big_endian_traits<
T,
typename std::enable_if<std::is_integral<T>::value>::type
> {
public:
typedef T type;
public:
static SPROUT_CONSTEXPR std::size_t size() {
return sizeof(type);
}
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) {
return static_cast<unsigned char>(
(t & (UCHAR_MAX << CHAR_BIT * ((size() - 1) - i)))
>> CHAR_BIT * ((size() - 1) - i)
);
}
};
template<typename T, typename Enable = void>
class default_little_endian_traits;
template<typename T>
class default_little_endian_traits<
T,
typename std::enable_if<std::is_integral<T>::value>::type
> {
public:
typedef T type;
public:
static SPROUT_CONSTEXPR std::size_t size() {
return sizeof(type);
}
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) {
return static_cast<unsigned char>(
(t & (UCHAR_MAX << CHAR_BIT * i))
>> CHAR_BIT * i
);
}
};
} // namespace detail
//
// big_endian_traits
//
template<typename T>
class big_endian_traits {
public:
typedef T type;
public:
static SPROUT_CONSTEXPR std::size_t size() {
return sizeof(type);
}
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) {
return static_cast<unsigned char>(
(t & (UCHAR_MAX << CHAR_BIT * ((size() - 1) - i)))
>> CHAR_BIT * ((size() - 1) - i)
);
}
};
class big_endian_traits
: public sprout::detail::default_big_endian_traits<T>
{};
//
// little_endian_traits
//
template<typename T>
class little_endian_traits {
public:
typedef T type;
public:
static SPROUT_CONSTEXPR std::size_t size() {
return sizeof(type);
}
static SPROUT_CONSTEXPR unsigned char get_byte(type const& t, std::size_t i) {
return static_cast<unsigned char>(
(t & (UCHAR_MAX << CHAR_BIT * i))
>> CHAR_BIT * i
);
}
};
class little_endian_traits
: public sprout::detail::default_little_endian_traits<T>
{};
//
// endian_traits
//
template<typename T>
class endian_traits
: public sprout::big_endian_traits<T>
{};
} // namespace sprout
#endif // #ifndef SPROUT_ENDIAN_TRAITS_HPP

View file

@ -15,7 +15,7 @@ namespace sprout {
//
template<
typename Iterator,
typename Traits = sprout::big_endian_traits<typename std::iterator_traits<Iterator>::value_type>
typename Traits = sprout::endian_traits<typename std::iterator_traits<Iterator>::value_type>
>
class bytes_iterator
: public std::iterator<

View file

@ -0,0 +1,72 @@
#ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP
#define SPROUT_UUID_MD5_NAME_GENERATOR_HPP
#include <sprout/config.hpp>
#include <sprout/string.hpp>
#include <sprout/uuid/uuid.hpp>
#include <sprout/checksum/md5.hpp>
namespace sprout {
namespace uuids {
//
// md5_name_generator
//
class md5_name_generator {
public:
typedef sprout::uuids::uuid result_type;
private:
typedef typename result_type::value_type value_type;
private:
sprout::md5 sum_;
private:
SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::md5::value_type const& value) const {
return result_type{{
value[0],
value[1],
value[2],
value[3],
value[4],
value[5],
static_cast<value_type>((value[6] & 0x5F) | 0x50),
value[7],
static_cast<value_type>((value[8] & 0xBF) | 0x80),
value[9],
value[10],
value[11],
value[12],
value[13],
value[14],
value[15]
}};
}
SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::md5 const& sha) const {
return sha_to_uuid_1(sha.checksum());
}
public:
SPROUT_CONSTEXPR md5_name_generator()
: sum_(sprout::md5::const_type().process_range(sprout::uuids::uuid{{0}}))
{}
explicit SPROUT_CONSTEXPR md5_name_generator(sprout::uuids::uuid const& namespace_uuid)
: sum_(sprout::md5::const_type().process_range(namespace_uuid))
{}
template<typename Elem, std::size_t N, typename Traits>
SPROUT_CONSTEXPR result_type operator()(sprout::basic_string<Elem, N, Traits> const& name) const {
return sha_to_uuid(sum_.process_range(name));
}
SPROUT_CONSTEXPR result_type operator()(char const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<wchar_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char16_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char32_t>::length(name)));
}
};
} // namespace uuids
} // namespace sprout
#endif // #ifndef SPROUT_UUID_MD5_NAME_GENERATOR_HPP

View file

@ -2,70 +2,15 @@
#define SPROUT_UUID_NAME_GENERATOR_HPP
#include <sprout/config.hpp>
#include <sprout/string.hpp>
#include <sprout/uuid/uuid.hpp>
#include <sprout/checksum/sha1.hpp>
#include <sprout/uuid/md5_name_generator.hpp>
#include <sprout/uuid/sha1_name_generator.hpp>
namespace sprout {
namespace uuids {
//
// name_generator
//
class name_generator {
public:
typedef sprout::uuids::uuid result_type;
private:
typedef typename result_type::value_type value_type;
private:
sprout::sha1 sha_;
private:
SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::sha1::value_type const& value) const {
return result_type{{
value[0],
value[1],
value[2],
value[3],
value[4],
value[5],
static_cast<value_type>((value[6] & 0x5F) | 0x50),
value[7],
static_cast<value_type>((value[8] & 0xBF) | 0x80),
value[9],
value[10],
value[11],
value[12],
value[13],
value[14],
value[15]
}};
}
SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::sha1 const& sha) const {
return sha_to_uuid_1(sha.checksum());
}
public:
SPROUT_CONSTEXPR name_generator()
: sha_(sprout::sha1::const_type().process_range(sprout::uuids::uuid{{0}}))
{}
explicit SPROUT_CONSTEXPR name_generator(sprout::uuids::uuid const& namespace_uuid)
: sha_(sprout::sha1::const_type().process_range(namespace_uuid))
{}
template<typename Elem, std::size_t N, typename Traits>
SPROUT_CONSTEXPR result_type operator()(sprout::basic_string<Elem, N, Traits> const& name) const {
return sha_to_uuid(sha_.process_range(name));
}
SPROUT_CONSTEXPR result_type operator()(char const* name) const {
return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits<char>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const {
return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits<wchar_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const {
return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits<char16_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const {
return sha_to_uuid(sha_.process_bytes(name, sprout::char_traits<char32_t>::length(name)));
}
};
typedef sprout::uuids::sha1_name_generator name_generator;
} // namespace uuids
} // namespace sprout

View file

@ -0,0 +1,72 @@
#ifndef SPROUT_UUID_SHA1_NAME_GENERATOR_HPP
#define SPROUT_UUID_SHA1_NAME_GENERATOR_HPP
#include <sprout/config.hpp>
#include <sprout/string.hpp>
#include <sprout/uuid/uuid.hpp>
#include <sprout/checksum/sha1.hpp>
namespace sprout {
namespace uuids {
//
// sha1_name_generator
//
class sha1_name_generator {
public:
typedef sprout::uuids::uuid result_type;
private:
typedef typename result_type::value_type value_type;
private:
sprout::sha1 sum_;
private:
SPROUT_CONSTEXPR result_type sha_to_uuid_1(sprout::sha1::value_type const& value) const {
return result_type{{
value[0],
value[1],
value[2],
value[3],
value[4],
value[5],
static_cast<value_type>((value[6] & 0x5F) | 0x50),
value[7],
static_cast<value_type>((value[8] & 0xBF) | 0x80),
value[9],
value[10],
value[11],
value[12],
value[13],
value[14],
value[15]
}};
}
SPROUT_CONSTEXPR result_type sha_to_uuid(sprout::sha1 const& sha) const {
return sha_to_uuid_1(sha.checksum());
}
public:
SPROUT_CONSTEXPR sha1_name_generator()
: sum_(sprout::sha1::const_type().process_range(sprout::uuids::uuid{{0}}))
{}
explicit SPROUT_CONSTEXPR sha1_name_generator(sprout::uuids::uuid const& namespace_uuid)
: sum_(sprout::sha1::const_type().process_range(namespace_uuid))
{}
template<typename Elem, std::size_t N, typename Traits>
SPROUT_CONSTEXPR result_type operator()(sprout::basic_string<Elem, N, Traits> const& name) const {
return sha_to_uuid(sum_.process_range(name));
}
SPROUT_CONSTEXPR result_type operator()(char const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(wchar_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<wchar_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char16_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char16_t>::length(name)));
}
SPROUT_CONSTEXPR result_type operator()(char32_t const* name) const {
return sha_to_uuid(sum_.process_bytes(name, sprout::char_traits<char32_t>::length(name)));
}
};
} // namespace uuids
} // namespace sprout
#endif // #ifndef SPROUT_UUID_SHA1_NAME_GENERATOR_HPP