TEST(StringCompare, Performance) { const char* volatile s1 = g_s1; const char* volatile s2 = g_s2; int total = 0; for (int i = 0; i < 1000000; ++i) total += CompareByteString(s1, sizeof(g_s1) - 1, s2, sizeof(g_s2) - 1); volatile int n = total; (void) n; }
int StringPiece::compare(const StringPiece& x) const { #if 0 return CompareByteString(m_ptr, m_length, x.m_ptr, x.m_length); #else // unoptimized code int r = memcmp(m_ptr, x.m_ptr, m_length < x.m_length ? m_length : x.m_length); if (r != 0) return r; if (m_length < x.m_length) return -1; else if (m_length > x.m_length) return 1; return 0; #endif }
TEST(StringCompare, ByteString) { std::string str1 = "abcdefgXXXXX"; std::string str2 = "abcdefg"; std::string str3 = "abcdefgYYYYY"; bool inclusive; ASSERT_GT(CompareByteString( str1.data(), str1.length(), str2.data(), str2.length(), &inclusive), 0); ASSERT_TRUE(inclusive); ASSERT_GT(CompareByteString(str1, str2), 0); ASSERT_LT(CompareByteString(str2, str1), 0); ASSERT_EQ(CompareByteString(str2, str2), 0); ASSERT_LT(CompareByteString(str1, str3), 0); ASSERT_GT(CompareByteString(str3, str2), 0); ASSERT_EQ(7U, GetCommonPrefixLength(str1, str3)); }