Skip to content

Commit 500cd57

Browse files
Merge pull request #37 from tusharpm/string_view
Support string_view parameters
2 parents 260b1f1 + 36778b4 commit 500cd57

File tree

4 files changed

+209
-96
lines changed

4 files changed

+209
-96
lines changed

.travis.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
language: cpp
22
compiler: gcc
3-
dist: trusty
3+
4+
addons:
5+
apt:
6+
sources:
7+
- ubuntu-toolchain-r-test
8+
packages:
9+
- cppcheck
10+
- g++-8
11+
- uuid-dev
412

513
before_install:
6-
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
7-
- sudo apt-get update -qq
814
- sudo apt-cache search libuuid
915

1016
install:
11-
- sudo apt-get install -qq g++-8
1217
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 90
13-
- sudo apt-get install -qq cppcheck
14-
- sudo apt-get install uuid-dev
1518

1619
before_script:
1720
- cd ${TRAVIS_BUILD_DIR}
18-
- cmake -H. -BBuild -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -Wdev
21+
- cmake -H. -BBuild -DCMAKE_BUILD_TYPE=Release -Wdev
1922
- cd Build
2023

2124
script:

include/uuid.h

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

test/test_generators.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,49 @@ TEST_CASE("Test name generator (std::string)", "[gen][name]")
254254
REQUIRE(id3 != id4);
255255
}
256256

257+
TEST_CASE("Test name generator (std::string_view)", "[gen][name]")
258+
{
259+
using namespace std::string_view_literals;
260+
261+
uuids::uuid_name_generator dgen(uuids::uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value());
262+
auto id1 = dgen("john"sv);
263+
REQUIRE(!id1.is_nil());
264+
REQUIRE(id1.version() == uuids::uuid_version::name_based_sha1);
265+
REQUIRE(id1.variant() == uuids::uuid_variant::rfc);
266+
267+
auto id2 = dgen("jane"sv);
268+
REQUIRE(!id2.is_nil());
269+
REQUIRE(id2.version() == uuids::uuid_version::name_based_sha1);
270+
REQUIRE(id2.variant() == uuids::uuid_variant::rfc);
271+
272+
auto id3 = dgen("jane"sv);
273+
REQUIRE(!id3.is_nil());
274+
REQUIRE(id3.version() == uuids::uuid_version::name_based_sha1);
275+
REQUIRE(id3.variant() == uuids::uuid_variant::rfc);
276+
277+
auto id4 = dgen(L"jane"sv);
278+
REQUIRE(!id4.is_nil());
279+
REQUIRE(id4.version() == uuids::uuid_version::name_based_sha1);
280+
REQUIRE(id4.variant() == uuids::uuid_variant::rfc);
281+
282+
REQUIRE(id1 != id2);
283+
REQUIRE(id2 == id3);
284+
REQUIRE(id3 != id4);
285+
}
286+
287+
TEST_CASE("Test name generator equality (char const*, std::string, std::string_view)", "[gen][name]")
288+
{
289+
using namespace std::literals;
290+
291+
uuids::uuid_name_generator dgen(uuids::uuid::from_string("47183823-2574-4bfd-b411-99ed177d3e43").value());
292+
auto id1 = dgen("john");
293+
auto id2 = dgen("john"s);
294+
auto id3 = dgen("john"sv);
295+
296+
REQUIRE(id1 == id2);
297+
REQUIRE(id2 == id3);
298+
}
299+
257300
#ifdef _WIN32
258301
TEST_CASE("Test time generator", "[gen][time]")
259302
{

0 commit comments

Comments
 (0)