From 2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Tue, 1 Jul 2025 17:07:04 +0100 Subject: add nihil.std --- nihil.uuid/CMakeLists.txt | 1 + nihil.uuid/test.cc | 334 +++++++++++++--------------------- nihil.uuid/uuid.ccm | 446 +++++++++++++++++++--------------------------- 3 files changed, 315 insertions(+), 466 deletions(-) (limited to 'nihil.uuid') diff --git a/nihil.uuid/CMakeLists.txt b/nihil.uuid/CMakeLists.txt index f82d308..a210322 100644 --- a/nihil.uuid/CMakeLists.txt +++ b/nihil.uuid/CMakeLists.txt @@ -1,6 +1,7 @@ # This source code is released into the public domain. add_library(nihil.uuid STATIC) +target_link_libraries(nihil.uuid PRIVATE nihil.std) target_sources(nihil.uuid PUBLIC FILE_SET modules TYPE CXX_MODULES FILES uuid.ccm diff --git a/nihil.uuid/test.cc b/nihil.uuid/test.cc index 0f21298..551c491 100644 --- a/nihil.uuid/test.cc +++ b/nihil.uuid/test.cc @@ -2,17 +2,17 @@ * From https://github.com/mariusbancila/stduuid * * Copyright (c) 2017 - * + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: - * + * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -22,45 +22,35 @@ * IN THE SOFTWARE. */ -#include -#include -#include -#include - #include -//NOLINTBEGIN(bugprone-unchecked-optional-access) +import nihil.std; +import nihil.uuid; -namespace -{ +// NOLINTBEGIN(bugprone-unchecked-optional-access) +namespace { // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0205r0.html template -void seed_rng(EngineT& engine) +void seed_rng(EngineT &engine) { using engine_type = typename EngineT::result_type; using device_type = std::random_device::result_type; using seedseq_type = std::seed_seq::result_type; constexpr auto bytes_needed = StateSize * sizeof(engine_type); - constexpr auto numbers_needed = - (sizeof(device_type) < sizeof(seedseq_type)) - ? (bytes_needed / sizeof(device_type)) - : (bytes_needed / sizeof(seedseq_type)); + constexpr auto numbers_needed = (sizeof(device_type) < sizeof(seedseq_type)) + ? (bytes_needed / sizeof(device_type)) + : (bytes_needed / sizeof(seedseq_type)); auto numbers = std::array{}; auto rnddev = std::random_device{}; std::ranges::generate(numbers, std::ref(rnddev)); - auto seedseq = std::seed_seq(std::cbegin(numbers), - std::cend(numbers)); + auto seedseq = std::seed_seq(std::cbegin(numbers), std::cend(numbers)); engine.seed(seedseq); } -} // anonymous namespace - -import nihil.uuid; - using namespace nihil; TEST_CASE("uuid: Test multiple default generators", "[uuid]") @@ -70,12 +60,12 @@ TEST_CASE("uuid: Test multiple default generators", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - std::mt19937 generator(seq); + std::mt19937 generator(seq); - id1 = uuid_random_generator{ generator }(); + id1 = uuid_random_generator{generator}(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -83,12 +73,12 @@ TEST_CASE("uuid: Test multiple default generators", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - std::mt19937 generator(seq); + std::mt19937 generator(seq); - id2 = uuid_random_generator{ generator }(); + id2 = uuid_random_generator{generator}(); REQUIRE(!id2.is_nil()); REQUIRE(id2.version() == uuid_version::random_number_based); REQUIRE(id2.variant() == uuid_variant::rfc); @@ -100,10 +90,10 @@ TEST_CASE("uuid: Test multiple default generators", "[uuid]") TEST_CASE("uuid: Test default generator", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - std::mt19937 generator(seq); + std::mt19937 generator(seq); uuid const guid = uuid_random_generator{generator}(); REQUIRE(!guid.is_nil()); @@ -111,17 +101,16 @@ TEST_CASE("uuid: Test default generator", "[uuid]") REQUIRE(guid.variant() == uuid_variant::rfc); } -TEST_CASE("uuid: Test random generator (conversion ctor w/ smart ptr)", - "[uuid]") +TEST_CASE("uuid: Test random generator (conversion ctor w/ smart ptr)", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - std::mt19937 generator(seq); + std::mt19937 generator(seq); uuid_random_generator dgen(&generator); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -137,13 +126,13 @@ TEST_CASE("uuid: Test random generator (conversion ctor w/ smart ptr)", TEST_CASE("uuid: Test random generator (conversion ctor w/ ptr)", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - auto generator = std::make_unique(seq); + auto generator = std::make_unique(seq); uuid_random_generator dgen(generator.get()); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -159,13 +148,13 @@ TEST_CASE("uuid: Test random generator (conversion ctor w/ ptr)", "[uuid]") TEST_CASE("uuid: Test random generator (conversion ctor w/ ref)", "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - std::mt19937 generator(seq); + std::mt19937 generator(seq); uuid_random_generator dgen(generator); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -179,16 +168,17 @@ TEST_CASE("uuid: Test random generator (conversion ctor w/ ref)", "[uuid]") } TEST_CASE("uuid: Test basic random generator (conversion ctor w/ ptr) " - "w/ ranlux48_base", "[uuid]") + "w/ ranlux48_base", + "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); - std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); + std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::ranlux48_base generator(seq); basic_uuid_random_generator dgen(&generator); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -202,16 +192,17 @@ TEST_CASE("uuid: Test basic random generator (conversion ctor w/ ptr) " } TEST_CASE("uuid: Test basic random generator (conversion ctor w/ smart ptr) " - "w/ ranlux48_base", "[uuid]") + "w/ ranlux48_base", + "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - auto generator = std::make_unique(seq); + auto generator = std::make_unique(seq); basic_uuid_random_generator dgen(generator.get()); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -225,16 +216,17 @@ TEST_CASE("uuid: Test basic random generator (conversion ctor w/ smart ptr) " } TEST_CASE("uuid: Test basic random generator (conversion ctor w/ ref) " - "w/ ranlux48_base", "[uuid]") + "w/ ranlux48_base", + "[uuid]") { std::random_device rd; - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); - std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); + std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); std::ranlux48_base generator(seq); basic_uuid_random_generator dgen(generator); - auto id1 = dgen(); + auto id1 = dgen(); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::random_number_based); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -289,7 +281,7 @@ TEST_CASE("uuid: Test name generator (std::string)", "[uuid]") using namespace std::string_literals; uuid_name_generator dgen(uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value()); - auto id1 = dgen("john"s); + auto id1 = dgen("john"s); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::name_based_sha1); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -319,7 +311,7 @@ TEST_CASE("uuid: Test name generator (std::string_view)", "[uuid]") using namespace std::string_view_literals; uuid_name_generator dgen(uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value()); - auto id1 = dgen("john"sv); + auto id1 = dgen("john"sv); REQUIRE(!id1.is_nil()); REQUIRE(id1.version() == uuid_version::name_based_sha1); REQUIRE(id1.variant() == uuid_variant::rfc); @@ -345,12 +337,13 @@ TEST_CASE("uuid: Test name generator (std::string_view)", "[uuid]") } TEST_CASE("uuid: Test name generator equality (char const*, std::string, " - "std::string_view)", "[uuid]") + "std::string_view)", + "[uuid]") { using namespace std::literals; - auto dgen = uuid_name_generator(uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e43").value()); + auto dgen = uuid_name_generator( + uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value()); auto id1 = dgen("john"); auto id2 = dgen("john"s); auto id3 = dgen("john"sv); @@ -368,30 +361,20 @@ TEST_CASE("uuid: Test default constructor", "[uuid]") TEST_CASE("uuid: Test string conversion", "[uuid]") { auto empty = uuid(); - REQUIRE(to_string(empty) == - "00000000-0000-0000-0000-000000000000"); - REQUIRE(to_string(empty) == - L"00000000-0000-0000-0000-000000000000"); + REQUIRE(to_string(empty) == "00000000-0000-0000-0000-000000000000"); + REQUIRE(to_string(empty) == L"00000000-0000-0000-0000-000000000000"); } TEST_CASE("uuid: Test is_valid_uuid(char*)", "[uuid]") { - REQUIRE(uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e43")); - REQUIRE(uuid::is_valid_uuid( - "{47183823-2574-4bfd-b411-99ed177d3e43}")); - REQUIRE(uuid::is_valid_uuid( - L"47183823-2574-4bfd-b411-99ed177d3e43")); - REQUIRE(uuid::is_valid_uuid( - L"{47183823-2574-4bfd-b411-99ed177d3e43}")); - REQUIRE(uuid::is_valid_uuid( - "00000000-0000-0000-0000-000000000000")); - REQUIRE(uuid::is_valid_uuid( - "{00000000-0000-0000-0000-000000000000}")); - REQUIRE(uuid::is_valid_uuid( - L"00000000-0000-0000-0000-000000000000")); - REQUIRE(uuid::is_valid_uuid( - L"{00000000-0000-0000-0000-000000000000}")); + REQUIRE(uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e43")); + REQUIRE(uuid::is_valid_uuid("{47183823-2574-4bfd-b411-99ed177d3e43}")); + REQUIRE(uuid::is_valid_uuid(L"47183823-2574-4bfd-b411-99ed177d3e43")); + REQUIRE(uuid::is_valid_uuid(L"{47183823-2574-4bfd-b411-99ed177d3e43}")); + REQUIRE(uuid::is_valid_uuid("00000000-0000-0000-0000-000000000000")); + REQUIRE(uuid::is_valid_uuid("{00000000-0000-0000-0000-000000000000}")); + REQUIRE(uuid::is_valid_uuid(L"00000000-0000-0000-0000-000000000000")); + REQUIRE(uuid::is_valid_uuid(L"{00000000-0000-0000-0000-000000000000}")); } TEST_CASE("uuid: Test is_valid_uuid(basic_string)", "[uuid]") @@ -443,36 +426,24 @@ TEST_CASE("uuid: Test is_valid_uuid(basic_string_view)", "[uuid]") { using namespace std::string_view_literals; - REQUIRE(uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e43"sv)); - REQUIRE(uuid::is_valid_uuid( - "{47183823-2574-4bfd-b411-99ed177d3e43}"sv)); - REQUIRE(uuid::is_valid_uuid( - L"47183823-2574-4bfd-b411-99ed177d3e43"sv)); - REQUIRE(uuid::is_valid_uuid( - L"{47183823-2574-4bfd-b411-99ed177d3e43}"sv)); - REQUIRE(uuid::is_valid_uuid( - "00000000-0000-0000-0000-000000000000"sv)); - REQUIRE(uuid::is_valid_uuid( - "{00000000-0000-0000-0000-000000000000}"sv)); - REQUIRE(uuid::is_valid_uuid( - L"00000000-0000-0000-0000-000000000000"sv)); - REQUIRE(uuid::is_valid_uuid( - L"{00000000-0000-0000-0000-000000000000}"sv)); + REQUIRE(uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e43"sv)); + REQUIRE(uuid::is_valid_uuid("{47183823-2574-4bfd-b411-99ed177d3e43}"sv)); + REQUIRE(uuid::is_valid_uuid(L"47183823-2574-4bfd-b411-99ed177d3e43"sv)); + REQUIRE(uuid::is_valid_uuid(L"{47183823-2574-4bfd-b411-99ed177d3e43}"sv)); + REQUIRE(uuid::is_valid_uuid("00000000-0000-0000-0000-000000000000"sv)); + REQUIRE(uuid::is_valid_uuid("{00000000-0000-0000-0000-000000000000}"sv)); + REQUIRE(uuid::is_valid_uuid(L"00000000-0000-0000-0000-000000000000"sv)); + REQUIRE(uuid::is_valid_uuid(L"{00000000-0000-0000-0000-000000000000}"sv)); } TEST_CASE("uuid: Test is_valid_uuid(char*) invalid format", "[uuid]") { REQUIRE(!uuid::is_valid_uuid("")); REQUIRE(!uuid::is_valid_uuid("{}")); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e4")); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e430")); - REQUIRE(!uuid::is_valid_uuid( - "{47183823-2574-4bfd-b411-99ed177d3e43")); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e43}")); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e4")); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e430")); + REQUIRE(!uuid::is_valid_uuid("{47183823-2574-4bfd-b411-99ed177d3e43")); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e43}")); } TEST_CASE("uuid: Test is_valid_uuid(basic_string) invalid format", "[uuid]") @@ -510,21 +481,16 @@ TEST_CASE("uuid: Test is_valid_uuid(basic_string) invalid format", "[uuid]") } } -TEST_CASE("uuid: Test is_valid_uuid(basic_string_view) invalid format", - "[uuid]") +TEST_CASE("uuid: Test is_valid_uuid(basic_string_view) invalid format", "[uuid]") { using namespace std::string_view_literals; REQUIRE(!uuid::is_valid_uuid(""sv)); REQUIRE(!uuid::is_valid_uuid("{}"sv)); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e4"sv)); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e430"sv)); - REQUIRE(!uuid::is_valid_uuid( - "{47183823-2574-4bfd-b411-99ed177d3e43"sv)); - REQUIRE(!uuid::is_valid_uuid( - "47183823-2574-4bfd-b411-99ed177d3e43}"sv)); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e4"sv)); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e430"sv)); + REQUIRE(!uuid::is_valid_uuid("{47183823-2574-4bfd-b411-99ed177d3e43"sv)); + REQUIRE(!uuid::is_valid_uuid("47183823-2574-4bfd-b411-99ed177d3e43}"sv)); } TEST_CASE("uuid: Test from_string(char*)", "[uuid]") @@ -714,14 +680,10 @@ TEST_CASE("uuid: Test from_string(char*) invalid format", "[uuid]") { REQUIRE(!uuid::from_string("").has_value()); REQUIRE(!uuid::from_string("{}").has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e4").has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e430").has_value()); - REQUIRE(!uuid::from_string( - "{47183823-2574-4bfd-b411-99ed177d3e43").has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e43}").has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e4").has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e430").has_value()); + REQUIRE(!uuid::from_string("{47183823-2574-4bfd-b411-99ed177d3e43").has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43}").has_value()); } TEST_CASE("uuid: Test from_string(basic_string) invalid format", "[uuid]") @@ -765,14 +727,10 @@ TEST_CASE("uuid: Test from_string(basic_string_view) invalid format", "[uuid]") REQUIRE(!uuid::from_string(""sv).has_value()); REQUIRE(!uuid::from_string("{}"sv).has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e4"sv).has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e430"sv).has_value()); - REQUIRE(!uuid::from_string( - "{47183823-2574-4bfd-b411-99ed177d3e43"sv).has_value()); - REQUIRE(!uuid::from_string( - "47183823-2574-4bfd-b411-99ed177d3e43}"sv).has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e4"sv).has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e430"sv).has_value()); + REQUIRE(!uuid::from_string("{47183823-2574-4bfd-b411-99ed177d3e43"sv).has_value()); + REQUIRE(!uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43}"sv).has_value()); } TEST_CASE("uuid: Test iterators constructor", "[uuid]") @@ -780,31 +738,22 @@ TEST_CASE("uuid: Test iterators constructor", "[uuid]") using namespace std::string_literals; { - std::array arr{{ - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }}; + std::array arr{ + {0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, + 0x17, 0x7d, 0x3e, 0x43} + }; - uuid guid(std::begin(arr), std::end(arr)); - REQUIRE(to_string(guid) == - "47183823-2574-4bfd-b411-99ed177d3e43"s); + auto const guid = uuid(std::begin(arr), std::end(arr)); + REQUIRE(to_string(guid) == "47183823-2574-4bfd-b411-99ed177d3e43"s); } { - uuid::value_type arr[16] = { // NOLINT - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }; + uuid::value_type arr[16] = {// NOLINT + 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, + 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43}; - uuid guid(std::begin(arr), std::end(arr)); - REQUIRE(to_string(guid) == - "47183823-2574-4bfd-b411-99ed177d3e43"s); + auto const guid = uuid(std::begin(arr), std::end(arr)); + REQUIRE(to_string(guid) == "47183823-2574-4bfd-b411-99ed177d3e43"s); } } @@ -813,44 +762,31 @@ TEST_CASE("uuid: Test array constructors", "[uuid]") using namespace std::string_literals; { - uuid guid{{ - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }}; + auto const guid = uuid { + {0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, + 0x17, 0x7d, 0x3e, 0x43} + }; - REQUIRE(to_string(guid) == - "47183823-2574-4bfd-b411-99ed177d3e43"s); + REQUIRE(to_string(guid) == "47183823-2574-4bfd-b411-99ed177d3e43"s); } { - std::array arr{{ - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }}; + auto arr = std::array{ + {0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, + 0x17, 0x7d, 0x3e, 0x43} + }; - uuid guid(arr); - REQUIRE(to_string(guid) == - "47183823-2574-4bfd-b411-99ed177d3e43"s); + auto const guid = uuid(arr); + REQUIRE(to_string(guid) == "47183823-2574-4bfd-b411-99ed177d3e43"s); } { - uuid::value_type arr[16] { //NOLINT - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }; + uuid::value_type arr[16]{// NOLINT + 0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, + 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43}; - uuid guid(arr); - REQUIRE(to_string(guid) == - "47183823-2574-4bfd-b411-99ed177d3e43"s); + auto const guid = uuid(arr); + REQUIRE(to_string(guid) == "47183823-2574-4bfd-b411-99ed177d3e43"s); } } @@ -874,21 +810,15 @@ TEST_CASE("Test comparison", "[uuid]") auto engine = uuid_random_generator::engine_type{}; seed_rng(engine); - uuid_random_generator gen{engine}; + auto gen = uuid_random_generator{engine}; auto id = gen(); REQUIRE(empty < id); - std::set ids{ - uuid{}, - gen(), - gen(), - gen(), - gen() - }; + auto ids = std::set{uuid{}, gen(), gen(), gen(), gen()}; REQUIRE(ids.size() == 5); - REQUIRE(ids.find(uuid{}) != ids.end()); + REQUIRE(ids.contains(uuid{}) == true); } TEST_CASE("uuid: Test hashing", "[uuid]") @@ -904,15 +834,9 @@ TEST_CASE("uuid: Test hashing", "[uuid]") auto engine = uuid_random_generator::engine_type{}; seed_rng(engine); - uuid_random_generator gen{ engine }; - - std::unordered_set ids{ - uuid{}, - gen(), - gen(), - gen(), - gen() - }; + uuid_random_generator gen{engine}; + + std::unordered_set ids{uuid{}, gen(), gen(), gen(), gen()}; REQUIRE(ids.size() == 5); REQUIRE(ids.find(uuid{}) != ids.end()); @@ -950,7 +874,7 @@ TEST_CASE("uuid: Test constexpr", "[uuid]") TEST_CASE("uuid: Test size", "[uuid]") { - REQUIRE(sizeof(uuid) == 16); + REQUIRE(sizeof(uuid) == 16); } TEST_CASE("uuid: Test assignment", "[uuid]") @@ -973,16 +897,13 @@ TEST_CASE("uuid: Test trivial", "[uuid]") TEST_CASE("uuid: Test as_bytes", "[uuid]") { - std::array arr{{ - 0x47, 0x18, 0x38, 0x23, - 0x25, 0x74, - 0x4b, 0xfd, - 0xb4, 0x11, - 0x99, 0xed, 0x17, 0x7d, 0x3e, 0x43 - }}; + std::array arr{ + {0x47, 0x18, 0x38, 0x23, 0x25, 0x74, 0x4b, 0xfd, 0xb4, 0x11, 0x99, 0xed, 0x17, 0x7d, + 0x3e, 0x43} + }; { - uuid id{ arr }; + uuid id{arr}; REQUIRE(!id.is_nil()); auto view = id.as_bytes(); @@ -990,12 +911,13 @@ TEST_CASE("uuid: Test as_bytes", "[uuid]") } { - const uuid id{ arr }; + const uuid id{arr}; REQUIRE(!id.is_nil()); auto view = id.as_bytes(); REQUIRE(memcmp(view.data(), arr.data(), arr.size()) == 0); } } +} // anonymous namespace -//NOLINTEND(bugprone-unchecked-optional-access) +// NOLINTEND(bugprone-unchecked-optional-access) diff --git a/nihil.uuid/uuid.ccm b/nihil.uuid/uuid.ccm index 4aa424e..a7a4770 100644 --- a/nihil.uuid/uuid.ccm +++ b/nihil.uuid/uuid.ccm @@ -1,66 +1,42 @@ -/* - * From https://github.com/mariusbancila/stduuid - * - * Copyright (c) 2017 - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -module; - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +// From https://github.com/mariusbancila/stduuid +// +// Copyright (c) 2017 +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. export module nihil.uuid; +import nihil.std; + namespace nihil { template [[nodiscard]] constexpr auto hex2char(TChar const ch) noexcept -> unsigned char { if (ch >= static_cast('0') && ch <= static_cast('9')) - return static_cast( - ch - static_cast('0')); + return static_cast(ch - static_cast('0')); if (ch >= static_cast('a') && ch <= static_cast('f')) - return static_cast( - 10 + ch - static_cast('a')); + return static_cast(10 + ch - static_cast('a')); if (ch >= static_cast('A') && ch <= static_cast('F')) - return static_cast( - 10 + ch - static_cast('A')); + return static_cast(10 + ch - static_cast('A')); return 0; } @@ -68,17 +44,14 @@ template template [[nodiscard]] constexpr auto is_hex(TChar const ch) noexcept -> bool { - return (ch >= static_cast('0') && - ch <= static_cast('9')) || - (ch >= static_cast('a') && - ch <= static_cast('f')) || - (ch >= static_cast('A') && - ch <= static_cast('F')); + return (ch >= static_cast('0') && ch <= static_cast('9')) || + (ch >= static_cast('a') && ch <= static_cast('f')) || + (ch >= static_cast('A') && ch <= static_cast('F')); } template -[[nodiscard]] constexpr auto to_string_view(TChar const *str) noexcept - -> std::basic_string_view +[[nodiscard]] constexpr auto +to_string_view(TChar const *str) noexcept -> std::basic_string_view { if (str) return str; @@ -87,9 +60,7 @@ template template [[nodiscard]] constexpr auto to_string_view(StringType const &str) noexcept - -> std::basic_string_view< - typename StringType::value_type, - typename StringType::traits_type> + -> std::basic_string_view { return str; } @@ -106,9 +77,8 @@ struct sha1 reset(); } - [[nodiscard]] inline static auto - left_rotate(std::uint32_t value, std::size_t const count) noexcept - -> std::uint32_t + [[nodiscard]] static auto + left_rotate(std::uint32_t value, std::size_t const count) noexcept -> std::uint32_t { return (value << count) ^ (value >> (32 - count)); } @@ -126,7 +96,7 @@ struct sha1 auto process_byte(this sha1 &self, std::uint8_t octet) -> void { - self.m_block[self.m_blockByteIndex++] = octet; + self.m_block.at(self.m_blockByteIndex++) = octet; ++self.m_byteCount; if (self.m_blockByteIndex == block_bytes) { @@ -135,13 +105,10 @@ struct sha1 } } - auto process_block(this sha1 &self, - void const *const start, - void const *const end) - -> void + auto process_block(this sha1 &self, void const *const start, void const *const end) -> void { - auto *first = static_cast(start); - auto *last = static_cast(end); + auto const *first = static_cast(start); + auto const *last = static_cast(end); while (first != last) { self.process_byte(*first); @@ -149,12 +116,9 @@ struct sha1 } } - auto process_bytes(this sha1 &self, - void const *const data, - size_t const len) - -> void + auto process_bytes(this sha1 &self, void const *const data, std::size_t const len) -> void { - auto *block = static_cast(data); + auto *block = static_cast(data); self.process_block(block, block + len); } @@ -178,14 +142,10 @@ struct sha1 self.process_byte(0); self.process_byte(0); self.process_byte(0); - self.process_byte(static_cast( - (bit_count >> 24) & 0xFF)); - self.process_byte(static_cast( - (bit_count >> 16) & 0xFF)); - self.process_byte(static_cast( - (bit_count >> 8) & 0xFF)); - self.process_byte(static_cast( - (bit_count) & 0xFF)); + self.process_byte(static_cast((bit_count >> 24U) & 0xFFU)); + self.process_byte(static_cast((bit_count >> 16U) & 0xFFU)); + self.process_byte(static_cast((bit_count >> 8U) & 0xFFU)); + self.process_byte(static_cast((bit_count) & 0xFFU)); return self.m_digest; } @@ -193,57 +153,50 @@ struct sha1 auto get_digest_bytes(this sha1 &self) -> digest8_t { auto d32 = self.get_digest(); - auto digest = digest8_t{}; - - auto di = std::size_t{0}; - - digest[di++] = static_cast(d32[0] >> 24); - digest[di++] = static_cast(d32[0] >> 16); - digest[di++] = static_cast(d32[0] >> 8); - digest[di++] = static_cast(d32[0] >> 0); - digest[di++] = static_cast(d32[1] >> 24); - digest[di++] = static_cast(d32[1] >> 16); - digest[di++] = static_cast(d32[1] >> 8); - digest[di++] = static_cast(d32[1] >> 0); - - digest[di++] = static_cast(d32[2] >> 24); - digest[di++] = static_cast(d32[2] >> 16); - digest[di++] = static_cast(d32[2] >> 8); - digest[di++] = static_cast(d32[2] >> 0); - - digest[di++] = static_cast(d32[3] >> 24); - digest[di++] = static_cast(d32[3] >> 16); - digest[di++] = static_cast(d32[3] >> 8); - digest[di++] = static_cast(d32[3] >> 0); - - digest[di++] = static_cast(d32[4] >> 24); - digest[di++] = static_cast(d32[4] >> 16); - digest[di++] = static_cast(d32[4] >> 8); - digest[di++] = static_cast(d32[4] >> 0); - - return digest; + return { + static_cast(d32[0] >> 24U), + static_cast(d32[0] >> 16U), + static_cast(d32[0] >> 8U), + static_cast(d32[0] >> 0U), + + static_cast(d32[1] >> 24U), + static_cast(d32[1] >> 16U), + static_cast(d32[1] >> 8U), + static_cast(d32[1] >> 0U), + + static_cast(d32[2] >> 24U), + static_cast(d32[2] >> 16U), + static_cast(d32[2] >> 8U), + static_cast(d32[2] >> 0U), + + static_cast(d32[3] >> 24U), + static_cast(d32[3] >> 16U), + static_cast(d32[3] >> 8U), + static_cast(d32[3] >> 0U), + + static_cast(d32[4] >> 24U), + static_cast(d32[4] >> 16U), + static_cast(d32[4] >> 8U), + static_cast(d32[4] >> 0U), + }; } private: auto process_block(this sha1 &self) -> void { - auto w = std::array(); + auto w = std::array{}; for (std::size_t i = 0; i < 16; i++) { - w[i] = static_cast( - self.m_block[i * 4 + 0]) << 24; - w[i] |= static_cast( - self.m_block[i * 4 + 1]) << 16; - w[i] |= static_cast( - self.m_block[i * 4 + 2]) << 8; - w[i] |= static_cast( - self.m_block[i * 4 + 3]); + w.at(i) = static_cast(self.m_block.at((i * 4) + 0)) << 24U; + w.at(i) |= static_cast(self.m_block.at((i * 4) + 1)) << 16U; + w.at(i) |= static_cast(self.m_block.at((i * 4) + 2)) << 8U; + w.at(i) |= static_cast(self.m_block.at((i * 4) + 3)); } for (std::size_t i = 16; i < 80; i++) { - w[i] = left_rotate((w[i - 3] ^ w[i - 8] ^ - w[i - 14] ^ w[i - 16]), 1); + w.at(i) = left_rotate( + (w.at(i - 3) ^ w.at(i - 8) ^ w.at(i - 14) ^ w.at(i - 16)), 1); } auto a = self.m_digest[0]; @@ -270,8 +223,7 @@ private: k = 0xCA62C1D6; } - auto temp = std::uint32_t{left_rotate(a, 5) - + f + e + k + w[i]}; + auto temp = std::uint32_t{left_rotate(a, 5) + f + e + k + w.at(i)}; e = d; d = c; c = left_rotate(b, 30); @@ -286,19 +238,17 @@ private: self.m_digest[4] += e; } - digest32_t m_digest; - std::array m_block; - std::size_t m_blockByteIndex; - std::size_t m_byteCount; + digest32_t m_digest{}; + std::array m_block{}; + std::size_t m_blockByteIndex{}; + std::size_t m_byteCount{}; }; template -inline constexpr std::string_view empty_guid = - "00000000-0000-0000-0000-000000000000"; +inline constexpr std::string_view empty_guid = "00000000-0000-0000-0000-000000000000"; template <> -inline constexpr std::wstring_view empty_guid - = L"00000000-0000-0000-0000-000000000000"; +inline constexpr std::wstring_view empty_guid = L"00000000-0000-0000-0000-000000000000"; template inline constexpr std::string_view guid_encoder = "0123456789abcdef"; @@ -345,19 +295,19 @@ inline constexpr std::wstring_view guid_encoder = L"0123456789abcdef"; // indicated by a bit pattern in octet 8, marked with N in // xxxxxxxx-xxxx-xxxx-Nxxx-xxxxxxxxxxxx -export enum struct uuid_variant { +export enum struct uuid_variant : std::uint8_t { // NCS backward compatibility (with the obsolete Apollo Network // Computing System 1.5 UUID format). // N bit pattern: 0xxx // > the first 6 octets of the UUID are a 48-bit timestamp (the number // of 4 microsecond units of time since 1 Jan 1980 UTC); // > the next 2 octets are reserved; - // > the next octet is the "address family"; + // > the next octet is the "address family"; // > the final 7 octets are a 56-bit host ID in the form specified by // the address family ncs, - // RFC 4122/DCE 1.1 + // RFC 4122/DCE 1.1 // N bit pattern: 10xx // > big-endian byte order rfc, @@ -365,17 +315,17 @@ export enum struct uuid_variant { // Microsoft Corporation backward compatibility // N bit pattern: 110x // > little endian byte order - // > formely used in the Component Object Model (COM) library + // > formely used in the Component Object Model (COM) library microsoft, // reserved for possible future definition - // N bit pattern: 111x + // N bit pattern: 111x reserved }; // indicated by a bit pattern in octet 6, marked with M in -// xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx -export enum struct uuid_version { +// xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx +export enum struct uuid_version : std::uint8_t { // only possible for nil or invalid uuids none = 0, // The time-based version specified in RFC 4122 @@ -393,25 +343,25 @@ export enum struct uuid_version { // Forward declare uuid and to_string so that we can declare to_string as a // friend later. export struct uuid; -export template , - typename Allocator = std::allocator> +export template , + typename Allocator = std::allocator> auto to_string(uuid const &id) -> std::basic_string; // -------------------------------------------------------------------------------------------------------------------------- // uuid class // -------------------------------------------------------------------------------------------------------------------------- -export struct uuid { +export struct uuid +{ using value_type = std::uint8_t; constexpr uuid() noexcept = default; - uuid(value_type(&arr)[16]) noexcept // NOLINT + uuid(value_type (&arr)[16]) noexcept // NOLINT { std::ranges::copy(arr, std::ranges::begin(data)); } - constexpr uuid(std::array const &arr) noexcept + explicit constexpr uuid(std::array const &arr) noexcept : data{arr} { } @@ -428,7 +378,7 @@ export struct uuid { std::ranges::copy(bytes, std::ranges::begin(data)); } - template + template explicit uuid(ForwardIterator first, ForwardIterator last) { if (std::distance(first, last) != 16) @@ -439,11 +389,11 @@ export struct uuid { [[nodiscard]] constexpr auto variant() const noexcept -> uuid_variant { - if ((data[8] & 0x80) == 0x00) + if ((data[8] & 0x80U) == 0x00U) return uuid_variant::ncs; - else if ((data[8] & 0xC0) == 0x80) + else if ((data[8] & 0xC0U) == 0x80U) return uuid_variant::rfc; - else if ((data[8] & 0xE0) == 0xC0) + else if ((data[8] & 0xE0U) == 0xC0U) return uuid_variant::microsoft; else return uuid_variant::reserved; @@ -451,15 +401,15 @@ export struct uuid { [[nodiscard]] constexpr auto version() const noexcept -> uuid_version { - if ((data[6] & 0xF0) == 0x10) + if ((data[6] & 0xF0U) == 0x10U) return uuid_version::time_based; - else if ((data[6] & 0xF0) == 0x20) + else if ((data[6] & 0xF0U) == 0x20U) return uuid_version::dce_security; - else if ((data[6] & 0xF0) == 0x30) + else if ((data[6] & 0xF0U) == 0x30U) return uuid_version::name_based_md5; - else if ((data[6] & 0xF0) == 0x40) + else if ((data[6] & 0xF0U) == 0x40U) return uuid_version::random_number_based; - else if ((data[6] & 0xF0) == 0x50) + else if ((data[6] & 0xF0U) == 0x50U) return uuid_version::name_based_sha1; else return uuid_version::none; @@ -467,10 +417,7 @@ export struct uuid { [[nodiscard]] constexpr auto is_nil() const noexcept -> bool { - for (auto i : data) - if (i != 0) - return false; - return true; + return std::ranges::all_of(data, [](auto i) { return i == 0; }); } auto swap(uuid &other) noexcept -> void @@ -478,18 +425,14 @@ export struct uuid { data.swap(other.data); } - [[nodiscard]] inline auto as_bytes() const - -> std::span + [[nodiscard]] auto as_bytes() const -> std::span { return std::span( - reinterpret_cast(data.data()), - 16); + reinterpret_cast(data.data()), 16); } template - [[nodiscard]] constexpr static auto - is_valid_uuid(StringType const &in_str) noexcept - -> bool + [[nodiscard]] constexpr static auto is_valid_uuid(StringType const &in_str) noexcept -> bool { auto str = to_string_view(in_str); auto firstDigit = true; @@ -505,7 +448,7 @@ export struct uuid { if (hasBraces && str.back() != '}') return false; - for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) { + for (std::size_t i = hasBraces; i < str.size() - hasBraces; ++i) { if (str[i] == '-') continue; @@ -528,15 +471,14 @@ export struct uuid { template [[nodiscard]] constexpr static auto - from_string(StringType const & in_str) noexcept - -> std::optional + from_string(StringType const &in_str) -> std::optional { auto str = to_string_view(in_str); bool firstDigit = true; - size_t hasBraces = 0; - size_t index = 0; + auto hasBraces = std::size_t{0}; + auto index = std::size_t{0}; - std::array data{ { 0 } }; + auto data = std::array{}; if (str.empty()) return {}; @@ -546,7 +488,7 @@ export struct uuid { if (hasBraces && str.back() != '}') return {}; - for (size_t i = hasBraces; i < str.size() - hasBraces; ++i) { + for (std::size_t i = hasBraces; i < str.size() - hasBraces; ++i) { if (str[i] == '-') continue; @@ -555,10 +497,11 @@ export struct uuid { } if (firstDigit) { - data[index] = static_cast(hex2char(str[i]) << 4); + data.at(index) = static_cast(hex2char(str[i]) << 4); firstDigit = false; } else { - data[index] = static_cast(data[index] | hex2char(str[i])); + data.at(index) = + static_cast(data.at(index) | hex2char(str[i])); index++; firstDigit = true; } @@ -572,19 +515,17 @@ export struct uuid { } private: - std::array data{ { 0 } }; + std::array data{{0}}; friend auto operator==(uuid const &, uuid const &) noexcept -> bool; friend auto operator<(uuid const &, uuid const &) noexcept -> bool; template - friend auto operator<<(std::basic_ostream &s, - uuid const &id) + friend auto operator<<(std::basic_ostream &s, uuid const &id) -> std::basic_ostream &; - template - friend auto to_string(uuid const &id) - -> std::basic_string; + template + friend auto to_string(uuid const &id) -> std::basic_string; friend std::hash; }; @@ -594,36 +535,35 @@ private: // -------------------------------------------------------------------------------------------------------------------------- export [[nodiscard]] -auto operator== (uuid const &lhs, uuid const &rhs) noexcept -> bool +auto operator==(uuid const &lhs, uuid const &rhs) noexcept -> bool { return lhs.data == rhs.data; } -export [[nodiscard]] -auto operator!= (uuid const &lhs, uuid const &rhs) noexcept -> bool +export [[nodiscard]] +auto operator!=(uuid const &lhs, uuid const &rhs) noexcept -> bool { return !(lhs == rhs); } export [[nodiscard]] -auto operator< (uuid const &lhs, uuid const &rhs) noexcept -> bool +auto operator<(uuid const &lhs, uuid const &rhs) noexcept -> bool { return lhs.data < rhs.data; } export template -[[nodiscard]] auto to_string(uuid const &id) - -> std::basic_string +[[nodiscard]] auto to_string(uuid const &id) -> std::basic_string { - auto uustr = std::basic_string( - std::from_range, empty_guid); + auto uustr = + std::basic_string(std::from_range, empty_guid); for (std::size_t i = 0, index = 0; i < 36; ++i) { if (i == 8 || i == 13 || i == 18 || i == 23) continue; - uustr[i] = guid_encoder[id.data[index] >> 4 & 0x0f]; - uustr[++i] = guid_encoder[id.data[index] & 0x0f]; + uustr[i] = guid_encoder[id.data.at(index) >> 4U & 0x0FU]; + uustr[++i] = guid_encoder[id.data.at(index) & 0x0FU]; index++; } @@ -634,13 +574,12 @@ export template auto operator<<(std::basic_ostream &s, uuid const &id) -> std::basic_ostream & { - s << to_string(id); - return s; + return s << to_string(id); } export auto swap(uuid &lhs, uuid &rhs) noexcept -> void { - lhs.swap(rhs); + lhs.swap(rhs); } /*********************************************************************** @@ -648,31 +587,31 @@ export auto swap(uuid &lhs, uuid &rhs) noexcept -> void */ // Name string is a fully-qualified domain name -export uuid uuid_namespace_dns{{ - 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, - 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 -}}; +export uuid uuid_namespace_dns{ + {0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8} +}; // Name string is a URL -export uuid uuid_namespace_url{{ - 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, - 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 -}}; +export uuid uuid_namespace_url{ + {0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8} +}; // Name string is an ISO OID (See https://oidref.com/, // https://en.wikipedia.org/wiki/Object_identifier) -export uuid uuid_namespace_oid{{ - 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, - 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 -}}; +export uuid uuid_namespace_oid{ + {0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8} +}; // Name string is an X.500 DN, in DER or a text output format (See // https://en.wikipedia.org/wiki/X.500, // https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One) -export uuid uuid_namespace_x500{{ - 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, - 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 -}}; +export uuid uuid_namespace_x500{ + {0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, + 0xc8} +}; /*********************************************************************** * uuid generators @@ -695,20 +634,16 @@ struct basic_uuid_random_generator [[nodiscard]] auto operator()() -> uuid { - alignas(std::uint32_t) auto bytes = std::array{}; - - for (int i = 0; i < 16; i += 4) - *reinterpret_cast(bytes.data() + i) = - distribution(*generator); + std::ranges::generate(bytes, [&] { return distribution(*generator); }); // variant must be 10xxxxxx - bytes[8] &= 0xBF; - bytes[8] |= 0x80; + bytes[8] &= 0xBFU; + bytes[8] |= 0x80U; // version must be 0100xxxx - bytes[6] &= 0x4F; - bytes[6] |= 0x40; + bytes[6] &= 0x4FU; + bytes[6] |= 0x40U; return uuid{std::begin(bytes), std::end(bytes)}; } @@ -749,18 +684,14 @@ private: } template - auto process_characters( - std::basic_string_view const str) -> void + auto process_characters(std::basic_string_view const str) -> void { for (std::uint32_t c : str) { - hasher.process_byte(static_cast(c & 0xFF)); + hasher.process_byte(static_cast(c & 0xFFU)); if constexpr (!std::is_same_v) { - hasher.process_byte( - static_cast((c >> 8) & 0xFF)); - hasher.process_byte( - static_cast((c >> 16) & 0xFF)); - hasher.process_byte( - static_cast((c >> 24) & 0xFF)); + hasher.process_byte(static_cast((c >> 8U) & 0xFFU)); + hasher.process_byte(static_cast((c >> 16U) & 0xFFU)); + hasher.process_byte(static_cast((c >> 24U) & 0xFFU)); } } } @@ -770,17 +701,16 @@ private: auto digest = hasher.get_digest_bytes(); // variant must be 0b10xxxxxx - digest[8] &= 0xBF; - digest[8] |= 0x80; + digest[8] &= 0xBFU; + digest[8] |= 0x80U; // version must be 0b0101xxxx - digest[6] &= 0x5F; - digest[6] |= 0x50; + digest[6] &= 0x5FU; + digest[6] |= 0x50U; return uuid(std::span(digest).subspan(0, 16)); } -private: uuid nsuuid; sha1 hasher; }; @@ -791,15 +721,14 @@ private: export auto random_uuid() -> uuid { auto rd = std::random_device(); - auto seed_data = std::array {}; + auto seed_data = std::array{}; std::ranges::generate(seed_data, std::ref(rd)); - auto seq = std::seed_seq(std::ranges::begin(seed_data), - std::ranges::end(seed_data)); + auto seq = std::seed_seq(std::ranges::begin(seed_data), std::ranges::end(seed_data)); auto generator = std::mt19937(seq); auto gen = uuid_random_generator{generator}; - return gen(); + return gen(); } } // namespace nihil @@ -810,30 +739,27 @@ export template <> struct hash { using argument_type = nihil::uuid; - using result_type = std::size_t; + using result_type = std::size_t; - [[nodiscard]] auto operator()(argument_type const &uuid) const - -> result_type + [[nodiscard]] auto operator()(argument_type const &uuid) const noexcept -> result_type { - std::uint64_t l = - static_cast(uuid.data[0]) << 56 | - static_cast(uuid.data[1]) << 48 | - static_cast(uuid.data[2]) << 40 | - static_cast(uuid.data[3]) << 32 | - static_cast(uuid.data[4]) << 24 | - static_cast(uuid.data[5]) << 16 | - static_cast(uuid.data[6]) << 8 | - static_cast(uuid.data[7]); - - std::uint64_t h = - static_cast(uuid.data[8]) << 56 | - static_cast(uuid.data[9]) << 48 | - static_cast(uuid.data[10]) << 40 | - static_cast(uuid.data[11]) << 32 | - static_cast(uuid.data[12]) << 24 | - static_cast(uuid.data[13]) << 16 | - static_cast(uuid.data[14]) << 8 | - static_cast(uuid.data[15]); + auto const l = static_cast(uuid.data[0]) << 56U | + static_cast(uuid.data[1]) << 48U | + static_cast(uuid.data[2]) << 40U | + static_cast(uuid.data[3]) << 32U | + static_cast(uuid.data[4]) << 24U | + static_cast(uuid.data[5]) << 16U | + static_cast(uuid.data[6]) << 8U | + static_cast(uuid.data[7]); + + auto const h = static_cast(uuid.data[8]) << 56U | + static_cast(uuid.data[9]) << 48U | + static_cast(uuid.data[10]) << 40U | + static_cast(uuid.data[11]) << 32U | + static_cast(uuid.data[12]) << 24U | + static_cast(uuid.data[13]) << 16U | + static_cast(uuid.data[14]) << 8U | + static_cast(uuid.data[15]); return std::hash{}(l ^ h); } -- cgit v1.2.3