diff options
author | Brad King <brad.king@kitware.com> | 2019-07-26 11:29:31 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2019-07-26 11:30:45 (GMT) |
commit | c23c4ed9cf912e265e1070507905b3dbc495a210 (patch) | |
tree | c6cdcac257f4c00f4c65158965fed8a52ff264d4 | |
parent | 9f23c91919945f8158ef5bc2512aefaa418de0da (diff) | |
parent | 4ff0bb054b65ac332fed3272afac8142599b6464 (diff) | |
download | CMake-c23c4ed9cf912e265e1070507905b3dbc495a210.zip CMake-c23c4ed9cf912e265e1070507905b3dbc495a210.tar.gz CMake-c23c4ed9cf912e265e1070507905b3dbc495a210.tar.bz2 |
Merge topic 'IsON_IsOFF_string_view'
4ff0bb054b cmSystemTools: Make IsInternallyOn, IsON and IsOff cm::string_view based
65d3ea2c7f cmAlgorithms: Make cmHasLiteral{Prefix,Suffix} cm::string_view based
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3598
-rw-r--r-- | Source/cmAlgorithms.h | 52 | ||||
-rw-r--r-- | Source/cmSourceFileLocation.cxx | 3 | ||||
-rw-r--r-- | Source/cmSystemTools.cxx | 160 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 38 |
4 files changed, 109 insertions, 144 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 <utility> #include <vector> -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 <typename T, size_t N> -bool cmHasLiteralPrefix(const T& str1, const char (&str2)[N]) -{ - return cmHasLiteralPrefixImpl(str1, str2, N - 1); -} - -template <typename T, size_t N> -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 <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. **/ 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 <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. **/ 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; } diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 723f280..e824757 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -282,115 +282,85 @@ void cmSystemTools::ReportLastSystemError(const char* msg) cmSystemTools::Error(m); } -bool cmSystemTools::IsInternallyOn(const char* val) -{ - if (!val) { - return false; - } - std::string v = val; - if (v.size() > 4) { - return false; - } - - for (char& c : v) { - c = static_cast<char>(toupper(c)); +bool cmSystemTools::IsInternallyOn(cm::string_view val) +{ + return (val.size() == 4) && // + (val[0] == 'I' || val[0] == 'i') && // + (val[1] == '_') && // + (val[2] == 'O' || val[2] == 'o') && // + (val[3] == 'N' || val[3] == 'n'); +} + +bool cmSystemTools::IsOn(cm::string_view val) +{ + switch (val.size()) { + case 1: + return val[0] == '1' || val[0] == 'Y' || val[0] == 'y'; + case 2: + return // + (val[0] == 'O' || val[0] == 'o') && // + (val[1] == 'N' || val[1] == 'n'); + case 3: + return // + (val[0] == 'Y' || val[0] == 'y') && // + (val[1] == 'E' || val[1] == 'e') && // + (val[2] == 'S' || val[2] == 's'); + case 4: + return // + (val[0] == 'T' || val[0] == 't') && // + (val[1] == 'R' || val[1] == 'r') && // + (val[2] == 'U' || val[2] == 'u') && // + (val[3] == 'E' || val[3] == 'e'); + default: + break; } - return v == "I_ON"; -} -bool cmSystemTools::IsOn(const char* val) -{ - if (!val) { - return false; - } - /* clang-format off */ - // "1" - if (val[0] == '1' && val[1] == '\0') { - return true; - } - // "ON" - if ((val[0] == 'O' || val[0] == 'o') && - (val[1] == 'N' || val[1] == 'n') && val[2] == '\0') { - return true; - } - // "Y", "YES" - if ((val[0] == 'Y' || val[0] == 'y') && (val[1] == '\0' || ( - (val[1] == 'E' || val[1] == 'e') && - (val[2] == 'S' || val[2] == 's') && val[3] == '\0'))) { - return true; - } - // "TRUE" - if ((val[0] == 'T' || val[0] == 't') && - (val[1] == 'R' || val[1] == 'r') && - (val[2] == 'U' || val[2] == 'u') && - (val[3] == 'E' || val[3] == 'e') && val[4] == '\0') { - return true; - } - /* clang-format on */ return false; } -bool cmSystemTools::IsOn(const std::string& val) +bool cmSystemTools::IsNOTFOUND(cm::string_view val) { - return cmSystemTools::IsOn(val.c_str()); + return (val == "NOTFOUND") || cmHasLiteralSuffix(val, "-NOTFOUND"); } -bool cmSystemTools::IsNOTFOUND(const char* val) +bool cmSystemTools::IsOff(cm::string_view val) { - if (strcmp(val, "NOTFOUND") == 0) { - return true; + switch (val.size()) { + case 0: + return true; + case 1: + return val[0] == '0' || val[0] == 'N' || val[0] == 'n'; + case 2: + return // + (val[0] == 'N' || val[0] == 'n') && // + (val[1] == 'O' || val[1] == 'o'); + case 3: + return // + (val[0] == 'O' || val[0] == 'o') && // + (val[1] == 'F' || val[1] == 'f') && // + (val[2] == 'F' || val[2] == 'f'); + case 5: + return // + (val[0] == 'F' || val[0] == 'f') && // + (val[1] == 'A' || val[1] == 'a') && // + (val[2] == 'L' || val[2] == 'l') && // + (val[3] == 'S' || val[3] == 's') && // + (val[4] == 'E' || val[4] == 'e'); + case 6: + return // + (val[0] == 'I' || val[0] == 'i') && // + (val[1] == 'G' || val[1] == 'g') && // + (val[2] == 'N' || val[2] == 'n') && // + (val[3] == 'O' || val[3] == 'o') && // + (val[4] == 'R' || val[4] == 'r') && // + (val[5] == 'E' || val[5] == 'e'); + default: + break; } - return cmHasLiteralSuffix(val, "-NOTFOUND"); -} -bool cmSystemTools::IsOff(const char* val) -{ - // "" - if (!val || val[0] == '\0') { - return true; - } - /* clang-format off */ - // "0" - if (val[0] == '0' && val[1] == '\0') { - return true; - } - // "OFF" - if ((val[0] == 'O' || val[0] == 'o') && - (val[1] == 'F' || val[1] == 'f') && - (val[2] == 'F' || val[2] == 'f') && val[3] == '\0') { - return true; - } - // "N", "NO" - if ((val[0] == 'N' || val[0] == 'n') && (val[1] == '\0' || ( - (val[1] == 'O' || val[1] == 'o') && val[2] == '\0'))) { - return true; - } - // "FALSE" - if ((val[0] == 'F' || val[0] == 'f') && - (val[1] == 'A' || val[1] == 'a') && - (val[2] == 'L' || val[2] == 'l') && - (val[3] == 'S' || val[3] == 's') && - (val[4] == 'E' || val[4] == 'e') && val[5] == '\0') { - return true; - } - // "IGNORE" - if ((val[0] == 'I' || val[0] == 'i') && - (val[1] == 'G' || val[1] == 'g') && - (val[2] == 'N' || val[2] == 'n') && - (val[3] == 'O' || val[3] == 'o') && - (val[4] == 'R' || val[4] == 'r') && - (val[5] == 'E' || val[5] == 'e') && val[6] == '\0') { - return true; - } - /* clang-format on */ return cmSystemTools::IsNOTFOUND(val); } -bool cmSystemTools::IsOff(const std::string& val) -{ - return cmSystemTools::IsOff(val.c_str()); -} - void cmSystemTools::ParseWindowsCommandLine(const char* command, std::vector<std::string>& args) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index a9c03bd..5ce6a48 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -8,6 +8,7 @@ #include "cmCryptoHash.h" #include "cmDuration.h" #include "cmProcessOutput.h" +#include "cm_string_view.hxx" #include "cmsys/Process.h" #include "cmsys/SystemTools.hxx" // IWYU pragma: export #include <functional> @@ -149,26 +150,45 @@ public: * forced this value. This is not the same as On, but this * may be considered as "internally switched on". */ - static bool IsInternallyOn(const char* val); + static bool IsInternallyOn(cm::string_view val); + static inline bool IsInternallyOn(const char* val) + { + if (!val) { + return false; + } + return IsInternallyOn(cm::string_view(val)); + } + /** - * does a string indicate a true or on value ? This is not the same - * as ifdef. + * Does a string indicate a true or on value? This is not the same as ifdef. */ - static bool IsOn(const char* val); - static bool IsOn(const std::string& val); + static bool IsOn(cm::string_view val); + inline static bool IsOn(const char* val) + { + if (!val) { + return false; + } + return IsOn(cm::string_view(val)); + } /** - * does a string indicate a false or off value ? Note that this is + * Does a string indicate a false or off value ? Note that this is * not the same as !IsOn(...) because there are a number of * ambiguous values such as "/usr/local/bin" a path will result in * IsON and IsOff both returning false. Note that the special path * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true. */ - static bool IsOff(const char* val); - static bool IsOff(const std::string& val); + static bool IsOff(cm::string_view val); + inline static bool IsOff(const char* val) + { + if (!val) { + return true; + } + return IsOff(cm::string_view(val)); + } //! Return true if value is NOTFOUND or ends in -NOTFOUND. - static bool IsNOTFOUND(const char* value); + static bool IsNOTFOUND(cm::string_view val); //! Return true if the path is a framework static bool IsPathToFramework(const std::string& value); |