]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost string_generator.hpp header file ----------------------------------------------// |
2 | ||
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 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | ||
8 | #ifndef BOOST_UUID_STRING_GENERATOR_HPP | |
9 | #define BOOST_UUID_STRING_GENERATOR_HPP | |
10 | ||
11 | #include <boost/uuid/uuid.hpp> | |
12 | #include <string> | |
13 | #include <cstring> // for strlen, wcslen | |
14 | #include <iterator> | |
15 | #include <algorithm> // for find | |
16 | #include <stdexcept> | |
17 | #include <boost/throw_exception.hpp> | |
18 | ||
19 | #ifdef BOOST_NO_STDC_NAMESPACE | |
20 | namespace std { | |
21 | using ::strlen; | |
22 | using ::wcslen; | |
23 | } //namespace std | |
24 | #endif //BOOST_NO_STDC_NAMESPACE | |
25 | ||
26 | namespace boost { | |
27 | namespace uuids { | |
28 | ||
29 | // generate a uuid from a string | |
30 | // lexical_cast works fine using uuid_io.hpp | |
31 | // but this generator should accept more forms | |
32 | // and be more efficient | |
33 | // would like to accept the following forms: | |
34 | // 0123456789abcdef0123456789abcdef | |
35 | // 01234567-89ab-cdef-0123456789abcdef | |
36 | // {01234567-89ab-cdef-0123456789abcdef} | |
37 | // {0123456789abcdef0123456789abcdef} | |
38 | // others? | |
39 | struct string_generator { | |
40 | typedef uuid result_type; | |
41 | ||
42 | template <typename ch, typename char_traits, typename alloc> | |
43 | uuid operator()(std::basic_string<ch, char_traits, alloc> const& s) const { | |
44 | return operator()(s.begin(), s.end()); | |
45 | } | |
46 | ||
47 | uuid operator()(char const*const s) const { | |
48 | return operator()(s, s+std::strlen(s)); | |
49 | } | |
50 | ||
51 | uuid operator()(wchar_t const*const s) const { | |
52 | return operator()(s, s+std::wcslen(s)); | |
53 | } | |
54 | ||
55 | template <typename CharIterator> | |
56 | uuid operator()(CharIterator begin, CharIterator end) const | |
57 | { | |
58 | typedef typename std::iterator_traits<CharIterator>::value_type char_type; | |
59 | ||
60 | // check open brace | |
61 | char_type c = get_next_char(begin, end); | |
62 | bool has_open_brace = is_open_brace(c); | |
63 | char_type open_brace_char = c; | |
64 | if (has_open_brace) { | |
65 | c = get_next_char(begin, end); | |
66 | } | |
67 | ||
68 | bool has_dashes = false; | |
69 | ||
70 | uuid u; | |
71 | int i=0; | |
72 | for (uuid::iterator it_byte=u.begin(); it_byte!=u.end(); ++it_byte, ++i) { | |
73 | if (it_byte != u.begin()) { | |
74 | c = get_next_char(begin, end); | |
75 | } | |
76 | ||
77 | if (i == 4) { | |
78 | has_dashes = is_dash(c); | |
79 | if (has_dashes) { | |
80 | c = get_next_char(begin, end); | |
81 | } | |
82 | } | |
83 | ||
84 | if (has_dashes) { | |
85 | if (i == 6 || i == 8 || i == 10) { | |
86 | if (is_dash(c)) { | |
87 | c = get_next_char(begin, end); | |
88 | } else { | |
89 | throw_invalid(); | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | *it_byte = get_value(c); | |
95 | ||
96 | c = get_next_char(begin, end); | |
97 | *it_byte <<= 4; | |
98 | *it_byte |= get_value(c); | |
99 | } | |
100 | ||
101 | // check close brace | |
102 | if (has_open_brace) { | |
103 | c = get_next_char(begin, end); | |
104 | check_close_brace(c, open_brace_char); | |
105 | } | |
106 | ||
107 | return u; | |
108 | } | |
109 | ||
110 | private: | |
111 | template <typename CharIterator> | |
112 | typename std::iterator_traits<CharIterator>::value_type | |
113 | get_next_char(CharIterator& begin, CharIterator end) const { | |
114 | if (begin == end) { | |
115 | throw_invalid(); | |
116 | } | |
117 | return *begin++; | |
118 | } | |
119 | ||
120 | unsigned char get_value(char c) const { | |
121 | static char const*const digits_begin = "0123456789abcdefABCDEF"; | |
122 | static char const*const digits_end = digits_begin + 22; | |
123 | ||
124 | static unsigned char const values[] = | |
125 | { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 | |
126 | , static_cast<unsigned char>(-1) }; | |
127 | ||
128 | char const* d = std::find(digits_begin, digits_end, c); | |
129 | return values[d - digits_begin]; | |
130 | } | |
131 | ||
132 | unsigned char get_value(wchar_t c) const { | |
133 | static wchar_t const*const digits_begin = L"0123456789abcdefABCDEF"; | |
134 | static wchar_t const*const digits_end = digits_begin + 22; | |
135 | ||
136 | static unsigned char const values[] = | |
137 | { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 | |
138 | , static_cast<unsigned char>(-1) }; | |
139 | ||
140 | wchar_t const* d = std::find(digits_begin, digits_end, c); | |
141 | return values[d - digits_begin]; | |
142 | } | |
143 | ||
144 | bool is_dash(char c) const { | |
145 | return c == '-'; | |
146 | } | |
147 | ||
148 | bool is_dash(wchar_t c) const { | |
149 | return c == L'-'; | |
150 | } | |
151 | ||
152 | // return closing brace | |
153 | bool is_open_brace(char c) const { | |
154 | return (c == '{'); | |
155 | } | |
156 | ||
157 | bool is_open_brace(wchar_t c) const { | |
158 | return (c == L'{'); | |
159 | } | |
160 | ||
161 | void check_close_brace(char c, char open_brace) const { | |
162 | if (open_brace == '{' && c == '}') { | |
163 | //great | |
164 | } else { | |
165 | throw_invalid(); | |
166 | } | |
167 | } | |
168 | ||
169 | void check_close_brace(wchar_t c, wchar_t open_brace) const { | |
170 | if (open_brace == L'{' && c == L'}') { | |
171 | // great | |
172 | } else { | |
173 | throw_invalid(); | |
174 | } | |
175 | } | |
176 | ||
177 | void throw_invalid() const { | |
178 | BOOST_THROW_EXCEPTION(std::runtime_error("invalid uuid string")); | |
179 | } | |
180 | }; | |
181 | ||
182 | }} // namespace boost::uuids | |
183 | ||
184 | #endif //BOOST_UUID_STRING_GENERATOR_HPP | |
185 |