string_generator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef BOOST_UUID_STRING_GENERATOR_HPP_INCLUDED
  2. #define BOOST_UUID_STRING_GENERATOR_HPP_INCLUDED
  3. // Copyright 2010 Andy Tompkins
  4. // Copyright 2024 Peter Dimov
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // https://www.boost.org/LICENSE_1_0.txt
  7. #include <boost/uuid/uuid.hpp>
  8. #include <boost/throw_exception.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/config/workaround.hpp>
  11. #include <string>
  12. #include <iterator>
  13. #include <stdexcept>
  14. #include <cstdio>
  15. #include <cstddef>
  16. #if BOOST_WORKAROUND(BOOST_GCC, < 60000)
  17. # define BOOST_UUID_CXX14_CONSTEXPR
  18. #else
  19. # define BOOST_UUID_CXX14_CONSTEXPR BOOST_CXX14_CONSTEXPR
  20. #endif
  21. namespace boost {
  22. namespace uuids {
  23. namespace detail
  24. {
  25. template<class Ch>
  26. BOOST_CXX14_CONSTEXPR std::size_t cx_strlen( Ch const* s )
  27. {
  28. std::size_t r = 0;
  29. while( *s ) ++s, ++r;
  30. return r;
  31. }
  32. template<class Ch>
  33. BOOST_CXX14_CONSTEXPR Ch const* cx_find( Ch const* s, std::size_t n, Ch ch )
  34. {
  35. for( std::size_t i = 0; i < n; ++i )
  36. {
  37. if( s[i] == ch ) return s + i;
  38. }
  39. return nullptr;
  40. }
  41. } // namespace detail
  42. // Generates a UUID from a string
  43. //
  44. // Accepts the following forms:
  45. //
  46. // 0123456789abcdef0123456789abcdef
  47. // 01234567-89ab-cdef-0123-456789abcdef
  48. // {01234567-89ab-cdef-0123-456789abcdef}
  49. // {0123456789abcdef0123456789abcdef}
  50. struct string_generator
  51. {
  52. private:
  53. template <typename CharIterator>
  54. typename std::iterator_traits<CharIterator>::value_type
  55. BOOST_UUID_CXX14_CONSTEXPR get_next_char( CharIterator& begin, CharIterator end, int& ipos ) const
  56. {
  57. if( begin == end )
  58. {
  59. throw_invalid( ipos, "unexpected end of input" );
  60. }
  61. ++ipos;
  62. return *begin++;
  63. }
  64. public:
  65. using result_type = uuid;
  66. template<class CharIterator>
  67. BOOST_UUID_CXX14_CONSTEXPR uuid operator()( CharIterator begin, CharIterator end ) const
  68. {
  69. using char_type = typename std::iterator_traits<CharIterator>::value_type;
  70. int ipos = 0;
  71. // check open brace
  72. char_type c = get_next_char( begin, end, ipos );
  73. bool has_open_brace = is_open_brace( c );
  74. if( has_open_brace )
  75. {
  76. c = get_next_char( begin, end, ipos );
  77. }
  78. bool has_dashes = false;
  79. uuid u;
  80. int i = 0;
  81. for( uuid::iterator it_byte = u.begin(); it_byte != u.end(); ++it_byte, ++i )
  82. {
  83. if( it_byte != u.begin() )
  84. {
  85. c = get_next_char( begin, end, ipos );
  86. }
  87. if( i == 4 )
  88. {
  89. has_dashes = is_dash( c );
  90. if( has_dashes )
  91. {
  92. c = get_next_char( begin, end, ipos );
  93. }
  94. }
  95. else if( i == 6 || i == 8 || i == 10 )
  96. {
  97. // if there are dashes, they must be in every slot
  98. if( has_dashes )
  99. {
  100. if( is_dash( c ) )
  101. {
  102. c = get_next_char( begin, end, ipos );
  103. }
  104. else
  105. {
  106. throw_invalid( ipos - 1, "dash expected" );
  107. }
  108. }
  109. }
  110. *it_byte = get_value( c, ipos - 1 );
  111. c = get_next_char( begin, end, ipos );
  112. *it_byte <<= 4;
  113. *it_byte |= get_value( c, ipos - 1 );
  114. }
  115. // check close brace
  116. if( has_open_brace )
  117. {
  118. c = get_next_char( begin, end, ipos );
  119. if( !is_close_brace( c ) )
  120. {
  121. throw_invalid( ipos - 1, "closing brace expected" );
  122. }
  123. }
  124. // check end of string - any additional data is an invalid uuid
  125. if( begin != end )
  126. {
  127. throw_invalid( ipos, "unexpected extra input" );
  128. }
  129. return u;
  130. }
  131. template<class Ch, class Traits, class Alloc>
  132. BOOST_UUID_CXX14_CONSTEXPR uuid operator()( std::basic_string<Ch, Traits, Alloc> const& s ) const
  133. {
  134. return operator()( s.begin(), s.end() );
  135. }
  136. BOOST_UUID_CXX14_CONSTEXPR uuid operator()( char const* s ) const
  137. {
  138. return operator()( s, s + detail::cx_strlen( s ) );
  139. }
  140. BOOST_UUID_CXX14_CONSTEXPR uuid operator()( wchar_t const* s ) const
  141. {
  142. return operator()( s, s + detail::cx_strlen( s ) );
  143. }
  144. private:
  145. BOOST_NORETURN void throw_invalid( int ipos, char const* error ) const
  146. {
  147. char buffer[ 16 ];
  148. std::snprintf( buffer, sizeof( buffer ), "%d", ipos );
  149. BOOST_THROW_EXCEPTION( std::runtime_error( std::string( "Invalid UUID string at position " ) + buffer + ": " + error ) );
  150. }
  151. BOOST_UUID_CXX14_CONSTEXPR unsigned char get_value( char c, int ipos ) const
  152. {
  153. constexpr char digits[] = "0123456789abcdefABCDEF";
  154. constexpr std::size_t digits_len = sizeof(digits) / sizeof(char) - 1;
  155. constexpr unsigned char values[] =
  156. { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
  157. auto pos = detail::cx_find( digits, digits_len, c );
  158. if( pos == 0 )
  159. {
  160. throw_invalid( ipos, "hex digit expected" );
  161. }
  162. return values[ pos - digits ];
  163. }
  164. BOOST_UUID_CXX14_CONSTEXPR unsigned char get_value( wchar_t c, int ipos ) const
  165. {
  166. constexpr wchar_t digits[] = L"0123456789abcdefABCDEF";
  167. constexpr std::size_t digits_len = sizeof(digits) / sizeof(wchar_t) - 1;
  168. constexpr unsigned char values[] =
  169. { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,10,11,12,13,14,15 };
  170. auto pos = detail::cx_find( digits, digits_len, c );
  171. if( pos == 0 )
  172. {
  173. throw_invalid( ipos, "hex digit expected" );
  174. }
  175. return values[ pos - digits ];
  176. }
  177. static constexpr bool is_dash( char c )
  178. {
  179. return c == '-';
  180. }
  181. static constexpr bool is_dash( wchar_t c )
  182. {
  183. return c == L'-';
  184. }
  185. static constexpr bool is_open_brace( char c )
  186. {
  187. return c == '{';
  188. }
  189. static constexpr bool is_open_brace( wchar_t c )
  190. {
  191. return c == L'{';
  192. }
  193. static constexpr bool is_close_brace( char c )
  194. {
  195. return c == '}';
  196. }
  197. static constexpr bool is_close_brace( wchar_t c )
  198. {
  199. return c == L'}';
  200. }
  201. };
  202. }} // namespace boost::uuids
  203. #undef BOOST_UUID_CXX14_CONSTEXPR
  204. #endif // BOOST_UUID_STRING_GENERATOR_HPP_INCLUDED