mirror of
https://github.com/Kitware/CMake.git
synced 2026-03-10 02:19:10 -05:00
Merge topic 'cmMakefile-refactoring'
62bb8a77a5cmMakefile: Teach ConfigureFile to move the temp file instead of copying itdc38f81237cmSystemTools: Revise MoveFileIfDifferent to return cmsys::Status0a0a826b86cmMakefile: Use find_if instead of manual loope0294fa310cmMakefile: Replace single-char string literal with char for cmStrCat206961bc60cmMakefile::FormatListFileStack: Refactor "manual" loop into algorithms2e16b58b7ccmStringAlgorithms: Move generic strings join function to public APIce991188f3cmMakefile::FormatListFileStack: Exit early on empty stack trace29fb605822cmMakefile::FormatListFileStack: Refactor 'while' into 'for' ... Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: buildbot <buildbot@kitware.com> Merge-request: !9635
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -7,7 +7,6 @@
|
||||
#include <cstddef> // IWYU pragma: keep
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iterator>
|
||||
|
||||
std::string cmTrimWhitespace(cm::string_view str)
|
||||
{
|
||||
@@ -239,51 +238,14 @@ bool cmStrToULongLong(std::string const& str, unsigned long long* value)
|
||||
return cmStrToULongLong(str.c_str(), value);
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
std::size_t getJoinedLength(Range const& rng, cm::string_view separator)
|
||||
{
|
||||
std::size_t rangeLength{};
|
||||
for (auto const& item : rng) {
|
||||
rangeLength += item.size();
|
||||
}
|
||||
|
||||
auto const separatorsLength = (rng.size() - 1) * separator.size();
|
||||
|
||||
return rangeLength + separatorsLength;
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
std::string cmJoinImpl(Range const& rng, cm::string_view separator,
|
||||
cm::string_view initial)
|
||||
{
|
||||
if (rng.empty()) {
|
||||
return { std::begin(initial), std::end(initial) };
|
||||
}
|
||||
|
||||
std::string result;
|
||||
result.reserve(initial.size() + getJoinedLength(rng, separator));
|
||||
result.append(std::begin(initial), std::end(initial));
|
||||
|
||||
auto begin = std::begin(rng);
|
||||
auto end = std::end(rng);
|
||||
result += *begin;
|
||||
|
||||
for (++begin; begin != end; ++begin) {
|
||||
result.append(std::begin(separator), std::end(separator));
|
||||
result += *begin;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string cmJoin(std::vector<std::string> const& rng,
|
||||
cm::string_view separator, cm::string_view initial)
|
||||
{
|
||||
return cmJoinImpl(rng, separator, initial);
|
||||
return cmJoinStrings(rng, separator, initial);
|
||||
}
|
||||
|
||||
std::string cmJoin(cmStringRange const& rng, cm::string_view separator,
|
||||
cm::string_view initial)
|
||||
{
|
||||
return cmJoinImpl(rng, separator, initial);
|
||||
return cmJoinStrings(rng, separator, initial);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -77,6 +79,38 @@ std::string cmJoin(Range const& rng, cm::string_view separator)
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/** Generic function to join strings range with separator
|
||||
* and initial leading string into a single string.
|
||||
*/
|
||||
template <typename Range>
|
||||
std::string cmJoinStrings(Range const& rng, cm::string_view separator,
|
||||
cm::string_view initial)
|
||||
{
|
||||
if (rng.empty()) {
|
||||
return { std::begin(initial), std::end(initial) };
|
||||
}
|
||||
|
||||
std::string result;
|
||||
result.reserve(
|
||||
std::accumulate(std::begin(rng), std::end(rng),
|
||||
initial.size() + (rng.size() - 1) * separator.size(),
|
||||
[](std::size_t sum, const std::string& item) {
|
||||
return sum + item.size();
|
||||
}));
|
||||
result.append(std::begin(initial), std::end(initial));
|
||||
|
||||
auto begin = std::begin(rng);
|
||||
auto end = std::end(rng);
|
||||
result += *begin;
|
||||
|
||||
for (++begin; begin != end; ++begin) {
|
||||
result.append(std::begin(separator), std::end(separator));
|
||||
result += *begin;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Faster overloads for std::string ranges.
|
||||
* If @a initial is provided, it prepends the resulted string without
|
||||
|
||||
@@ -1319,16 +1319,18 @@ cmSystemTools::RenameResult cmSystemTools::RenameFile(
|
||||
#endif
|
||||
}
|
||||
|
||||
void cmSystemTools::MoveFileIfDifferent(const std::string& source,
|
||||
const std::string& destination)
|
||||
cmsys::Status cmSystemTools::MoveFileIfDifferent(
|
||||
const std::string& source, const std::string& destination)
|
||||
{
|
||||
cmsys::Status res = {};
|
||||
if (FilesDiffer(source, destination)) {
|
||||
if (RenameFile(source, destination)) {
|
||||
return;
|
||||
return res;
|
||||
}
|
||||
CopyFileAlways(source, destination);
|
||||
res = CopyFileAlways(source, destination);
|
||||
}
|
||||
RemoveFile(source);
|
||||
return res;
|
||||
}
|
||||
|
||||
void cmSystemTools::Glob(const std::string& directory,
|
||||
|
||||
@@ -211,8 +211,8 @@ public:
|
||||
std::string* err = nullptr);
|
||||
|
||||
//! Rename a file if contents are different, delete the source otherwise
|
||||
static void MoveFileIfDifferent(const std::string& source,
|
||||
const std::string& destination);
|
||||
static cmsys::Status MoveFileIfDifferent(const std::string& source,
|
||||
const std::string& destination);
|
||||
|
||||
/**
|
||||
* Run a single executable command
|
||||
|
||||
Reference in New Issue
Block a user