diff options
author | Brad King <brad.king@kitware.com> | 2019-07-23 11:32:14 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2019-07-23 11:32:30 (GMT) |
commit | dcd4d3e9bfa95f62718d0bb561a6f328d508cda5 (patch) | |
tree | c743ca872a781bc976bbe4c0d219911d80e1b055 /Source | |
parent | 5396c5fce4eae4f4b0a9b974e0a0304c7afda459 (diff) | |
parent | 301f5356fd1cb6d30f662b9b4071108a378063cd (diff) | |
download | CMake-dcd4d3e9bfa95f62718d0bb561a6f328d508cda5.zip CMake-dcd4d3e9bfa95f62718d0bb561a6f328d508cda5.tar.gz CMake-dcd4d3e9bfa95f62718d0bb561a6f328d508cda5.tar.bz2 |
Merge topic 'cmHasLiteralPrefix_cm_string_view'
301f5356fd cmAlgorithms: Make cmHasPrefix and cmHasSuffix cm::string_view based
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Daniel Pfeifer <daniel@pfeifer-mail.de>
Merge-request: !3583
Diffstat (limited to 'Source')
-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()); |