diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2019-07-29 12:47:10 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2019-08-01 08:38:12 (GMT) |
commit | 541361a58bae487956c5b50d475edc4fcb453030 (patch) | |
tree | f6dc3ecded4151755d875202b0c6a4cbfda67872 | |
parent | 33d9a691306956750bffb10be9d0b17a47c0ec0b (diff) | |
download | CMake-541361a58bae487956c5b50d475edc4fcb453030.zip CMake-541361a58bae487956c5b50d475edc4fcb453030.tar.gz CMake-541361a58bae487956c5b50d475edc4fcb453030.tar.bz2 |
cmStringAlgorithms: Simplify cmStrCmp using cm::string_view
-rw-r--r-- | Source/cmStringAlgorithms.h | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index 149e0ad..3f88692 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -15,28 +15,21 @@ #include <utility> #include <vector> +/** String range type. */ typedef cmRange<std::vector<std::string>::const_iterator> cmStringRange; +/** Callable string comparison struct. */ struct cmStrCmp { - cmStrCmp(const char* test) - : m_test(test) + cmStrCmp(std::string str) + : Test_(std::move(str)) { } - 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; - } + bool operator()(cm::string_view sv) const { return Test_ == sv; } private: - const std::string m_test; + std::string const Test_; }; template <typename Range> @@ -81,46 +74,46 @@ std::string cmWrap(char prefix, Range const& r, char suffix, return cmWrap(std::string(1, prefix), r, std::string(1, suffix), sep); } -/** Returns true if string @a str starts with the character @a prefix. **/ +/** Returns true if string @a str starts with the character @a prefix. */ inline bool cmHasPrefix(cm::string_view str, char prefix) { return !str.empty() && (str.front() == prefix); } -/** Returns true if string @a str starts with string @a prefix. **/ +/** Returns true if string @a str starts with string @a prefix. */ inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix) { return str.compare(0, prefix.size(), prefix) == 0; } -/** Returns true if string @a str starts with string @a prefix. **/ +/** Returns true if string @a str starts with string @a prefix. */ template <size_t N> inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N]) { return cmHasPrefix(str, cm::string_view(prefix, N - 1)); } -/** Returns true if string @a str ends with the character @a suffix. **/ +/** Returns true if string @a str ends with the character @a suffix. */ inline bool cmHasSuffix(cm::string_view str, char suffix) { return !str.empty() && (str.back() == suffix); } -/** Returns true if string @a str ends with string @a suffix. **/ +/** Returns true if string @a str ends with string @a suffix. */ inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix) { return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } -/** Returns true if string @a str ends with string @a suffix. **/ +/** Returns true if string @a str ends with string @a suffix. */ template <size_t N> inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N]) { return cmHasSuffix(str, cm::string_view(suffix, N - 1)); } -/** Removes an existing suffix character of from the string @a str. **/ +/** Removes an existing suffix character of from the string @a str. */ inline void cmStripSuffixIfExists(std::string& str, char suffix) { if (cmHasSuffix(str, suffix)) { @@ -128,7 +121,7 @@ inline void cmStripSuffixIfExists(std::string& str, char suffix) } } -/** Removes an existing suffix string of from the string @a str. **/ +/** Removes an existing suffix string of from the string @a str. */ inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix) { if (cmHasSuffix(str, suffix)) { |