@@ -82,9 +82,19 @@ namespace uuids
8282 }
8383
8484 template <typename TChar>
85- constexpr inline unsigned char hexpair2char (TChar const a, TChar const b )
85+ constexpr std::basic_string_view<TChar> to_string_view (TChar const * str )
8686 {
87- return (hex2char (a) << 4 ) | hex2char (b);
87+ if (str) return str;
88+ return {};
89+ }
90+
91+ template <typename StringType>
92+ constexpr std::basic_string_view<
93+ typename StringType::value_type,
94+ typename StringType::traits_type>
95+ to_string_view (StringType const & str)
96+ {
97+ return str;
8898 }
8999
90100 class sha1
@@ -348,17 +358,14 @@ namespace uuids
348358 public:
349359 using value_type = uint8_t ;
350360
351- constexpr uuid () noexcept : data({}) {} ;
361+ constexpr uuid () noexcept = default ;
352362
353363 uuid (value_type(&arr)[16 ]) noexcept
354364 {
355365 std::copy (std::cbegin (arr), std::cend (arr), std::begin (data));
356366 }
357367
358- uuid (std::array<value_type, 16 > const & arr) noexcept
359- {
360- std::copy (std::cbegin (arr), std::cend (arr), std::begin (data));
361- }
368+ constexpr uuid (std::array<value_type, 16 > const & arr) noexcept : data{arr} {}
362369
363370 explicit uuid (span<value_type, 16 > bytes)
364371 {
@@ -416,29 +423,25 @@ namespace uuids
416423 return span<std::byte const , 16 >(reinterpret_cast <std::byte const *>(data.data ()), 16 );
417424 }
418425
419- template < class CharT = char >
420- static bool is_valid_uuid (CharT const * str ) noexcept
426+ template < typename StringType >
427+ constexpr static bool is_valid_uuid (StringType const & in_str ) noexcept
421428 {
429+ auto str = detail::to_string_view (in_str);
422430 bool firstDigit = true ;
423431 int hasBraces = 0 ;
424432 size_t index = 0 ;
425- size_t size = 0 ;
426- if constexpr (std::is_same_v<CharT, char >)
427- size = strlen (str);
428- else
429- size = wcslen (str);
430433
431- if (str == nullptr || size == 0 )
434+ if (str. empty ())
432435 return false ;
433436
434- if (str[ 0 ] == static_cast <CharT>( ' {' ) )
437+ if (str. front () == ' {' )
435438 hasBraces = 1 ;
436- if (hasBraces && str[size - 1 ] != static_cast <CharT>( ' }' ) )
439+ if (hasBraces && str. back () != ' }' )
437440 return false ;
438441
439- for (size_t i = hasBraces; i < size - hasBraces; ++i)
442+ for (size_t i = hasBraces; i < str. size () - hasBraces; ++i)
440443 {
441- if (str[i] == static_cast <CharT>( ' -' ) ) continue ;
444+ if (str[i] == ' -' ) continue ;
442445
443446 if (index >= 16 || !detail::is_hex (str[i]))
444447 {
@@ -464,39 +467,26 @@ namespace uuids
464467 return true ;
465468 }
466469
467- template <class CharT = char ,
468- class Traits = std::char_traits<CharT>,
469- class Allocator = std::allocator<CharT>>
470- static bool is_valid_uuid (std::basic_string<CharT, Traits, Allocator> const & str) noexcept
470+ template <typename StringType>
471+ constexpr static std::optional<uuid> from_string (StringType const & in_str) noexcept
471472 {
472- return is_valid_uuid (str.c_str ());
473- }
474-
475- template <class CharT = char >
476- static std::optional<uuid> from_string (CharT const * str) noexcept
477- {
478- CharT digit = 0 ;
473+ auto str = detail::to_string_view (in_str);
479474 bool firstDigit = true ;
480475 int hasBraces = 0 ;
481476 size_t index = 0 ;
482- size_t size = 0 ;
483- if constexpr (std::is_same_v<CharT, char >)
484- size = strlen (str);
485- else
486- size = wcslen (str);
487477
488478 std::array<uint8_t , 16 > data{ { 0 } };
489479
490- if (str == nullptr || size == 0 ) return {};
480+ if (str. empty () ) return {};
491481
492- if (str[ 0 ] == static_cast <CharT>( ' {' ) )
482+ if (str. front () == ' {' )
493483 hasBraces = 1 ;
494- if (hasBraces && str[size - 1 ] != static_cast <CharT>( ' }' ) )
484+ if (hasBraces && str. back () != ' }' )
495485 return {};
496486
497- for (size_t i = hasBraces; i < size - hasBraces; ++i)
487+ for (size_t i = hasBraces; i < str. size () - hasBraces; ++i)
498488 {
499- if (str[i] == static_cast <CharT>( ' -' ) ) continue ;
489+ if (str[i] == ' -' ) continue ;
500490
501491 if (index >= 16 || !detail::is_hex (str[i]))
502492 {
@@ -505,12 +495,12 @@ namespace uuids
505495
506496 if (firstDigit)
507497 {
508- digit = str[i];
498+ data[index] = detail::hex2char ( str[i]) << 4 ;
509499 firstDigit = false ;
510500 }
511501 else
512502 {
513- data[index++] = detail::hexpair2char (digit, str[i]);
503+ data[index++] | = detail::hex2char ( str[i]);
514504 firstDigit = true ;
515505 }
516506 }
@@ -520,15 +510,7 @@ namespace uuids
520510 return {};
521511 }
522512
523- return uuid{ std::cbegin (data), std::cend (data) };
524- }
525-
526- template <class CharT = char ,
527- class Traits = std::char_traits<CharT>,
528- class Allocator = std::allocator<CharT>>
529- static std::optional<uuid> from_string (std::basic_string<CharT, Traits, Allocator> const & str) noexcept
530- {
531- return from_string (str.c_str ());
513+ return uuid{ data };
532514 }
533515
534516 private:
@@ -769,27 +751,11 @@ namespace uuids
769751 : nsuuid(namespace_uuid)
770752 {}
771753
772- template <class CharT = char >
773- uuid operator ()(CharT const * name)
774- {
775- size_t size = 0 ;
776- if constexpr (std::is_same_v<CharT, char >)
777- size = strlen (name);
778- else
779- size = wcslen (name);
780-
781- reset ();
782- process_characters (name, size);
783- return make_uuid ();
784- }
785-
786- template <class CharT = char ,
787- class Traits = std::char_traits<CharT>,
788- class Allocator = std::allocator<CharT>>
789- uuid operator ()(std::basic_string<CharT, Traits, Allocator> const & name)
754+ template <typename StringType>
755+ uuid operator ()(StringType const & name)
790756 {
791757 reset ();
792- process_characters (name. data (), name. size ( ));
758+ process_characters (detail::to_string_view ( name));
793759 return make_uuid ();
794760 }
795761
@@ -802,26 +768,22 @@ namespace uuids
802768 std::copy (std::cbegin (nsbytes), std::cend (nsbytes), bytes);
803769 hasher.process_bytes (bytes, 16 );
804770 }
805-
806- template <typename char_type,
807- typename = std::enable_if_t <std::is_integral<char_type>::value>>
808- void process_characters (char_type const * const characters, size_t const count)
771+
772+ template <typename CharT, typename Traits>
773+ void process_characters (std::basic_string_view<CharT, Traits> const str)
809774 {
810- for (size_t i = 0 ; i < count; i++)
775+ for (uint32_t c : str)
811776 {
812- uint32_t c = characters[i];
813- hasher.process_byte (static_cast <unsigned char >((c >> 0 ) & 0xFF ));
814- hasher.process_byte (static_cast <unsigned char >((c >> 8 ) & 0xFF ));
815- hasher.process_byte (static_cast <unsigned char >((c >> 16 ) & 0xFF ));
816- hasher.process_byte (static_cast <unsigned char >((c >> 24 ) & 0xFF ));
777+ hasher.process_byte (static_cast <uint8_t >(c & 0xFF ));
778+ if constexpr (!std::is_same_v<CharT, char >)
779+ {
780+ hasher.process_byte (static_cast <uint8_t >((c >> 8 ) & 0xFF ));
781+ hasher.process_byte (static_cast <uint8_t >((c >> 16 ) & 0xFF ));
782+ hasher.process_byte (static_cast <uint8_t >((c >> 24 ) & 0xFF ));
783+ }
817784 }
818785 }
819786
820- void process_characters (const char * const characters, size_t const count)
821- {
822- hasher.process_bytes (characters, count);
823- }
824-
825787 uuid make_uuid ()
826788 {
827789 detail::sha1::digest8_t digest;
0 commit comments