From 7a0f71cb10a60f6aa5f1e99b47104d7fefd6a217 Mon Sep 17 00:00:00 2001 From: Marius Bancila Date: Wed, 9 Jan 2019 11:25:14 +0200 Subject: [PATCH] const references and function templates --- P0959.md | 10 ++++++++-- include/uuid.h | 22 ++++++++++++++++------ test/test_generators.cpp | 32 +++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/P0959.md b/P0959.md index ae92e26..2345c60 100644 --- a/P0959.md +++ b/P0959.md @@ -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` * 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 + uuid operator()(CharT const * name); + + template, + class Allocator = std::allocator> + uuid operator()(std::basic_string const & name); }; } ``` diff --git a/include/uuid.h b/include/uuid.h index 56b3e8d..a6921e6 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -410,7 +410,7 @@ namespace uuids template, class Allocator = std::allocator> - static bool is_valid_uuid(std::basic_string& str) noexcept + static bool is_valid_uuid(std::basic_string const & str) noexcept { return is_valid_uuid(str.c_str()); } @@ -469,7 +469,7 @@ namespace uuids template, class Allocator = std::allocator> - static std::optional from_string(std::basic_string& str) noexcept + static std::optional from_string(std::basic_string 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 + uuid operator()(CharT const * name) { + size_t size = 0; + if constexpr (std::is_same_v) + 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 Allocator = std::allocator> + uuid operator()(std::basic_string const & name) { reset(); process_characters(name.data(), name.size()); @@ -739,7 +749,7 @@ namespace uuids private: uuid nsuuid; detail::sha1 hasher; - }; + }; } namespace std diff --git a/test/test_generators.cpp b/test/test_generators.cpp index 8aa5185..64c0ec8 100644 --- a/test/test_generators.cpp +++ b/test/test_generators.cpp @@ -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); +} \ No newline at end of file