diff options
Diffstat (limited to 'Source/cmAlgorithms.h')
-rw-r--r-- | Source/cmAlgorithms.h | 174 |
1 files changed, 33 insertions, 141 deletions
diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index d1e32b0..59aa86f 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -6,93 +6,66 @@ #include "cmConfigure.h" // IWYU pragma: keep #include "cmRange.h" - #include "cm_kwiml.h" #include <algorithm> #include <functional> #include <iterator> -#include <memory> -#include <sstream> -#include <string.h> -#include <string> #include <unordered_set> #include <utility> #include <vector> -inline bool cmHasLiteralPrefixImpl(const std::string& str1, const char* str2, - size_t N) +template <std::size_t N> +struct cmOverloadPriority : cmOverloadPriority<N - 1> { - return strncmp(str1.c_str(), str2, N) == 0; -} +}; -inline bool cmHasLiteralPrefixImpl(const char* str1, const char* str2, - size_t N) +template <> +struct cmOverloadPriority<0> { - return strncmp(str1, str2, N) == 0; -} +}; -inline bool cmHasLiteralSuffixImpl(const std::string& str1, const char* str2, - size_t N) +template <typename FwdIt> +FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last) { - size_t len = str1.size(); - return len >= N && strcmp(str1.c_str() + len - N, str2) == 0; + const typename std::iterator_traits<FwdIt>::difference_type dist = + std::distance(middle, last); + std::rotate(first, middle, last); + std::advance(first, dist); + return first; } -inline bool cmHasLiteralSuffixImpl(const char* str1, const char* str2, - size_t N) +template <typename Container, typename Predicate> +void cmEraseIf(Container& cont, Predicate pred) { - size_t len = strlen(str1); - return len >= N && strcmp(str1 + len - N, str2) == 0; + cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end()); } -template <typename T, size_t N> -bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N]) +template <typename Range, typename Key> +auto cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<2>) + -> decltype(range.exists(key)) { - return cmHasLiteralPrefixImpl(str1, str2, N - 1); + return range.exists(key); } -template <typename T, size_t N> -bool cmHasLiteralSuffix(const T& str1, const char (&str2)[N]) +template <typename Range, typename Key> +auto cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<1>) + -> decltype(range.find(key) != range.end()) { - return cmHasLiteralSuffixImpl(str1, str2, N - 1); + return range.find(key) != range.end(); } -struct cmStrCmp +template <typename Range, typename Key> +bool cmContainsImpl(Range const& range, Key const& key, cmOverloadPriority<0>) { - cmStrCmp(const char* test) - : m_test(test) - { - } - cmStrCmp(std::string test) - : m_test(std::move(test)) - { - } - - bool operator()(const std::string& input) const { return m_test == input; } - - bool operator()(const char* input) const - { - return strcmp(input, m_test.c_str()) == 0; - } - -private: - const std::string m_test; -}; - -template <typename FwdIt> -FwdIt cmRotate(FwdIt first, FwdIt middle, FwdIt last) -{ - const typename std::iterator_traits<FwdIt>::difference_type dist = - std::distance(middle, last); - std::rotate(first, middle, last); - std::advance(first, dist); - return first; + using std::begin; + using std::end; + return std::find(begin(range), end(range), key) != end(range); } -template <typename Container, typename Predicate> -void cmEraseIf(Container& cont, Predicate pred) +template <typename Range, typename Key> +bool cmContains(Range const& range, Key const& key) { - cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end()); + return cmContainsImpl(range, key, cmOverloadPriority<2>{}); } namespace ContainerAlgorithms { @@ -158,8 +131,6 @@ private: }; } -typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange; - class cmListFileBacktrace; typedef cmRange<std::vector<cmListFileBacktrace>::const_iterator> cmBacktraceRange; @@ -184,31 +155,6 @@ void cmAppend(std::vector<T>& v, InputIt first, InputIt last) } template <typename Range> -std::string cmJoin(Range const& r, const char* delimiter) -{ - if (r.empty()) { - return std::string(); - } - std::ostringstream os; - typedef typename Range::value_type ValueType; - typedef typename Range::const_iterator InputIt; - const InputIt first = r.begin(); - InputIt last = r.end(); - --last; - std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter)); - - os << *last; - - return os.str(); -} - -template <typename Range> -std::string cmJoin(Range const& r, std::string const& delimiter) -{ - return cmJoin(r, delimiter.c_str()); -} - -template <typename Range> typename Range::const_iterator cmRemoveN(Range& r, size_t n) { return ContainerAlgorithms::RemoveN(r.begin(), r.end(), n); @@ -268,7 +214,7 @@ ForwardIterator cmRemoveDuplicates(ForwardIterator first, ForwardIterator last) ForwardIterator result = first; while (first != last) { - if (uniq.find(first) == uniq.end()) { + if (!cmContains(uniq, first)) { if (result != first) { *result = std::move(*first); } @@ -286,23 +232,6 @@ typename Range::const_iterator cmRemoveDuplicates(Range& r) return cmRemoveDuplicates(r.begin(), r.end()); } -template <typename Range> -std::string cmWrap(std::string const& prefix, Range const& r, - std::string const& suffix, std::string const& sep) -{ - if (r.empty()) { - return std::string(); - } - return prefix + cmJoin(r, suffix + sep + prefix) + suffix; -} - -template <typename Range> -std::string cmWrap(char prefix, Range const& r, char suffix, - std::string const& sep) -{ - return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep); -} - template <typename Range, typename T> typename Range::const_iterator cmFindNot(Range const& r, T const& t) { @@ -315,45 +244,8 @@ std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it) return std::reverse_iterator<Iter>(it); } -inline bool cmHasPrefix(std::string const& str, std::string const& prefix) -{ - if (str.size() < prefix.size()) { - return false; - } - return str.compare(0, prefix.size(), prefix) == 0; -} - -inline bool cmHasSuffix(const std::string& str, const std::string& suffix) -{ - if (str.size() < suffix.size()) { - return false; - } - return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; -} - -inline void cmStripSuffixIfExists(std::string& str, const std::string& suffix) -{ - if (cmHasSuffix(str, suffix)) { - str.resize(str.size() - suffix.size()); - } -} - namespace cm { -#if defined(CMake_HAVE_CXX_MAKE_UNIQUE) - -using std::make_unique; - -#else - -template <typename T, typename... Args> -std::unique_ptr<T> make_unique(Args&&... args) -{ - return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); -} - -#endif - #if __cplusplus >= 201703L || defined(_MSVC_LANG) && _MSVC_LANG >= 201703L using std::size; |