From 2693d34c3054a0926132c5f4e09a22a439d84c5e Mon Sep 17 00:00:00 2001 From: Juanli Shen Date: Mon, 2 Dec 2019 14:29:27 -0800 Subject: [PATCH] Make StringView cmp compatible with absl --- src/core/lib/gprpp/string_view.h | 24 +++++++++++-------- .../security/security_connector/ssl_utils.cc | 5 ++-- test/core/gprpp/string_view_test.cc | 18 +++++++------- test/core/security/credentials_test.cc | 4 ++-- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/core/lib/gprpp/string_view.h b/src/core/lib/gprpp/string_view.h index e2e082e3ea5..5b03616c280 100644 --- a/src/core/lib/gprpp/string_view.h +++ b/src/core/lib/gprpp/string_view.h @@ -121,6 +121,16 @@ class StringView final { size()); } + // Compares with other. + inline int compare(StringView other) { + const size_t len = GPR_MIN(size(), other.size()); + const int ret = strncmp(data(), other.data(), len); + if (ret != 0) return ret; + if (size() == other.size()) return 0; + if (size() < other.size()) return -1; + return 1; + } + private: const char* ptr_; size_t size_; @@ -133,6 +143,10 @@ inline bool operator==(StringView lhs, StringView rhs) { inline bool operator!=(StringView lhs, StringView rhs) { return !(lhs == rhs); } +inline bool operator<(StringView lhs, StringView rhs) { + return lhs.compare(rhs) < 0; +} + #endif // GRPC_USE_ABSL // Converts grpc_slice to StringView. @@ -150,16 +164,6 @@ inline grpc_core::UniquePtr StringViewToCString(const StringView sv) { return grpc_core::UniquePtr(str); } -// Compares lhs and rhs. -inline int StringViewCmp(const StringView lhs, const StringView rhs) { - const size_t len = GPR_MIN(lhs.size(), rhs.size()); - const int ret = strncmp(lhs.data(), rhs.data(), len); - if (ret != 0) return ret; - if (lhs.size() == rhs.size()) return 0; - if (lhs.size() < rhs.size()) return -1; - return 1; -} - } // namespace grpc_core #endif /* GRPC_CORE_LIB_GPRPP_STRING_VIEW_H */ diff --git a/src/core/lib/security/security_connector/ssl_utils.cc b/src/core/lib/security/security_connector/ssl_utils.cc index e6695915a7a..f4bb70bef8d 100644 --- a/src/core/lib/security/security_connector/ssl_utils.cc +++ b/src/core/lib/security/security_connector/ssl_utils.cc @@ -189,10 +189,9 @@ int grpc_ssl_cmp_target_name( grpc_core::StringView target_name, grpc_core::StringView other_target_name, grpc_core::StringView overridden_target_name, grpc_core::StringView other_overridden_target_name) { - int c = grpc_core::StringViewCmp(target_name, other_target_name); + int c = target_name.compare(other_target_name); if (c != 0) return c; - return grpc_core::StringViewCmp(overridden_target_name, - other_overridden_target_name); + return overridden_target_name.compare(other_overridden_target_name); } grpc_core::RefCountedPtr grpc_ssl_peer_to_auth_context( diff --git a/test/core/gprpp/string_view_test.cc b/test/core/gprpp/string_view_test.cc index d0a1d3cb724..a48967ee170 100644 --- a/test/core/gprpp/string_view_test.cc +++ b/test/core/gprpp/string_view_test.cc @@ -104,15 +104,15 @@ TEST(StringViewTest, Cmp) { grpc_core::StringView str1(kStr1); grpc_core::StringView str2(kStr2); grpc_core::StringView str3(kStr3); - EXPECT_EQ(grpc_core::StringViewCmp(str1, str1), 0); - EXPECT_LT(grpc_core::StringViewCmp(str1, str2), 0); - EXPECT_LT(grpc_core::StringViewCmp(str1, str3), 0); - EXPECT_EQ(grpc_core::StringViewCmp(str2, str2), 0); - EXPECT_GT(grpc_core::StringViewCmp(str2, str1), 0); - EXPECT_GT(grpc_core::StringViewCmp(str2, str3), 0); - EXPECT_EQ(grpc_core::StringViewCmp(str3, str3), 0); - EXPECT_GT(grpc_core::StringViewCmp(str3, str1), 0); - EXPECT_LT(grpc_core::StringViewCmp(str3, str2), 0); + EXPECT_EQ(str1.compare(str1), 0); + EXPECT_LT(str1.compare(str2), 0); + EXPECT_LT(str1.compare(str3), 0); + EXPECT_EQ(str2.compare(str2), 0); + EXPECT_GT(str2.compare(str1), 0); + EXPECT_GT(str2.compare(str3), 0); + EXPECT_EQ(str3.compare(str3), 0); + EXPECT_GT(str3.compare(str1), 0); + EXPECT_LT(str3.compare(str2), 0); } TEST(StringViewTest, RemovePrefix) { diff --git a/test/core/security/credentials_test.cc b/test/core/security/credentials_test.cc index 099cdd7aa67..440009a44b8 100644 --- a/test/core/security/credentials_test.cc +++ b/test/core/security/credentials_test.cc @@ -752,8 +752,8 @@ static void test_valid_sts_creds_options(void) { grpc_core::StringView host; grpc_core::StringView port; GPR_ASSERT(grpc_core::SplitHostPort(sts_url->authority, &host, &port)); - GPR_ASSERT(grpc_core::StringViewCmp(host, "foo.com") == 0); - GPR_ASSERT(grpc_core::StringViewCmp(port, "5555") == 0); + GPR_ASSERT(host == "foo.com"); + GPR_ASSERT(port == "5555"); grpc_uri_destroy(sts_url); }