Merge cd28d872e7 into e830bc502f
This commit is contained in:
commit
eb44f36e55
@ -33,6 +33,12 @@ SOFTWARE.
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
/* forward declarations */
|
||||||
|
class alt_string;
|
||||||
|
bool operator<(const char* op1, const alt_string& op2);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is virtually a string class.
|
* This is virtually a string class.
|
||||||
* It covers std::string under the hood.
|
* It covers std::string under the hood.
|
||||||
@ -60,17 +66,36 @@ class alt_string
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename op_type>
|
template <typename op_type>
|
||||||
bool operator==(op_type&& op) const
|
typename std::enable_if< // disable for alt_string
|
||||||
|
!std::is_same< alt_string,
|
||||||
|
typename std::remove_reference<op_type>::type
|
||||||
|
>::value,
|
||||||
|
bool>::type
|
||||||
|
operator==(op_type&& op) const
|
||||||
{
|
{
|
||||||
return str_impl == op;
|
return str_impl == op;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const alt_string& op) const
|
||||||
|
{
|
||||||
|
return str_impl == op.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename op_type>
|
template <typename op_type>
|
||||||
bool operator!=(op_type&& op) const
|
typename std::enable_if< // disable for alt_string
|
||||||
|
!std::is_same< alt_string,
|
||||||
|
typename std::remove_reference<op_type>::type
|
||||||
|
>::value,
|
||||||
|
bool>::type
|
||||||
|
operator!=(op_type&& op) const
|
||||||
{
|
{
|
||||||
return str_impl != op;
|
return str_impl != op;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator!=(const alt_string& op) const {
|
||||||
|
return str_impl != op.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
std::size_t size() const noexcept
|
std::size_t size() const noexcept
|
||||||
{
|
{
|
||||||
return str_impl.size();
|
return str_impl.size();
|
||||||
@ -87,7 +112,12 @@ class alt_string
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename op_type>
|
template <typename op_type>
|
||||||
bool operator<(op_type&& op) const
|
typename std::enable_if< // disable for alt_string
|
||||||
|
!std::is_same< alt_string,
|
||||||
|
typename std::remove_reference<op_type>::type
|
||||||
|
>::value,
|
||||||
|
bool>::type
|
||||||
|
operator<(op_type&& op) const
|
||||||
{
|
{
|
||||||
return str_impl < op;
|
return str_impl < op;
|
||||||
}
|
}
|
||||||
@ -134,6 +164,8 @@ class alt_string
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::string str_impl;
|
std::string str_impl;
|
||||||
|
|
||||||
|
friend bool ::operator<(const char*, const alt_string&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -149,6 +181,11 @@ using alt_json = nlohmann::basic_json <
|
|||||||
nlohmann::adl_serializer >;
|
nlohmann::adl_serializer >;
|
||||||
|
|
||||||
|
|
||||||
|
bool operator<(const char* op1, const alt_string& op2) {
|
||||||
|
return op1 < op2.str_impl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("alternative string type")
|
TEST_CASE("alternative string type")
|
||||||
{
|
{
|
||||||
@ -210,4 +247,26 @@ TEST_CASE("alternative string type")
|
|||||||
alt_string dump = doc.dump();
|
alt_string dump = doc.dump();
|
||||||
CHECK(dump == R"({"foo":"bar"})");
|
CHECK(dump == R"({"foo":"bar"})");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("equality")
|
||||||
|
{
|
||||||
|
alt_json doc;
|
||||||
|
doc["Who are you?"] = "I'm Batman";
|
||||||
|
|
||||||
|
CHECK("I'm Batman" == doc["Who are you?"]);
|
||||||
|
CHECK(doc["Who are you?"] == "I'm Batman");
|
||||||
|
|
||||||
|
CHECK("I'm Bruce Wayne" != doc["Who are you?"]);
|
||||||
|
CHECK(doc["Who are you?"] != "I'm Bruce Wayne");
|
||||||
|
|
||||||
|
{
|
||||||
|
const alt_json& const_doc = doc;
|
||||||
|
|
||||||
|
CHECK("I'm Batman" == const_doc["Who are you?"]);
|
||||||
|
CHECK(const_doc["Who are you?"] == "I'm Batman");
|
||||||
|
|
||||||
|
CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]);
|
||||||
|
CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user