From 07f69bd5cc9b6b4bc040327f315620c736ec15ba Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 13 Sep 2016 08:37:00 -0400 Subject: [PATCH 1/2] cmSystemTools: Add strverscmp Add support for natural string order by comparing non-numerical character directly and numerical number by firstly collecting contiguous digits. The order is defined by the `strverscmp(3)` manual [1]. [1] http://man7.org/linux/man-pages/man3/strverscmp.3.html Inspired-by: Pierluigi Taddei --- Source/cmSystemTools.cxx | 77 ++++++++++++++++++++++++++++++++++++++++ Source/cmSystemTools.h | 10 ++++++ 2 files changed, 87 insertions(+) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 7da997581..a0a9c5098 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2426,6 +2426,83 @@ bool cmSystemTools::VersionCompareGreaterEq(std::string const& lhs, lhs.c_str(), rhs.c_str()); } +static size_t cm_strverscmp_find_first_difference_or_end(const char* lhs, + const char* rhs) +{ + size_t i = 0; + /* Step forward until we find a difference or both strings end together. + The difference may lie on the null-terminator of one string. */ + while (lhs[i] == rhs[i] && lhs[i] != 0) { + ++i; + } + return i; +} + +static size_t cm_strverscmp_find_digits_begin(const char* s, size_t i) +{ + /* Step back until we are not preceded by a digit. */ + while (i > 0 && isdigit(s[i - 1])) { + --i; + } + return i; +} + +static size_t cm_strverscmp_find_digits_end(const char* s, size_t i) +{ + /* Step forward over digits. */ + while (isdigit(s[i])) { + ++i; + } + return i; +} + +static size_t cm_strverscmp_count_leading_zeros(const char* s, size_t b) +{ + size_t i = b; + /* Step forward over zeros that are followed by another digit. */ + while (s[i] == '0' && isdigit(s[i + 1])) { + ++i; + } + return i - b; +} + +static int cm_strverscmp(const char* lhs, const char* rhs) +{ + size_t const i = cm_strverscmp_find_first_difference_or_end(lhs, rhs); + if (lhs[i] != rhs[i]) { + /* The strings differ starting at 'i'. Check for a digit sequence. */ + size_t const b = cm_strverscmp_find_digits_begin(lhs, i); + if (b != i || (isdigit(lhs[i]) && isdigit(rhs[i]))) { + /* A digit sequence starts at 'b', preceding or at 'i'. */ + + /* Look for leading zeros, implying a leading decimal point. */ + size_t const lhs_zeros = cm_strverscmp_count_leading_zeros(lhs, b); + size_t const rhs_zeros = cm_strverscmp_count_leading_zeros(rhs, b); + if (lhs_zeros != rhs_zeros) { + /* The side with more leading zeros orders first. */ + return rhs_zeros > lhs_zeros ? 1 : -1; + } + if (lhs_zeros == 0) { + /* No leading zeros; compare digit sequence lengths. */ + size_t const lhs_end = cm_strverscmp_find_digits_end(lhs, i); + size_t const rhs_end = cm_strverscmp_find_digits_end(rhs, i); + if (lhs_end != rhs_end) { + /* The side with fewer digits orders first. */ + return lhs_end > rhs_end ? 1 : -1; + } + } + } + } + + /* Ordering was not decided by digit sequence lengths; compare bytes. */ + return lhs[i] - rhs[i]; +} + +int cmSystemTools::strverscmp(std::string const& lhs, std::string const& rhs) +{ + return cm_strverscmp(lhs.c_str(), rhs.c_str()); +} + bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg, bool* removed) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 3c1a9f4f2..aecf40eb8 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -305,6 +305,16 @@ public: static bool VersionCompareGreaterEq(std::string const& lhs, std::string const& rhs); + /** + * Compare two ASCII strings using natural versioning order. + * Non-numerical characters are compared directly. + * Numerical characters are first globbed such that, e.g. + * `test000 < test01 < test0 < test1 < test10`. + * Return a value less than, equal to, or greater than zero if lhs + * precedes, equals, or succeeds rhs in the defined ordering. + */ + static int strverscmp(std::string const& lhs, std::string const& rhs); + /** * Determine the file type based on the extension */ From 884943251b16d3e34901a6533944e6b78ef8eb03 Mon Sep 17 00:00:00 2001 From: Pierluigi Taddei Date: Mon, 12 Sep 2016 23:13:13 +0200 Subject: [PATCH 2/2] Tests: Add test for our strverscmp implementation Cover typical examples and the ordering defined by the `strverscmp(3)` man page. --- Tests/CMakeLib/testSystemTools.cxx | 72 ++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/Tests/CMakeLib/testSystemTools.cxx b/Tests/CMakeLib/testSystemTools.cxx index e834b93b8..5b5c8d27b 100644 --- a/Tests/CMakeLib/testSystemTools.cxx +++ b/Tests/CMakeLib/testSystemTools.cxx @@ -19,6 +19,13 @@ std::cout << "FAILED: " << (m) << "\n"; \ failed = 1 +#define cmAssert(exp, m) \ + if ((exp)) { \ + cmPassed(m); \ + } else { \ + cmFailed(m); \ + } + int testSystemTools(int /*unused*/, char* /*unused*/ []) { int failed = 0; @@ -26,10 +33,67 @@ int testSystemTools(int /*unused*/, char* /*unused*/ []) // Test cmSystemTools::UpperCase std::string str = "abc"; std::string strupper = "ABC"; - if (cmSystemTools::UpperCase(str) == strupper) { - cmPassed("cmSystemTools::UpperCase is working"); - } else { - cmFailed("cmSystemTools::UpperCase is working"); + cmAssert(cmSystemTools::UpperCase(str) == strupper, + "cmSystemTools::UpperCase"); + + // ---------------------------------------------------------------------- + // Test cmSystemTools::strverscmp + cmAssert(cmSystemTools::strverscmp("", "") == 0, "strverscmp empty string"); + cmAssert(cmSystemTools::strverscmp("abc", "") > 0, + "strverscmp string vs empty string"); + cmAssert(cmSystemTools::strverscmp("abc", "abc") == 0, + "strverscmp same string"); + cmAssert(cmSystemTools::strverscmp("abd", "abc") > 0, + "strverscmp character string"); + cmAssert(cmSystemTools::strverscmp("abc", "abd") < 0, + "strverscmp symmetric"); + cmAssert(cmSystemTools::strverscmp("12345", "12344") > 0, + "strverscmp natural numbers"); + cmAssert(cmSystemTools::strverscmp("100", "99") > 0, + "strverscmp natural numbers different digits"); + cmAssert(cmSystemTools::strverscmp("12345", "00345") > 0, + "strverscmp natural against decimal (same length)"); + cmAssert(cmSystemTools::strverscmp("99999999999999", "99999999999991") > 0, + "strverscmp natural overflow"); + cmAssert(cmSystemTools::strverscmp("00000000000009", "00000000000001") > 0, + "strverscmp deciaml precision"); + cmAssert(cmSystemTools::strverscmp("a.b.c.0", "a.b.c.000") > 0, + "strverscmp multiple zeros"); + cmAssert(cmSystemTools::strverscmp("lib_1.2_10", "lib_1.2_2") > 0, + "strverscmp last number "); + cmAssert(cmSystemTools::strverscmp("12lib", "2lib") > 0, + "strverscmp first number "); + cmAssert(cmSystemTools::strverscmp("02lib", "002lib") > 0, + "strverscmp first number decimal "); + cmAssert(cmSystemTools::strverscmp("10", "9a") > 0, + "strverscmp letter filler "); + cmAssert(cmSystemTools::strverscmp("000", "0001") > 0, + "strverscmp zero and leading zeros "); + + // test sorting using standard strvercmp input + std::vector testString; + testString.push_back("000"); + testString.push_back("00"); + testString.push_back("01"); + testString.push_back("010"); + testString.push_back("09"); + testString.push_back("0"); + testString.push_back("1"); + testString.push_back("9"); + testString.push_back("10"); + + // test global ordering of input strings + for (size_t i = 0; i < testString.size() - 1; i++) { + for (size_t j = i + 1; j < testString.size(); j++) { + if (cmSystemTools::strverscmp(testString[i], testString[j]) >= 0) { + cmFailed("cmSystemTools::strverscmp error in comparing strings " + + testString[i] + " " + testString[j]); + } + } + } + + if (!failed) { + cmPassed("cmSystemTools::strverscmp working"); } return failed; }