From e04e814b411c06b73309cd21ae2b41da8178059b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Wed, 3 Aug 2022 13:24:44 +0200 Subject: [PATCH 1/6] Refs #15294. Add std::string::compare api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- include/fastrtps/utils/fixed_size_string.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/fastrtps/utils/fixed_size_string.hpp b/include/fastrtps/utils/fixed_size_string.hpp index a2b0aa970fb..f8c4cf0da90 100644 --- a/include/fastrtps/utils/fixed_size_string.hpp +++ b/include/fastrtps/utils/fixed_size_string.hpp @@ -170,6 +170,12 @@ struct fixed_string return string_len; } + int compare( + const std::string& str ) const + { + return strncmp(string_data, str.c_str(), MAX_CHARS); + } + private: void set( From 1f299d35074a73f30d300dc4d76af6f824b37782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Wed, 3 Aug 2022 13:29:51 +0200 Subject: [PATCH 2/6] Refs #15294. Add doxygen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- include/fastrtps/utils/fixed_size_string.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/fastrtps/utils/fixed_size_string.hpp b/include/fastrtps/utils/fixed_size_string.hpp index f8c4cf0da90..48077d88715 100644 --- a/include/fastrtps/utils/fixed_size_string.hpp +++ b/include/fastrtps/utils/fixed_size_string.hpp @@ -170,6 +170,13 @@ struct fixed_string return string_len; } + /*! + * Compare with a std:.string. + * + * @param str std::string to be compared with. + * + * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + */ int compare( const std::string& str ) const { From a4d80e590bd74df1b871b15b1d6f356f89b344c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Wed, 3 Aug 2022 13:33:21 +0200 Subject: [PATCH 3/6] Refs #15294. Fixed typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- include/fastrtps/utils/fixed_size_string.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/fastrtps/utils/fixed_size_string.hpp b/include/fastrtps/utils/fixed_size_string.hpp index 48077d88715..2e02ce5b1a7 100644 --- a/include/fastrtps/utils/fixed_size_string.hpp +++ b/include/fastrtps/utils/fixed_size_string.hpp @@ -171,7 +171,7 @@ struct fixed_string } /*! - * Compare with a std:.string. + * Compare with a std::string. * * @param str std::string to be compared with. * From 6177ad393483fc60a9d615572f8f143471dc87c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Thu, 4 Aug 2022 08:41:31 +0200 Subject: [PATCH 4/6] Refs #15294. Add suggested functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- include/fastrtps/utils/fixed_size_string.hpp | 28 +++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/include/fastrtps/utils/fixed_size_string.hpp b/include/fastrtps/utils/fixed_size_string.hpp index 2e02ce5b1a7..423767398ca 100644 --- a/include/fastrtps/utils/fixed_size_string.hpp +++ b/include/fastrtps/utils/fixed_size_string.hpp @@ -170,6 +170,19 @@ struct fixed_string return string_len; } + /*! + * Compare with a C string. + * + * @param str C string to be compared with. + * + * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + */ + int compare( + const char* str) const noexcept + { + return strncmp(string_data, str, MAX_CHARS); + } + /*! * Compare with a std::string. * @@ -178,7 +191,20 @@ struct fixed_string * @return Integer value with the result of the comparison as described in `std.:string::compare()`. */ int compare( - const std::string& str ) const + const std::string& str) const noexcept + { + return strncmp(string_data, str.c_str(), MAX_CHARS); + } + + /*! + * Compare with a fixed_string + * + * @param str fixed_string to be compared with. + * + * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + */ + template int compare( + const fixed_string& str) const noexcept { return strncmp(string_data, str.c_str(), MAX_CHARS); } From 83e0c96a546641bb12b457d504c14bb1397d77f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Thu, 4 Aug 2022 08:42:01 +0200 Subject: [PATCH 5/6] Refs #15294. Add unit test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- test/unittest/utils/FixedSizeStringTests.cpp | 70 ++++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/test/unittest/utils/FixedSizeStringTests.cpp b/test/unittest/utils/FixedSizeStringTests.cpp index 275d660b4b9..909f070e6dd 100644 --- a/test/unittest/utils/FixedSizeStringTests.cpp +++ b/test/unittest/utils/FixedSizeStringTests.cpp @@ -20,11 +20,12 @@ using namespace eprosima::fastrtps; constexpr size_t MAX_CHARS = 255; constexpr size_t OTHER_MAX_CHARS = 127; -class FixedSizeStringTests: public ::testing::Test +class FixedSizeStringTests : public ::testing::Test { - public: - char const *pattern0 = "foo/bar/baz"; - char const *long_pattern = +public: + + char const* pattern0 = "foo/bar/baz"; + char const* long_pattern = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" @@ -34,7 +35,7 @@ class FixedSizeStringTests: public ::testing::Test "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; - size_t pattern0_len = strlen(pattern0); + size_t pattern0_len = strlen(pattern0); }; TEST_F(FixedSizeStringTests, default_constructor) @@ -166,7 +167,64 @@ TEST_F(FixedSizeStringTests, different_fixed_sizes) ASSERT_EQ(s1, s2); } -int main(int argc, char **argv) +TEST_F(FixedSizeStringTests, comparisons) +{ + const char* c_string_short = "test string"; + const char* c_string_a = "test string a"; + const char* c_string_b = "test string b"; + const char* c_string_c = "test string c"; + const char* c_string_large = "test string b "; + std::string std_string_short = "test string"; + std::string std_string_a = "test string a"; + std::string std_string_b = "test string b"; + std::string std_string_c = "test string c"; + std::string std_string_large = "test string b "; + fixed_string fixed_short = "test string"; + fixed_string fixed_a = "test string a"; + fixed_string fixed_b = "test string b"; + fixed_string fixed_c = "test string c"; + fixed_string fixed_large = "test string b "; + + ASSERT_NE(fixed_b, c_string_short); + ASSERT_NE(fixed_b, c_string_a); + ASSERT_EQ(fixed_b, c_string_b); + ASSERT_NE(fixed_b, c_string_c); + ASSERT_NE(fixed_b, c_string_large); + + ASSERT_LT(0, fixed_b.compare(c_string_short)); + ASSERT_LT(0, fixed_b.compare(c_string_a)); + ASSERT_EQ(0, fixed_b.compare(c_string_b)); + ASSERT_GT(0, fixed_b.compare(c_string_c)); + ASSERT_GT(0, fixed_b.compare(c_string_large)); + + ASSERT_NE(fixed_b, std_string_short); + ASSERT_NE(fixed_b, std_string_a); + ASSERT_EQ(fixed_b, std_string_b); + ASSERT_NE(fixed_b, std_string_c); + ASSERT_NE(fixed_b, std_string_large); + + ASSERT_LE(0, fixed_b.compare(std_string_short)); + ASSERT_LE(0, fixed_b.compare(std_string_a)); + ASSERT_EQ(0, fixed_b.compare(std_string_b)); + ASSERT_GT(0, fixed_b.compare(std_string_c)); + ASSERT_GT(0, fixed_b.compare(std_string_large)); + + ASSERT_NE(fixed_b, fixed_short); + ASSERT_NE(fixed_b, fixed_a); + ASSERT_EQ(fixed_b, fixed_b); + ASSERT_NE(fixed_b, fixed_c); + ASSERT_NE(fixed_b, fixed_large); + + ASSERT_LE(0, fixed_b.compare(fixed_short)); + ASSERT_LE(0, fixed_b.compare(fixed_a)); + ASSERT_EQ(0, fixed_b.compare(fixed_b)); + ASSERT_GT(0, fixed_b.compare(fixed_c)); + ASSERT_GT(0, fixed_b.compare(fixed_large)); +} + +int main( + int argc, + char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 0eec881024902642e42b886dfc32a0a0b1652d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Gonz=C3=A1lez=20Moreno?= Date: Fri, 5 Aug 2022 08:51:59 +0200 Subject: [PATCH 6/6] Refs #15294. Fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ricardo González Moreno --- include/fastrtps/utils/fixed_size_string.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/fastrtps/utils/fixed_size_string.hpp b/include/fastrtps/utils/fixed_size_string.hpp index 423767398ca..2f0db8006b2 100644 --- a/include/fastrtps/utils/fixed_size_string.hpp +++ b/include/fastrtps/utils/fixed_size_string.hpp @@ -175,7 +175,7 @@ struct fixed_string * * @param str C string to be compared with. * - * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + * @return Integer value with the result of the comparison as described in `std::string::compare()`. */ int compare( const char* str) const noexcept @@ -188,7 +188,7 @@ struct fixed_string * * @param str std::string to be compared with. * - * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + * @return Integer value with the result of the comparison as described in `std::string::compare()`. */ int compare( const std::string& str) const noexcept @@ -201,7 +201,7 @@ struct fixed_string * * @param str fixed_string to be compared with. * - * @return Integer value with the result of the comparison as described in `std.:string::compare()`. + * @return Integer value with the result of the comparison as described in `std::string::compare()`. */ template int compare( const fixed_string& str) const noexcept