Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String/StringView/UUIDValueに三方比較演算子を実装 #664

Merged
merged 2 commits into from Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions Siv3D/include/Siv3D/String.hpp
Expand Up @@ -10,6 +10,9 @@
//-----------------------------------------------

# pragma once
# if __has_include(<compare>)
# include <compare>
# endif
# include <string>
# include "Common.hpp"
# include "StringView.hpp"
Expand Down Expand Up @@ -1129,13 +1132,21 @@ namespace s3d
/// @return 新しい文字列
String xml_escaped() const;

friend bool operator ==(const String& lhs, const value_type* rhs);

friend bool operator ==(const String& lhs, const String& rhs) noexcept;
#if __cpp_impl_three_way_comparison

friend bool operator ==(const value_type* lhs, const String& rhs);
bool operator == (const String& rhs) const noexcept = default;

friend bool operator ==(const String& lhs, const value_type* rhs);
std::strong_ordering operator <=> (const String & rhs) const noexcept = default;

friend std::strong_ordering operator <=> (const String & lhs, const value_type * rhs);

#else

friend bool operator ==(const String & lhs, const String & rhs) noexcept;

friend bool operator ==(const value_type * lhs, const String & rhs);

friend bool operator !=(const String& lhs, const String& rhs) noexcept;

Expand Down Expand Up @@ -1171,6 +1182,7 @@ namespace s3d

friend bool operator >=(const String& lhs, const value_type* rhs);

#endif

friend String operator +(const value_type lhs, const String& rhs);

Expand Down
12 changes: 12 additions & 0 deletions Siv3D/include/Siv3D/StringView.hpp
Expand Up @@ -10,6 +10,9 @@
//-----------------------------------------------

# pragma once
# if __has_include(<compare>)
# include <compare>
# endif
# include <iosfwd>
# include <string_view>
# include <string>
Expand Down Expand Up @@ -262,6 +265,13 @@ namespace s3d
[[nodiscard]]
uint64 hash() const noexcept;

#if __cpp_impl_three_way_comparison

[[nodiscard]]
constexpr std::strong_ordering operator <=> (const StringView& rhs) const noexcept = default;

#else

[[nodiscard]]
friend constexpr bool operator ==(StringView lhs, StringView rhs) noexcept
{
Expand Down Expand Up @@ -298,6 +308,8 @@ namespace s3d
return (lhs.compare(rhs) >= 0);
}

#endif

friend std::ostream& operator <<(std::ostream& output, const StringView& value);

friend std::wostream& operator <<(std::wostream& output, const StringView& value);
Expand Down
12 changes: 12 additions & 0 deletions Siv3D/include/Siv3D/UUIDValue.hpp
Expand Up @@ -11,6 +11,9 @@

# pragma once
# include <array>
# if __has_include(<compare>)
# include <compare>
# endif
# include "Common.hpp"
# include "StringView.hpp"
# include "Optional.hpp"
Expand Down Expand Up @@ -77,6 +80,13 @@ namespace s3d
[[nodiscard]]
size_t hash() const noexcept;

#if __cpp_impl_three_way_comparison

[[nodiscard]]
std::strong_ordering operator <=>(const UUIDValue& rhs) const noexcept = default;

#else

[[nodiscard]]
friend bool operator ==(const UUIDValue& lhs, const UUIDValue& rhs) noexcept
{
Expand All @@ -95,6 +105,8 @@ namespace s3d
return (lhs.m_data < rhs.m_data);
}

#endif

[[nodiscard]]
static UUIDValue Generate();

Expand Down
20 changes: 15 additions & 5 deletions Siv3D/src/Siv3D/String/SivString.cpp
Expand Up @@ -384,6 +384,20 @@ namespace s3d
return new_string;
}

bool operator ==(const String& lhs, const String::value_type* rhs)
{
return (lhs.str() == rhs);
}

#if __cpp_impl_three_way_comparison

std::strong_ordering operator <=> (const String& lhs, const String::value_type* rhs)
{
return lhs.str() <=> rhs;
}

#else

bool operator ==(const String& lhs, const String& rhs) noexcept
{
return (lhs.m_string == rhs.m_string);
Expand All @@ -394,11 +408,6 @@ namespace s3d
return (lhs == rhs.str());
}

bool operator ==(const String& lhs, const String::value_type* rhs)
{
return (lhs.str() == rhs);
}

bool operator !=(const String& lhs, const String& rhs) noexcept
{
return (lhs.m_string != rhs.m_string);
Expand Down Expand Up @@ -473,6 +482,7 @@ namespace s3d
{
return (lhs.str() >= rhs);
}
#endif

String operator +(const String::value_type lhs, const String& rhs)
{
Expand Down