Added LiteralString helper class
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
class LiteralString
|
||||
{
|
||||
public:
|
||||
constexpr LiteralString() noexcept = default;
|
||||
template<std::size_t N>
|
||||
constexpr LiteralString(const char(&str)[N]) noexcept : _str{ str, N - 1 } { static_assert(N > 0); }
|
||||
|
||||
constexpr const char* c_str() const noexcept { return _str.data(); }
|
||||
constexpr std::size_t length() const noexcept { return _str.length(); }
|
||||
constexpr std::string_view str() const noexcept { return _str; }
|
||||
constexpr auto operator<=>(const LiteralString& other) const = default;
|
||||
|
||||
private:
|
||||
std::string_view _str;
|
||||
};
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<LiteralString>
|
||||
{
|
||||
size_t operator()(const LiteralString& str) const
|
||||
{
|
||||
return hash<std::string_view>{}(str.str());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
struct LiteralStringHash
|
||||
{
|
||||
using hash_type = std::hash<std::string_view>;
|
||||
using is_transparent = void;
|
||||
|
||||
[[nodiscard]] size_t operator()(const LiteralString& str) const {
|
||||
return hash_type{}(str.str());
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t operator()(std::string_view str) const {
|
||||
return hash_type{}(str);
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t operator()(const std::string& str) const {
|
||||
return hash_type{}(str);
|
||||
}
|
||||
};
|
||||
|
||||
struct LiteralStringEqual
|
||||
{
|
||||
using is_transparent = void;
|
||||
|
||||
[[nodiscard]] bool operator()(const LiteralString& lhs, const LiteralString& rhs) const {
|
||||
return lhs == rhs;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator()(const LiteralString& lhs, const std::string& rhs) const {
|
||||
return lhs.str() == rhs;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator()(const LiteralString& lhs, std::string_view rhs) const {
|
||||
return lhs.str() == rhs;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator()(const std::string_view& lhs, LiteralString rhs) const {
|
||||
return lhs == rhs.str();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator()(const std::string& lhs, const LiteralString& rhs) const {
|
||||
return lhs == rhs.str();
|
||||
}
|
||||
};
|
||||
@@ -2,6 +2,7 @@ include(GoogleTest)
|
||||
|
||||
add_executable(test-utils
|
||||
EnumSet.cpp
|
||||
LiteralString.cpp
|
||||
Path.cpp
|
||||
RecursiveSharedMutex.cpp
|
||||
String.cpp
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <unordered_map>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "utils/LiteralString.hpp"
|
||||
|
||||
TEST(LiteralString, ctr)
|
||||
{
|
||||
{
|
||||
constexpr LiteralString foo{ "abc" };
|
||||
static_assert(foo.length() == 3);
|
||||
static_assert(foo == "abc");
|
||||
static_assert(foo == LiteralString{ "abc" });
|
||||
static_assert(foo < LiteralString{ "abcd" });
|
||||
static_assert(foo > LiteralString{ "aac" });
|
||||
|
||||
EXPECT_EQ(::strlen(foo.c_str()), 3);
|
||||
EXPECT_EQ(foo, LiteralString{ "abc" });
|
||||
EXPECT_GT(foo, LiteralString{ "abb" });
|
||||
EXPECT_LT(foo, LiteralString{ "abcd" });
|
||||
}
|
||||
|
||||
{
|
||||
constexpr LiteralString foo{ "" };
|
||||
static_assert(foo.length() == 0);
|
||||
static_assert(foo == "");
|
||||
static_assert(foo == LiteralString{ "" });
|
||||
static_assert(foo < LiteralString{"a"});
|
||||
EXPECT_EQ(::strlen(foo.c_str()), 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(LiteralString, unordered_map)
|
||||
{
|
||||
{
|
||||
std::unordered_map<LiteralString, int> myMap{ {"abc", 42} };
|
||||
|
||||
EXPECT_TRUE(myMap.contains("abc"));
|
||||
EXPECT_TRUE(myMap.contains(LiteralString{ "abc" }));
|
||||
EXPECT_FALSE(myMap.contains("abcd"));
|
||||
}
|
||||
|
||||
{
|
||||
std::unordered_map<LiteralString, int, LiteralStringHash, LiteralStringEqual> myMap{ {"abc", 42} };
|
||||
EXPECT_TRUE(myMap.contains(std::string{ "abc" }));
|
||||
EXPECT_TRUE(myMap.contains(std::string_view{ "abc" }));
|
||||
EXPECT_FALSE(myMap.contains(std::string{ "abcd" }));
|
||||
EXPECT_FALSE(myMap.contains(std::string_view{ "abcd" }));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user