const references and function templates

This commit is contained in:
Marius Bancila 2019-01-09 11:25:14 +02:00
parent 58c7031c33
commit 7a0f71cb10
3 changed files with 55 additions and 9 deletions

View File

@ -86,6 +86,7 @@ Based on this feedback the following changes have been done in this version:
* removed `to_wstring()` and made `to_string()` a function template
* made `from_string()` a non-throwing function template returning `std::optional<std::uuid>`
* added `is_valid_uuid()` a non-throwing function template that checks if a string contains a valid uuid
* removed the `std::wstring` overloaded call operator for `uuid_name_generator` and replaced with with function templates
* `uuid`s produced from names in different character sets or encodings are different (i.e. "jane" and L"jane")
* removed the class `uuid_error`
* footnote on the use of the name Microsoft
@ -529,8 +530,13 @@ namespace std {
public:
explicit uuid_name_generator(uuid const& namespace_uuid) noexcept;
uuid operator()(std::string_view name);
uuid operator()(std::wstring_view name);
template<class CharT = char>
uuid operator()(CharT const * name);
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
uuid operator()(std::basic_string<CharT, Traits, Allocator> const & name);
};
}
```

View File

@ -410,7 +410,7 @@ namespace uuids
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
static bool is_valid_uuid(std::basic_string<CharT, Traits, Allocator>& str) noexcept
static bool is_valid_uuid(std::basic_string<CharT, Traits, Allocator> const & str) noexcept
{
return is_valid_uuid(str.c_str());
}
@ -469,7 +469,7 @@ namespace uuids
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
static std::optional<uuid> from_string(std::basic_string<CharT, Traits, Allocator>& str) noexcept
static std::optional<uuid> from_string(std::basic_string<CharT, Traits, Allocator> const & str) noexcept
{
return from_string(str.c_str());
}
@ -677,14 +677,24 @@ namespace uuids
: nsuuid(namespace_uuid)
{}
uuid operator()(std::string_view name)
template<class CharT = char>
uuid operator()(CharT const * name)
{
size_t size = 0;
if constexpr (std::is_same_v<CharT, char>)
size = strlen(name);
else
size = wcslen(name);
reset();
process_characters(name.data(), name.size());
process_characters(name, size);
return make_uuid();
}
uuid operator()(std::wstring_view name)
template<class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>>
uuid operator()(std::basic_string<CharT, Traits, Allocator> const & name)
{
reset();
process_characters(name.data(), name.size());
@ -739,7 +749,7 @@ namespace uuids
private:
uuid nsuuid;
detail::sha1 hasher;
};
};
}
namespace std

View File

@ -154,7 +154,7 @@ TEST_CASE("Test basic random generator (conversion ctor w/ ref) w/ ranlux48_base
REQUIRE(id1 != id2);
}
TEST_CASE("Test name generator", "[gen][name]")
TEST_CASE("Test name generator (char*)", "[gen][name]")
{
uuids::uuid_name_generator dgen(uuids::uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value());
auto id1 = dgen("john");
@ -181,3 +181,33 @@ TEST_CASE("Test name generator", "[gen][name]")
REQUIRE(id2 == id3);
REQUIRE(id3 != id4);
}
TEST_CASE("Test name generator (std::string)", "[gen][name]")
{
using namespace std::string_literals;
uuids::uuid_name_generator dgen(uuids::uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value());
auto id1 = dgen("john"s);
REQUIRE(!id1.is_nil());
REQUIRE(id1.version() == uuids::uuid_version::name_based_sha1);
REQUIRE(id1.variant() == uuids::uuid_variant::rfc);
auto id2 = dgen("jane"s);
REQUIRE(!id2.is_nil());
REQUIRE(id2.version() == uuids::uuid_version::name_based_sha1);
REQUIRE(id2.variant() == uuids::uuid_variant::rfc);
auto id3 = dgen("jane"s);
REQUIRE(!id3.is_nil());
REQUIRE(id3.version() == uuids::uuid_version::name_based_sha1);
REQUIRE(id3.variant() == uuids::uuid_variant::rfc);
auto id4 = dgen(L"jane"s);
REQUIRE(!id4.is_nil());
REQUIRE(id4.version() == uuids::uuid_version::name_based_sha1);
REQUIRE(id4.variant() == uuids::uuid_variant::rfc);
REQUIRE(id1 != id2);
REQUIRE(id2 == id3);
REQUIRE(id3 != id4);
}