From 9b443c9a4dc8846ff1cc255abee19909800343cb Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 17 Jan 2025 14:06:30 -0500 Subject: [PATCH] add utility function to compare two string while ignoring whitespace --- doc/src/Developer_utils.rst | 3 +++ src/utils.cpp | 23 +++++++++++++++++++++++ src/utils.h | 8 ++++++++ unittest/utils/test_utils.cpp | 16 ++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index 402ed680f7..2f9dce86d4 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -166,6 +166,9 @@ and parsing files or arguments. .. doxygenfunction:: split_lines :project: progguide +.. doxygenfunction:: strsame + :project: progguide + .. doxygenfunction:: strmatch :project: progguide diff --git a/src/utils.cpp b/src/utils.cpp index 90d165202a..b72a9f1be3 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -111,6 +111,29 @@ bool utils::strmatch(const std::string &text, const std::string &pattern) return (pos >= 0); } +bool utils::strsame(const std::string &text1, const std::string &text2) +{ + const char *ptr1 = text1.c_str(); + const char *ptr2 = text2.c_str(); + + while (*ptr1 && *ptr2) { + + // ignore whitespace + while (*ptr1 && isblank(*ptr1)) ++ptr1; + while (*ptr2 && isblank(*ptr2)) ++ptr2; + + // strings differ + if (*ptr1 != *ptr2) return false; + + // reached end of both strings + if (!*ptr1 && !*ptr2) return true; + + ++ptr1; + ++ptr2; + } + return true; +} + /** This function is a companion function to utils::strmatch(). Arguments * and logic is the same, but instead of a boolean, it returns the * sub-string that matches the regex pattern. There can be only one match. diff --git a/src/utils.h b/src/utils.h index 1854c07cf5..1d1ce27de5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -41,6 +41,14 @@ namespace utils { bool strmatch(const std::string &text, const std::string &pattern); + /*! Compare two string while ignoring whitespace + * + * \param text1 the first text to be compared + * \param text2 the second text to be compared + * \return true if the non-whitespace part of the two strings matches, false if not */ + + bool strsame(const std::string &text1, const std::string &text2); + /*! Find sub-string that matches a simplified regex pattern * * \param text the text to be matched against the pattern diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 510fcb0198..2a46de43f7 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -46,6 +46,22 @@ TEST(Utils, strdup) delete[] copy2; } +TEST(Utils, strsame) +{ + std::string text1("some_text"); + std::string text2("some_text"); + ASSERT_TRUE(utils::strsame(text1,text2)); + text1 = " some _\ttext\n "; + ASSERT_TRUE(utils::strsame(text1,text2)); + text2 = " some _ text\n "; + ASSERT_TRUE(utils::strsame(text1,text2)); + + text2 = "some_other_text"; + ASSERT_FALSE(utils::strsame(text1,text2)); + text2 = " some other_text"; + ASSERT_FALSE(utils::strsame(text1,text2)); +} + TEST(Utils, trim) { auto trimmed = utils::trim("\t some text");