Commit 575f9776 authored by Brad King's avatar Brad King 💬 Committed by Kitware Robot
Browse files

Merge topic 'list_sort'

49a51a61

 list: Add options to control the SORT comparison operation

Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !2065
parents 65ee7d1c 49a51a61
......@@ -28,7 +28,7 @@ Synopsis
`Ordering`_
list(`REVERSE`_ <list>)
list(`SORT`_ <list>)
list(`SORT`_ <list> [...])
Introduction
^^^^^^^^^^^^
......@@ -253,7 +253,23 @@ Reverses the contents of the list in-place.
::
list(SORT <list>)
list(SORT <list> [COMPARE <compare>] [CASE <case>] [ORDER <order>])
Sorts the list in-place alphabetically.
Use the option ``<compare>`` to select the compare type for sorting.
The ``<compare>`` option may be one of:
* ``STRING``: Sorts a list of strings alphabetically.
* ``FILE_BASENAME``: Sort a list of pathnames of files by their basenames.
Use the option ``<case>`` to select a case sensitive or case insensitive sort mode.
The ``<case>`` option may be one of:
* ``SENSITIVE``: Sorts the list alphabetically.
* ``INSENSITIVE``: Sorts the list alphabetically in descending order.
Use the option ``<order>`` to select a case sensitive or case insensitive sort mode.
The ``<order>`` option may be one of:
* ``ASCENDING``: Sorts the list in ascending order.
* ``DESCENDING``: Sorts the list in descending order.
list_sort
---------
* The :command:`list(SORT)` command gained options to control the
comparison operation used to order the entries.
......@@ -963,14 +963,193 @@ bool cmListCommand::HandleTransformCommand(
return true;
}
class cmStringSorter
{
public:
enum class Order
{
UNINITIALIZED,
ASCENDING,
DESCENDING,
};
enum class Compare
{
UNINITIALIZED,
STRING,
FILE_BASENAME,
};
enum class CaseSensitivity
{
UNINITIALIZED,
SENSITIVE,
INSENSITIVE,
};
protected:
typedef std::string (*StringFilter)(const std::string& in);
StringFilter GetCompareFilter(Compare compare)
{
return (compare == Compare::FILE_BASENAME) ? cmSystemTools::GetFilenameName
: nullptr;
}
StringFilter GetCaseFilter(CaseSensitivity sensitivity)
{
return (sensitivity == CaseSensitivity::INSENSITIVE)
? cmSystemTools::LowerCase
: nullptr;
}
public:
cmStringSorter(Compare compare, CaseSensitivity caseSensitivity,
Order desc = Order::ASCENDING)
: filters{ GetCompareFilter(compare), GetCaseFilter(caseSensitivity) }
, descending(desc == Order::DESCENDING)
{
}
std::string ApplyFilter(const std::string& argument)
{
std::string result = argument;
for (auto filter : filters) {
if (filter != nullptr) {
result = filter(result);
}
}
return result;
}
bool operator()(const std::string& a, const std::string& b)
{
std::string af = ApplyFilter(a);
std::string bf = ApplyFilter(b);
bool result;
if (descending) {
result = bf < af;
} else {
result = af < bf;
}
return result;
}
protected:
StringFilter filters[2] = { nullptr, nullptr };
bool descending;
};
bool cmListCommand::HandleSortCommand(std::vector<std::string> const& args)
{
assert(args.size() >= 2);
if (args.size() > 2) {
this->SetError("sub-command SORT only takes one argument.");
if (args.size() > 8) {
this->SetError("sub-command SORT only takes up to six arguments.");
return false;
}
auto sortCompare = cmStringSorter::Compare::UNINITIALIZED;
auto sortCaseSensitivity = cmStringSorter::CaseSensitivity::UNINITIALIZED;
auto sortOrder = cmStringSorter::Order::UNINITIALIZED;
size_t argumentIndex = 2;
const std::string messageHint = "sub-command SORT ";
while (argumentIndex < args.size()) {
const std::string option = args[argumentIndex++];
if (option == "COMPARE") {
if (sortCompare != cmStringSorter::Compare::UNINITIALIZED) {
std::string error = messageHint + "option \"" + option +
"\" has been specified multiple times.";
this->SetError(error);
return false;
}
if (argumentIndex < args.size()) {
const std::string argument = args[argumentIndex++];
if (argument == "STRING") {
sortCompare = cmStringSorter::Compare::STRING;
} else if (argument == "FILE_BASENAME") {
sortCompare = cmStringSorter::Compare::FILE_BASENAME;
} else {
std::string error = messageHint + "value \"" + argument +
"\" for option \"" + option + "\" is invalid.";
this->SetError(error);
return false;
}
} else {
std::string error =
messageHint + "missing argument for option \"" + option + "\".";
this->SetError(error);
return false;
}
} else if (option == "CASE") {
if (sortCaseSensitivity !=
cmStringSorter::CaseSensitivity::UNINITIALIZED) {
std::string error = messageHint + "option \"" + option +
"\" has been specified multiple times.";
this->SetError(error);
return false;
}
if (argumentIndex < args.size()) {
const std::string argument = args[argumentIndex++];
if (argument == "SENSITIVE") {
sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
} else if (argument == "INSENSITIVE") {
sortCaseSensitivity = cmStringSorter::CaseSensitivity::INSENSITIVE;
} else {
std::string error = messageHint + "value \"" + argument +
"\" for option \"" + option + "\" is invalid.";
this->SetError(error);
return false;
}
} else {
std::string error =
messageHint + "missing argument for option \"" + option + "\".";
this->SetError(error);
return false;
}
} else if (option == "ORDER") {
if (sortOrder != cmStringSorter::Order::UNINITIALIZED) {
std::string error = messageHint + "option \"" + option +
"\" has been specified multiple times.";
this->SetError(error);
return false;
}
if (argumentIndex < args.size()) {
const std::string argument = args[argumentIndex++];
if (argument == "ASCENDING") {
sortOrder = cmStringSorter::Order::ASCENDING;
} else if (argument == "DESCENDING") {
sortOrder = cmStringSorter::Order::DESCENDING;
} else {
std::string error = messageHint + "value \"" + argument +
"\" for option \"" + option + "\" is invalid.";
this->SetError(error);
return false;
}
} else {
std::string error =
messageHint + "missing argument for option \"" + option + "\".";
this->SetError(error);
return false;
}
} else {
std::string error =
messageHint + "option \"" + option + "\" is unknown.";
this->SetError(error);
return false;
}
}
// set Default Values if Option is not given
if (sortCompare == cmStringSorter::Compare::UNINITIALIZED) {
sortCompare = cmStringSorter::Compare::STRING;
}
if (sortCaseSensitivity == cmStringSorter::CaseSensitivity::UNINITIALIZED) {
sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
}
if (sortOrder == cmStringSorter::Order::UNINITIALIZED) {
sortOrder = cmStringSorter::Order::ASCENDING;
}
const std::string& listName = args[1];
// expand the variable
std::vector<std::string> varArgsExpanded;
......@@ -979,7 +1158,14 @@ bool cmListCommand::HandleSortCommand(std::vector<std::string> const& args)
return false;
}
std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
if ((sortCompare == cmStringSorter::Compare::STRING) &&
(sortCaseSensitivity == cmStringSorter::CaseSensitivity::SENSITIVE) &&
(sortOrder == cmStringSorter::Order::ASCENDING)) {
std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
} else {
cmStringSorter sorter(sortCompare, sortCaseSensitivity, sortOrder);
std::sort(varArgsExpanded.begin(), varArgsExpanded.end(), sorter);
}
std::string value = cmJoin(varArgsExpanded, ";");
this->Makefile->AddDefinition(listName, value.c_str());
......
......@@ -20,7 +20,6 @@ run_cmake(JOIN-TooManyArguments)
run_cmake(LENGTH-TooManyArguments)
run_cmake(REMOVE_DUPLICATES-TooManyArguments)
run_cmake(REVERSE-TooManyArguments)
run_cmake(SORT-TooManyArguments)
run_cmake(SUBLIST-TooManyArguments)
run_cmake(FILTER-NotList)
......@@ -84,3 +83,16 @@ run_cmake(TRANSFORM-GENEX_STRIP)
run_cmake(TRANSFORM-APPEND)
run_cmake(TRANSFORM-PREPEND)
run_cmake(TRANSFORM-REPLACE)
# argument tests
run_cmake(SORT-WrongOption)
run_cmake(SORT-BadCaseOption)
run_cmake(SORT-BadCompareOption)
run_cmake(SORT-BadOrderOption)
run_cmake(SORT-DuplicateOrderOption)
run_cmake(SORT-DuplicateCompareOption)
run_cmake(SORT-DuplicateCaseOption)
run_cmake(SORT-NoCaseOption)
# Successful tests
run_cmake(SORT)
^CMake Error at SORT-BadCaseOption.cmake:1 \(list\):
list sub-command SORT value "BAD_CASE_OPTION" for option "CASE" is invalid.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$
list(SORT mylist CASE BAD_CASE_OPTION)
^CMake Error at SORT-BadCompareOption.cmake:1 \(list\):
list sub-command SORT value "BAD_COMPARE_OPTION" for option "COMPARE" is
invalid.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$
list(SORT mylist COMPARE BAD_COMPARE_OPTION)
^CMake Error at SORT-BadOrderOption.cmake:1 \(list\):
list sub-command SORT value "BAD_ODER_OPTION" for option "ORDER" is
invalid.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$
list(SORT mylist ORDER BAD_ODER_OPTION)
^CMake Error at SORT-DuplicateCaseOption.cmake:2 \(list\):
list sub-command SORT option "CASE" has been specified multiple times.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$
set (mylist a b c)
list(SORT mylist CASE INSENSITIVE CASE INSENSITIVE )
^CMake Error at SORT-DuplicateCompareOption.cmake:2 \(list\):
list sub-command SORT option "COMPARE" has been specified multiple times.
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)$
set (mylist a b c)
list(SORT mylist COMPARE STRING COMPARE STRING)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment