diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2019-07-20 09:17:12 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2019-07-22 14:04:22 (GMT) |
commit | 301f5356fd1cb6d30f662b9b4071108a378063cd (patch) | |
tree | 16263e48e4728f378204af21c9dcc88978cb1231 /Source/cmAlgorithms.h | |
parent | 4cd039b78a955972a9c2b3947f267ce4b93489a0 (diff) | |
download | CMake-301f5356fd1cb6d30f662b9b4071108a378063cd.zip CMake-301f5356fd1cb6d30f662b9b4071108a378063cd.tar.gz CMake-301f5356fd1cb6d30f662b9b4071108a378063cd.tar.bz2 |
cmAlgorithms: Make cmHasPrefix and cmHasSuffix cm::string_view based
Make `cmHasPrefix`, `cmHasSuffix` and `cmStripSuffixIfExists` accept
arguments as `cm::string_view` instead of `const std::string&`.
This lets them accept literal strings without having to allocate a temporary
`std::string`.
Add variants of `cmHasPrefix`, `cmHasSuffix` and `cmStripSuffixIfExists` that
accept a single character as second argument.
Diffstat (limited to 'Source/cmAlgorithms.h')
-rw-r--r-- | Source/cmAlgorithms.h | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index d153076..b4b480b 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -8,6 +8,7 @@ #include "cmRange.h" #include "cm_kwiml.h" +#include "cm_string_view.hxx" #include <algorithm> #include <functional> #include <iterator> @@ -314,23 +315,41 @@ std::reverse_iterator<Iter> cmMakeReverseIterator(Iter it) return std::reverse_iterator<Iter>(it); } -inline bool cmHasPrefix(std::string const& str, std::string const& 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. **/ +inline bool cmHasPrefix(cm::string_view str, cm::string_view 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) +/** Returns true if string @a str ends with the character @a suffix. **/ +inline bool cmHasSuffix(cm::string_view str, char suffix) { - if (str.size() < suffix.size()) { - return false; + return !str.empty() && (str.back() == 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; +} + +/** Removes an existing suffix character of from the string @a str. **/ +inline void cmStripSuffixIfExists(std::string& str, char suffix) +{ + if (cmHasSuffix(str, suffix)) { + str.pop_back(); } - return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } -inline void cmStripSuffixIfExists(std::string& str, const std::string& suffix) +/** 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)) { str.resize(str.size() - suffix.size()); |