1 // Boost string_generator.hpp header file ----------------------------------------------//
3 // Copyright 2010 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // https://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_UUID_STRING_GENERATOR_HPP
9 #define BOOST_UUID_STRING_GENERATOR_HPP
11 #include <boost/uuid/uuid.hpp>
13 #include <cstring> // for strlen, wcslen
15 #include <algorithm> // for find
17 #include <boost/throw_exception.hpp>
18 #include <boost/config.hpp>
20 #ifdef BOOST_NO_STDC_NAMESPACE
25 #endif //BOOST_NO_STDC_NAMESPACE
30 // generate a uuid from a string
31 // lexical_cast works fine using uuid_io.hpp
32 // but this generator should accept more forms
33 // and be more efficient
34 // would like to accept the following forms:
35 // 0123456789abcdef0123456789abcdef
36 // 01234567-89ab-cdef-0123-456789abcdef
37 // {01234567-89ab-cdef-0123-456789abcdef}
38 // {0123456789abcdef0123456789abcdef}
40 struct string_generator {
41 typedef uuid result_type;
43 template <typename ch, typename char_traits, typename alloc>
44 uuid operator()(std::basic_string<ch, char_traits, alloc> const& s) const {
45 return operator()(s.begin(), s.end());
48 uuid operator()(char const*const s) const {
49 return operator()(s, s+std::strlen(s));
52 uuid operator()(wchar_t const*const s) const {
53 return operator()(s, s+std::wcslen(s));
56 template <typename CharIterator>
57 uuid operator()(CharIterator begin, CharIterator end) const
59 typedef typename std::iterator_traits<CharIterator>::value_type char_type;
62 char_type c = get_next_char(begin, end);
63 bool has_open_brace = is_open_brace(c);
64 char_type open_brace_char = c;
66 c = get_next_char(begin, end);
69 bool has_dashes = false;
73 for (uuid::iterator it_byte=u.begin(); it_byte!=u.end(); ++it_byte, ++i) {
74 if (it_byte != u.begin()) {
75 c = get_next_char(begin, end);
79 has_dashes = is_dash(c);
81 c = get_next_char(begin, end);
85 // if there are dashes, they must be in every slot
86 else if (i == 6 || i == 8 || i == 10) {
87 if (has_dashes == true) {
89 c = get_next_char(begin, end);
97 *it_byte = get_value(c);
99 c = get_next_char(begin, end);
101 *it_byte |= get_value(c);
105 if (has_open_brace) {
106 c = get_next_char(begin, end);
107 check_close_brace(c, open_brace_char);
110 // check end of string - any additional data is an invalid uuid
119 template <typename CharIterator>
120 typename std::iterator_traits<CharIterator>::value_type
121 get_next_char(CharIterator& begin, CharIterator end) const {
128 unsigned char get_value(char c) const {
129 static char const digits_begin[] = "0123456789abcdefABCDEF";
130 static size_t digits_len = (sizeof(digits_begin) / sizeof(char)) - 1;
131 static char const*const digits_end = digits_begin + digits_len;
133 static unsigned char const values[] =
134 { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
136 size_t pos = std::find(digits_begin, digits_end, c) - digits_begin;
137 if (pos >= digits_len) {
143 unsigned char get_value(wchar_t c) const {
144 static wchar_t const digits_begin[] = L"0123456789abcdefABCDEF";
145 static size_t digits_len = (sizeof(digits_begin) / sizeof(wchar_t)) - 1;
146 static wchar_t const*const digits_end = digits_begin + digits_len;
148 static unsigned char const values[] =
149 { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
151 size_t pos = std::find(digits_begin, digits_end, c) - digits_begin;
152 if (pos >= digits_len) {
158 bool is_dash(char c) const {
162 bool is_dash(wchar_t c) const {
166 // return closing brace
167 bool is_open_brace(char c) const {
171 bool is_open_brace(wchar_t c) const {
175 void check_close_brace(char c, char open_brace) const {
176 if (open_brace == '{' && c == '}') {
183 void check_close_brace(wchar_t c, wchar_t open_brace) const {
184 if (open_brace == L'{' && c == L'}') {
191 BOOST_NORETURN void throw_invalid() const {
192 BOOST_THROW_EXCEPTION(std::runtime_error("invalid uuid string"));
196 }} // namespace boost::uuids
198 #endif //BOOST_UUID_STRING_GENERATOR_HPP