mirror of
https://github.com/boostorg/uuid.git
synced 2026-01-19 16:52:14 +00:00
Add uuid_from_string
This commit is contained in:
64
include/boost/uuid/detail/uuid_from_string.hpp
Normal file
64
include/boost/uuid/detail/uuid_from_string.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef BOOST_UUID_DETAIL_UUID_FROM_STRING_INCLUDED
|
||||
#define BOOST_UUID_DETAIL_UUID_FROM_STRING_INCLUDED
|
||||
|
||||
// Copyright 2026 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/detail/from_chars.hpp>
|
||||
#include <boost/uuid/detail/throw_invalid_uuid.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace boost {
|
||||
namespace uuids {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class Ch>
|
||||
BOOST_CXX14_CONSTEXPR
|
||||
uuid uuid_from_string( Ch const* first, Ch const* last )
|
||||
{
|
||||
uuid u;
|
||||
|
||||
auto r = from_chars( first, last, u );
|
||||
|
||||
if( r.ec != from_chars_error::none )
|
||||
{
|
||||
detail::throw_invalid_uuid( static_cast<int>( r.ptr - first ), r.ec );
|
||||
}
|
||||
|
||||
if( r.ptr != last )
|
||||
{
|
||||
detail::throw_invalid_uuid( static_cast<int>( r.ptr - first ), from_chars_error::unexpected_extra_input );
|
||||
}
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class Ch>
|
||||
BOOST_CXX14_CONSTEXPR
|
||||
uuid uuid_from_string( Ch const* str )
|
||||
{
|
||||
Ch const* first = str;
|
||||
Ch const* last = str + std::char_traits<Ch>::length( str );
|
||||
|
||||
return detail::uuid_from_string( first, last );
|
||||
}
|
||||
|
||||
template<class Str, class Ch = typename Str::value_type, class Tr = typename Str::traits_type>
|
||||
BOOST_CXX14_CONSTEXPR
|
||||
uuid uuid_from_string( Str const& str )
|
||||
{
|
||||
Ch const* first = str.data();
|
||||
Ch const* last = str.data() + str.size();
|
||||
|
||||
return detail::uuid_from_string( first, last );
|
||||
}
|
||||
|
||||
}} // namespace boost::uuids
|
||||
|
||||
#endif // #ifndef BOOST_UUID_DETAIL_UUID_FROM_STRING_INCLUDED
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <boost/uuid/detail/to_chars.hpp>
|
||||
#include <boost/uuid/detail/from_chars.hpp>
|
||||
#include <boost/uuid/detail/static_assert.hpp>
|
||||
#include <boost/uuid/detail/uuid_from_string.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <iosfwd>
|
||||
#include <istream>
|
||||
|
||||
@@ -44,6 +44,8 @@ boost_test(TYPE run SOURCES test_from_chars.cpp COMPILE_DEFINITIONS BOOST_UUID_N
|
||||
boost_test(TYPE run SOURCES test_from_chars_2.cpp COMPILE_DEFINITIONS BOOST_UUID_REPORT_IMPLEMENTATION=1)
|
||||
boost_test(TYPE run SOURCES test_from_chars_2.cpp COMPILE_DEFINITIONS BOOST_UUID_NO_SIMD=1 BOOST_UUID_REPORT_IMPLEMENTATION=1 NAME test_from_chars_2_no_simd)
|
||||
|
||||
boost_test(TYPE run SOURCES test_uuid_from_string.cpp)
|
||||
|
||||
boost_test(TYPE run SOURCES test_uuid_clock.cpp)
|
||||
|
||||
boost_test(TYPE run SOURCES test_nil_generator.cpp)
|
||||
|
||||
@@ -113,6 +113,9 @@ run test_from_chars.cpp : : : <define>BOOST_UUID_NO_SIMD <define>BOOST_UUID_REPO
|
||||
run test_from_chars_2.cpp : : : <define>BOOST_UUID_REPORT_IMPLEMENTATION ;
|
||||
run test_from_chars_2.cpp : : : <define>BOOST_UUID_NO_SIMD <define>BOOST_UUID_REPORT_IMPLEMENTATION : test_from_chars_2_no_simd ;
|
||||
|
||||
run test_uuid_from_string.cpp ;
|
||||
run test_uuid_from_string_2.cpp ;
|
||||
|
||||
# test uuid_clock
|
||||
|
||||
run test_uuid_clock.cpp ;
|
||||
|
||||
@@ -22,7 +22,7 @@ BOOST_UUID_CXX14_CONSTEXPR_RT uuid uuid_from_string( Ch const (&str)[ N ] )
|
||||
return u;
|
||||
}
|
||||
|
||||
#define TEST(str) { BOOST_UUID_CXX14_CONSTEXPR_RT auto u = uuid_from_string(str); BOOST_TEST_EQ(u, expected); }
|
||||
#define TEST(str) { BOOST_UUID_CXX14_CONSTEXPR_RT auto u = ::uuid_from_string(str); BOOST_TEST_EQ(u, expected); }
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
69
test/test_uuid_from_string.cpp
Normal file
69
test/test_uuid_from_string.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2025, 2026 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
#include <boost/core/detail/string_view.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <string>
|
||||
|
||||
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
|
||||
# include <string_view>
|
||||
#endif
|
||||
|
||||
//
|
||||
|
||||
using namespace boost::uuids;
|
||||
|
||||
template<class Ch> void test( uuid const& expected, Ch const* str )
|
||||
{
|
||||
BOOST_TEST_EQ( uuid_from_string( str ), expected );
|
||||
BOOST_TEST_EQ( uuid_from_string( std::basic_string<Ch>( str ) ), expected );
|
||||
BOOST_TEST_EQ( uuid_from_string( boost::core::basic_string_view<Ch>( str ) ), expected );
|
||||
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
|
||||
BOOST_TEST_EQ( uuid_from_string( std::basic_string_view<Ch>( str ) ), expected );
|
||||
#endif
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uuid const u1 = {{ 0 }};
|
||||
|
||||
test( u1, "00000000-0000-0000-0000-000000000000" );
|
||||
test( u1, L"00000000-0000-0000-0000-000000000000" );
|
||||
test( u1, u"00000000-0000-0000-0000-000000000000" );
|
||||
test( u1, U"00000000-0000-0000-0000-000000000000" );
|
||||
test( u1, u8"00000000-0000-0000-0000-000000000000" );
|
||||
|
||||
uuid const u2 = {{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }};
|
||||
|
||||
test( u2, "00010203-0405-0607-0809-0a0b0c0d0e0f" );
|
||||
test( u2, L"00010203-0405-0607-0809-0a0b0c0d0e0f" );
|
||||
test( u2, u"00010203-0405-0607-0809-0a0b0c0d0e0f" );
|
||||
test( u2, U"00010203-0405-0607-0809-0a0b0c0d0e0f" );
|
||||
test( u2, u8"00010203-0405-0607-0809-0a0b0c0d0e0f" );
|
||||
|
||||
test( u2, "00010203-0405-0607-0809-0A0B0C0D0E0F" );
|
||||
test( u2, L"00010203-0405-0607-0809-0A0B0C0D0E0F" );
|
||||
test( u2, u"00010203-0405-0607-0809-0A0B0C0D0E0F" );
|
||||
test( u2, U"00010203-0405-0607-0809-0A0B0C0D0E0F" );
|
||||
test( u2, u8"00010203-0405-0607-0809-0A0B0C0D0E0F" );
|
||||
|
||||
uuid const u3 = {{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }};
|
||||
|
||||
test( u3, "12345678-90ab-cdef-1234-567890abcdef" );
|
||||
test( u3, L"12345678-90ab-cdef-1234-567890abcdef" );
|
||||
test( u3, u"12345678-90ab-cdef-1234-567890abcdef" );
|
||||
test( u3, U"12345678-90ab-cdef-1234-567890abcdef" );
|
||||
test( u3, u8"12345678-90ab-cdef-1234-567890abcdef" );
|
||||
|
||||
test( u3, "12345678-90AB-CDEF-1234-567890abcdef" );
|
||||
test( u3, L"12345678-90AB-CDEF-1234-567890abcdef" );
|
||||
test( u3, u"12345678-90AB-CDEF-1234-567890abcdef" );
|
||||
test( u3, U"12345678-90AB-CDEF-1234-567890abcdef" );
|
||||
test( u3, u8"12345678-90AB-CDEF-1234-567890abcdef" );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
147
test/test_uuid_from_string_2.cpp
Normal file
147
test/test_uuid_from_string_2.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
// Copyright 2025 Peter Dimov
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#include <boost/uuid/uuid.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <string>
|
||||
|
||||
using namespace boost::uuids;
|
||||
|
||||
template<class Ch> void test( Ch const* str, int pos, from_chars_error err )
|
||||
{
|
||||
std::string expected;
|
||||
|
||||
try
|
||||
{
|
||||
detail::throw_invalid_uuid( pos, err );
|
||||
}
|
||||
catch( std::exception const& x )
|
||||
{
|
||||
expected = x.what();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
uuid_from_string( str );
|
||||
BOOST_ERROR( "uuid_from_string failed to throw" );
|
||||
}
|
||||
catch( std::exception const& x )
|
||||
{
|
||||
BOOST_TEST_EQ( expected, std::string( x.what() ) );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test( "", 0, from_chars_error::unexpected_end_of_input );
|
||||
test( L"", 0, from_chars_error::unexpected_end_of_input );
|
||||
test( u"", 0, from_chars_error::unexpected_end_of_input );
|
||||
test( U"", 0, from_chars_error::unexpected_end_of_input );
|
||||
test( u8"", 0, from_chars_error::unexpected_end_of_input );
|
||||
|
||||
test( "0", 1, from_chars_error::unexpected_end_of_input );
|
||||
test( L"0", 1, from_chars_error::unexpected_end_of_input );
|
||||
test( u"0", 1, from_chars_error::unexpected_end_of_input );
|
||||
test( U"0", 1, from_chars_error::unexpected_end_of_input );
|
||||
test( u8"0", 1, from_chars_error::unexpected_end_of_input );
|
||||
|
||||
test( "01", 2, from_chars_error::unexpected_end_of_input );
|
||||
test( L"01", 2, from_chars_error::unexpected_end_of_input );
|
||||
test( u"01", 2, from_chars_error::unexpected_end_of_input );
|
||||
test( U"01", 2, from_chars_error::unexpected_end_of_input );
|
||||
test( u8"01", 2, from_chars_error::unexpected_end_of_input );
|
||||
|
||||
test( "01234567-89aB-cDeF-0123-456789AbCd", 34, from_chars_error::unexpected_end_of_input );
|
||||
test( L"01234567-89aB-cDeF-0123-456789AbCd", 34, from_chars_error::unexpected_end_of_input );
|
||||
test( u"01234567-89aB-cDeF-0123-456789AbCd", 34, from_chars_error::unexpected_end_of_input );
|
||||
test( U"01234567-89aB-cDeF-0123-456789AbCd", 34, from_chars_error::unexpected_end_of_input );
|
||||
test( u8"01234567-89aB-cDeF-0123-456789AbCd", 34, from_chars_error::unexpected_end_of_input );
|
||||
|
||||
test( "01234567-89aB-cDeF-0123-456789AbCdE", 35, from_chars_error::unexpected_end_of_input );
|
||||
test( L"01234567-89aB-cDeF-0123-456789AbCdE", 35, from_chars_error::unexpected_end_of_input );
|
||||
test( u"01234567-89aB-cDeF-0123-456789AbCdE", 35, from_chars_error::unexpected_end_of_input );
|
||||
test( U"01234567-89aB-cDeF-0123-456789AbCdE", 35, from_chars_error::unexpected_end_of_input );
|
||||
test( u8"01234567-89aB-cDeF-0123-456789AbCdE", 35, from_chars_error::unexpected_end_of_input );
|
||||
|
||||
test( "@", 0, from_chars_error::hex_digit_expected );
|
||||
test( L"@", 0, from_chars_error::hex_digit_expected );
|
||||
test( u"@", 0, from_chars_error::hex_digit_expected );
|
||||
test( U"@", 0, from_chars_error::hex_digit_expected );
|
||||
test( u8"@", 0, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "G", 0, from_chars_error::hex_digit_expected );
|
||||
test( L"G", 0, from_chars_error::hex_digit_expected );
|
||||
test( u"G", 0, from_chars_error::hex_digit_expected );
|
||||
test( U"G", 0, from_chars_error::hex_digit_expected );
|
||||
test( u8"G", 0, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "g", 0, from_chars_error::hex_digit_expected );
|
||||
test( L"g", 0, from_chars_error::hex_digit_expected );
|
||||
test( u"g", 0, from_chars_error::hex_digit_expected );
|
||||
test( U"g", 0, from_chars_error::hex_digit_expected );
|
||||
test( u8"g", 0, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "-", 0, from_chars_error::hex_digit_expected );
|
||||
test( L"-", 0, from_chars_error::hex_digit_expected );
|
||||
test( u"-", 0, from_chars_error::hex_digit_expected );
|
||||
test( U"-", 0, from_chars_error::hex_digit_expected );
|
||||
test( u8"-", 0, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "abcdefgh-0000-0000-0000-000000000000", 6, from_chars_error::hex_digit_expected );
|
||||
test( L"abcdefgh-0000-0000-0000-000000000000", 6, from_chars_error::hex_digit_expected );
|
||||
test( u"abcdefgh-0000-0000-0000-000000000000", 6, from_chars_error::hex_digit_expected );
|
||||
test( U"abcdefgh-0000-0000-0000-000000000000", 6, from_chars_error::hex_digit_expected );
|
||||
test( u8"abcdefgh-0000-0000-0000-000000000000", 6, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "Have a nice day", 0, from_chars_error::hex_digit_expected );
|
||||
test( L"Have a nice day", 0, from_chars_error::hex_digit_expected );
|
||||
test( u"Have a nice day", 0, from_chars_error::hex_digit_expected );
|
||||
test( U"Have a nice day", 0, from_chars_error::hex_digit_expected );
|
||||
test( u8"Have a nice day", 0, from_chars_error::hex_digit_expected );
|
||||
|
||||
test( "0000000000000-0000-0000-000000000000", 8, from_chars_error::dash_expected );
|
||||
test( L"0000000000000-0000-0000-000000000000", 8, from_chars_error::dash_expected );
|
||||
test( u"0000000000000-0000-0000-000000000000", 8, from_chars_error::dash_expected );
|
||||
test( U"0000000000000-0000-0000-000000000000", 8, from_chars_error::dash_expected );
|
||||
test( u8"0000000000000-0000-0000-000000000000", 8, from_chars_error::dash_expected );
|
||||
|
||||
test( "00000000-000000000-0000-000000000000", 13, from_chars_error::dash_expected );
|
||||
test( L"00000000-000000000-0000-000000000000", 13, from_chars_error::dash_expected );
|
||||
test( u"00000000-000000000-0000-000000000000", 13, from_chars_error::dash_expected );
|
||||
test( U"00000000-000000000-0000-000000000000", 13, from_chars_error::dash_expected );
|
||||
test( u8"00000000-000000000-0000-000000000000", 13, from_chars_error::dash_expected );
|
||||
|
||||
test( "00000000-0000-000000000-000000000000", 18, from_chars_error::dash_expected );
|
||||
test( L"00000000-0000-000000000-000000000000", 18, from_chars_error::dash_expected );
|
||||
test( u"00000000-0000-000000000-000000000000", 18, from_chars_error::dash_expected );
|
||||
test( U"00000000-0000-000000000-000000000000", 18, from_chars_error::dash_expected );
|
||||
test( u8"00000000-0000-000000000-000000000000", 18, from_chars_error::dash_expected );
|
||||
|
||||
test( "00000000-0000-0000-00000000000000000", 23, from_chars_error::dash_expected );
|
||||
test( L"00000000-0000-0000-00000000000000000", 23, from_chars_error::dash_expected );
|
||||
test( u"00000000-0000-0000-00000000000000000", 23, from_chars_error::dash_expected );
|
||||
test( U"00000000-0000-0000-00000000000000000", 23, from_chars_error::dash_expected );
|
||||
test( u8"00000000-0000-0000-00000000000000000", 23, from_chars_error::dash_expected );
|
||||
|
||||
test( "00000000-0000-0000-0000-000000000000-0000", 36, from_chars_error::unexpected_extra_input );
|
||||
test( L"00000000-0000-0000-0000-000000000000-0000", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u"00000000-0000-0000-0000-000000000000-0000", 36, from_chars_error::unexpected_extra_input );
|
||||
test( U"00000000-0000-0000-0000-000000000000-0000", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u8"00000000-0000-0000-0000-000000000000-0000", 36, from_chars_error::unexpected_extra_input );
|
||||
|
||||
test( "00010203-0405-0607-0809-0a0b0c0d0e0f1011", 36, from_chars_error::unexpected_extra_input );
|
||||
test( L"00010203-0405-0607-0809-0a0b0c0d0e0f1011", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u"00010203-0405-0607-0809-0a0b0c0d0e0f1011", 36, from_chars_error::unexpected_extra_input );
|
||||
test( U"00010203-0405-0607-0809-0a0b0c0d0e0f1011", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u8"00010203-0405-0607-0809-0a0b0c0d0e0f1011", 36, from_chars_error::unexpected_extra_input );
|
||||
|
||||
test( "12345678-90AB-CDEF-1234-567890abcdefghij", 36, from_chars_error::unexpected_extra_input );
|
||||
test( L"12345678-90AB-CDEF-1234-567890abcdefghij", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u"12345678-90AB-CDEF-1234-567890abcdefghij", 36, from_chars_error::unexpected_extra_input );
|
||||
test( U"12345678-90AB-CDEF-1234-567890abcdefghij", 36, from_chars_error::unexpected_extra_input );
|
||||
test( u8"12345678-90AB-CDEF-1234-567890abcdefghij", 36, from_chars_error::unexpected_extra_input );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user