Make StringView cmp compatible with absl

pull/21344/head
Juanli Shen 5 years ago
parent a6507e0bf4
commit 2693d34c30
  1. 24
      src/core/lib/gprpp/string_view.h
  2. 5
      src/core/lib/security/security_connector/ssl_utils.cc
  3. 18
      test/core/gprpp/string_view_test.cc
  4. 4
      test/core/security/credentials_test.cc

@ -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<char> StringViewToCString(const StringView sv) {
return grpc_core::UniquePtr<char>(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 */

@ -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_auth_context> grpc_ssl_peer_to_auth_context(

@ -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) {

@ -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);
}

Loading…
Cancel
Save