2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
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)
|
|
|
|
=============================================================================*/
|
2011-10-25 09:16:27 +00:00
|
|
|
#ifndef SPROUT_UUID_STRING_GENERATOR_HPP
|
|
|
|
#define SPROUT_UUID_STRING_GENERATOR_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
2011-10-26 06:57:31 +00:00
|
|
|
#include <stdexcept>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/string.hpp>
|
|
|
|
#include <sprout/uuid/uuid.hpp>
|
|
|
|
#include <sprout/uuid/detail/table.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-01-11 19:08:44 +00:00
|
|
|
#include <sprout/algorithm/find.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace uuids {
|
|
|
|
//
|
|
|
|
// string_generator
|
|
|
|
//
|
|
|
|
class string_generator {
|
|
|
|
public:
|
|
|
|
typedef sprout::uuids::uuid result_type;
|
|
|
|
private:
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator>
|
2011-10-25 09:16:27 +00:00
|
|
|
struct next_char {
|
|
|
|
public:
|
2013-08-07 13:13:03 +00:00
|
|
|
typedef typename std::iterator_traits<ForwardIterator>::value_type char_type;
|
2011-10-25 09:16:27 +00:00
|
|
|
public:
|
|
|
|
char_type c;
|
2013-08-07 13:13:03 +00:00
|
|
|
ForwardIterator first;
|
|
|
|
ForwardIterator last;
|
2011-10-25 09:16:27 +00:00
|
|
|
public:
|
2013-08-07 13:13:03 +00:00
|
|
|
SPROUT_CONSTEXPR next_char(ForwardIterator f, ForwardIterator l)
|
2011-10-26 06:57:31 +00:00
|
|
|
: c(f != l
|
|
|
|
? *f
|
2013-03-18 10:12:21 +00:00
|
|
|
: throw std::runtime_error("string_generator: invalid uuid string (out of range)")
|
2011-10-26 06:57:31 +00:00
|
|
|
)
|
2011-10-25 09:16:27 +00:00
|
|
|
, first(sprout::next(f))
|
|
|
|
, last(l)
|
|
|
|
{}
|
|
|
|
SPROUT_CONSTEXPR next_char next() const {
|
|
|
|
return next_char(first, last);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
SPROUT_CONSTEXPR std::uint8_t value_at(std::size_t i) const {
|
|
|
|
return i < 22
|
|
|
|
? sprout::uuids::detail::values<void>::table[i]
|
2013-03-18 10:12:21 +00:00
|
|
|
: throw std::runtime_error("string_generator: invalid uuid string (invalid character)")
|
2011-10-25 09:16:27 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Elem>
|
|
|
|
SPROUT_CONSTEXPR std::uint8_t get_value(Elem c) const {
|
|
|
|
return value_at(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::distance(
|
2014-10-23 08:23:38 +00:00
|
|
|
sprout::uuids::detail::digits<Elem>::value.begin(),
|
2013-01-11 19:08:44 +00:00
|
|
|
sprout::find(
|
2014-10-23 08:23:38 +00:00
|
|
|
sprout::uuids::detail::digits<Elem>::value.begin(),
|
|
|
|
sprout::uuids::detail::digits<Elem>::value.end(),
|
2011-10-25 09:16:27 +00:00
|
|
|
c
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-10-23 08:23:38 +00:00
|
|
|
template<typename Elem>
|
|
|
|
static SPROUT_CONSTEXPR bool is_dash(Elem c) {
|
|
|
|
return c == sprout::uuids::detail::dash<Elem>::value;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2014-10-23 08:23:38 +00:00
|
|
|
template<typename Elem>
|
|
|
|
static SPROUT_CONSTEXPR bool is_open_brace(Elem c) {
|
|
|
|
return c == sprout::uuids::detail::lbrace<Elem>::value;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2014-10-23 08:23:38 +00:00
|
|
|
template<typename Elem>
|
|
|
|
static SPROUT_CONSTEXPR bool is_close_brace(Elem c) {
|
|
|
|
return c == sprout::uuids::detail::rbrace<Elem>::value;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2014-10-23 08:23:38 +00:00
|
|
|
template<typename Elem>
|
|
|
|
static SPROUT_CONSTEXPR bool is_close_brace(Elem c, Elem open_brace) {
|
|
|
|
return is_open_brace(open_brace) && is_close_brace(c);
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2_3(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, std::uint8_t byte, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return generate_2(nc, open_brace, has_dashes, args..., static_cast<std::uint8_t>((byte << 4) | get_value(nc.c)));
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2_2(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return generate_2_3(nc.next(), open_brace, has_dashes, get_value(nc.c), args...);
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) == 6 || sizeof...(Args) == 8 || sizeof...(Args) == 10,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2_1(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return has_dashes
|
|
|
|
? is_dash(nc.c)
|
|
|
|
? generate_2_2(nc.next(), open_brace, has_dashes, args...)
|
2013-03-18 10:12:21 +00:00
|
|
|
: throw std::runtime_error("string_generator: invalid uuid string (dashes not found)")
|
2011-10-25 09:16:27 +00:00
|
|
|
: generate_2_2(nc, open_brace, has_dashes, args...)
|
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) == 4,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2_1(next_char<ForwardIterator> nc, Char open_brace, bool, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return is_dash(nc.c)
|
|
|
|
? generate_2_2(nc.next(), open_brace, true, args...)
|
|
|
|
: generate_2_2(nc, open_brace, false, args...)
|
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) != 4 && sizeof...(Args) != 6 && sizeof...(Args) != 8 && sizeof...(Args) != 10,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2_1(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return generate_2_2(nc, open_brace, has_dashes, args...);
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) == 16,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2(next_char<ForwardIterator> nc, Char open_brace, bool, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return !open_brace || (open_brace && is_close_brace(nc.next().c, open_brace))
|
|
|
|
? result_type{{args...}}
|
2013-03-18 10:12:21 +00:00
|
|
|
: throw std::runtime_error("string_generator: invalid uuid string (brace not closed)")
|
2011-10-25 09:16:27 +00:00
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) == 0,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return generate_2_2(nc, open_brace, has_dashes, args...);
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator, typename Char, typename... Args>
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sizeof...(Args) != 0 && sizeof...(Args) != 16,
|
|
|
|
result_type
|
2012-10-05 15:58:56 +00:00
|
|
|
>::type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_2(next_char<ForwardIterator> nc, Char open_brace, bool has_dashes, Args... args) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return generate_2_1(nc.next(), open_brace, has_dashes, args...);
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
SPROUT_CONSTEXPR result_type
|
2013-08-07 13:13:03 +00:00
|
|
|
generate_1(next_char<ForwardIterator> nc) const {
|
2011-10-25 09:16:27 +00:00
|
|
|
return is_open_brace(nc.c)
|
|
|
|
? generate_2(nc.next(), nc.c, false)
|
2013-08-07 13:13:03 +00:00
|
|
|
: generate_2(nc, typename next_char<ForwardIterator>::char_type(), false)
|
2011-10-25 09:16:27 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
public:
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator>
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(ForwardIterator first, ForwardIterator last) const {
|
|
|
|
return generate_1(next_char<ForwardIterator>(first, last));
|
2012-03-18 13:04:09 +00:00
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(sprout::basic_string<Elem, N, Traits> const& s) const {
|
|
|
|
return operator()(s.begin(), s.end());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(char const* s) const {
|
|
|
|
return operator()(s, s + sprout::char_traits<char>::length(s));
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(wchar_t const* s) const {
|
|
|
|
return operator()(s, s + sprout::char_traits<wchar_t>::length(s));
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(char16_t const* s) const {
|
|
|
|
return operator()(s, s + sprout::char_traits<char16_t>::length(s));
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(char32_t const* s) const {
|
|
|
|
return operator()(s, s + sprout::char_traits<char32_t>::length(s));
|
|
|
|
}
|
|
|
|
};
|
2013-04-14 10:42:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// make_uuid
|
|
|
|
//
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename ForwardIterator>
|
2013-04-14 10:42:34 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
2013-08-07 13:13:03 +00:00
|
|
|
make_uuid(ForwardIterator first, ForwardIterator last) {
|
2013-04-14 10:42:34 +00:00
|
|
|
return sprout::uuids::string_generator()(first, last);
|
|
|
|
}
|
|
|
|
template<typename Elem, std::size_t N, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid(sprout::basic_string<Elem, N, Traits> const& s) {
|
|
|
|
return sprout::uuids::string_generator()(s);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid(char const* s) {
|
|
|
|
return sprout::uuids::string_generator()(s);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid(wchar_t const* s) {
|
|
|
|
return sprout::uuids::string_generator()(s);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid(char16_t const* s) {
|
|
|
|
return sprout::uuids::string_generator()(s);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR sprout::uuids::uuid
|
|
|
|
make_uuid(char32_t const* s) {
|
|
|
|
return sprout::uuids::string_generator()(s);
|
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
} // namespace uuids
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_UUID_STRING_GENERATOR_HPP
|