Skip to content

Commit

Permalink
Add upb_StringView_Compare function.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 703267593
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 5, 2024
1 parent 629aba5 commit aac2600
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions upb/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ cc_library(
deps = ["//upb:port"],
)

cc_test(
name = "string_view_test",
srcs = ["string_view_test.cc"],
deps = [
":base",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

filegroup(
name = "source_files",
srcs = glob(
Expand Down
10 changes: 10 additions & 0 deletions upb/base/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size));
}

// Compares StringViews following strcmp rules.
// Please note this comparison is neither unicode nor locale aware.
UPB_INLINE int upb_StringView_Compare(upb_StringView a, upb_StringView b) {
int result = memcmp(a.data, b.data, UPB_MIN(a.size, b.size));
if (result == 0) {
return a.size - b.size;
}
return result;
}

// LINT.ThenChange(
// GoogleInternalName1,
// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string,
Expand Down
69 changes: 69 additions & 0 deletions upb/base/string_view_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "upb/base/string_view.h"

#include <string>

#include <gtest/gtest.h>

namespace {

TEST(upb_StringView, Compare_Eq) {
std::string s1("12345");
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_EQ(upb_StringView_Compare(h1, h2), 0);
}

TEST(upb_StringView, Compare_Eq_Shorter) {
std::string s1("1234"); // s1 is shorter.
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_LT(upb_StringView_Compare(h1, h2), 0);
}

TEST(upb_StringView, Compare_Eq_Longer) {
std::string s1("123456"); // s1 is longer.
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_GT(upb_StringView_Compare(h1, h2), 0);
}

TEST(upb_StringView, Compare_Less) {
std::string s1("12245"); // 2 < 3
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_LT(upb_StringView_Compare(h1, h2), 0);
}

TEST(upb_StringView, Compare_Greater) {
std::string s1("12445"); // 4 > 3
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_GT(upb_StringView_Compare(h1, h2), 0);
}

TEST(upb_StringView, Compare_Greater_Shorter) {
std::string s1("1244"); // s1 is shorter but 4 > 3.
std::string s2("12345");

upb_StringView h1 = upb_StringView_FromDataAndSize(s1.data(), s1.size());
upb_StringView h2 = upb_StringView_FromDataAndSize(s2.data(), s2.size());

ASSERT_GT(upb_StringView_Compare(h1, h2), 0);
}

} // namespace

0 comments on commit aac2600

Please sign in to comment.