From 65d3ea2c7f737c01b426d73f57167f5ad60c095c Mon Sep 17 00:00:00 2001 From: Sebastian Holtermann Date: Wed, 24 Jul 2019 11:40:39 +0200 Subject: cmAlgorithms: Make cmHasLiteral{Prefix,Suffix} cm::string_view based --- Source/cmAlgorithms.h | 52 +++++++++++------------------------------ Source/cmSourceFileLocation.cxx | 3 +-- 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index b4b480b..cf71052 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -19,44 +19,6 @@ #include #include -inline bool cmHasLiteralPrefixImpl(const std::string& str1, const char* str2, - size_t N) -{ - return strncmp(str1.c_str(), str2, N) == 0; -} - -inline bool cmHasLiteralPrefixImpl(const char* str1, const char* str2, - size_t N) -{ - return strncmp(str1, str2, N) == 0; -} - -inline bool cmHasLiteralSuffixImpl(const std::string& str1, const char* str2, - size_t N) -{ - size_t len = str1.size(); - return len >= N && strcmp(str1.c_str() + len - N, str2) == 0; -} - -inline bool cmHasLiteralSuffixImpl(const char* str1, const char* str2, - size_t N) -{ - size_t len = strlen(str1); - return len >= N && strcmp(str1 + len - N, str2) == 0; -} - -template -bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N]) -{ - return cmHasLiteralPrefixImpl(str1, str2, N - 1); -} - -template -bool cmHasLiteralSuffix(const T& str1, const char (&str2)[N]) -{ - return cmHasLiteralSuffixImpl(str1, str2, N - 1); -} - struct cmStrCmp { cmStrCmp(const char* test) @@ -327,6 +289,13 @@ 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. **/ +template +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. **/ inline bool cmHasSuffix(cm::string_view str, char suffix) { @@ -340,6 +309,13 @@ inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix) str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } +/** Returns true if string @a str ends with string @a suffix. **/ +template +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. **/ inline void cmStripSuffixIfExists(std::string& str, char suffix) { diff --git a/Source/cmSourceFileLocation.cxx b/Source/cmSourceFileLocation.cxx index acacba2..d887627 100644 --- a/Source/cmSourceFileLocation.cxx +++ b/Source/cmSourceFileLocation.cxx @@ -146,8 +146,7 @@ bool cmSourceFileLocation::MatchesAmbiguousExtension( // adding an extension. if (!(this->Name.size() > loc.Name.size() && this->Name[loc.Name.size()] == '.' && - cmHasLiteralPrefixImpl(this->Name.c_str(), loc.Name.c_str(), - loc.Name.size()))) { + cmHasPrefix(this->Name, loc.Name))) { return false; } -- cgit v0.12