diff options
author | Johnny Jazeix <jazeix@gmail.com> | 2020-04-13 16:26:28 (GMT) |
---|---|---|
committer | Johnny Jazeix <jazeix@gmail.com> | 2020-04-13 16:26:28 (GMT) |
commit | da99eca1e7956e42e88df1b52c322f9c571acddd (patch) | |
tree | a56a4154cfe09f318cbd3b235c22b0378f230d5d /Source/cmListCommand.cxx | |
parent | fab932c3cba9423443c7ab25fda3999e4fc4b347 (diff) | |
download | CMake-da99eca1e7956e42e88df1b52c322f9c571acddd.zip CMake-da99eca1e7956e42e88df1b52c322f9c571acddd.tar.gz CMake-da99eca1e7956e42e88df1b52c322f9c571acddd.tar.bz2 |
list: add NATURAL sorting on SORT sub-command
Fixes: #20563
Diffstat (limited to 'Source/cmListCommand.cxx')
-rw-r--r-- | Source/cmListCommand.cxx | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index 5200a16..860b4da 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -1051,6 +1051,7 @@ public: UNINITIALIZED, STRING, FILE_BASENAME, + NATURAL, }; enum class CaseSensitivity { @@ -1074,10 +1075,25 @@ protected: : nullptr; } + using ComparisonFunction = + std::function<bool(const std::string&, const std::string&)>; + ComparisonFunction GetComparisonFunction(Compare compare) + { + if (compare == Compare::NATURAL) { + return std::function<bool(const std::string&, const std::string&)>( + [](const std::string& x, const std::string& y) { + return cmSystemTools::strverscmp(x, y) < 0; + }); + } + return std::function<bool(const std::string&, const std::string&)>( + [](const std::string& x, const std::string& y) { return x < y; }); + } + public: cmStringSorter(Compare compare, CaseSensitivity caseSensitivity, Order desc = Order::ASCENDING) : filters{ GetCompareFilter(compare), GetCaseFilter(caseSensitivity) } + , sortMethod(GetComparisonFunction(compare)) , descending(desc == Order::DESCENDING) { } @@ -1099,15 +1115,16 @@ public: std::string bf = ApplyFilter(b); bool result; if (descending) { - result = bf < af; + result = sortMethod(bf, af); } else { - result = af < bf; + result = sortMethod(af, bf); } return result; } protected: StringFilter filters[2] = { nullptr, nullptr }; + ComparisonFunction sortMethod; bool descending; }; @@ -1142,6 +1159,8 @@ bool HandleSortCommand(std::vector<std::string> const& args, sortCompare = cmStringSorter::Compare::STRING; } else if (argument == "FILE_BASENAME") { sortCompare = cmStringSorter::Compare::FILE_BASENAME; + } else if (argument == "NATURAL") { + sortCompare = cmStringSorter::Compare::NATURAL; } else { std::string error = cmStrCat(messageHint, "value \"", argument, "\" for option \"", |