diff options
Diffstat (limited to 'Source/cmStringAlgorithms.h')
-rw-r--r-- | Source/cmStringAlgorithms.h | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/Source/cmStringAlgorithms.h b/Source/cmStringAlgorithms.h index cdb494f..5b8b878 100644 --- a/Source/cmStringAlgorithms.h +++ b/Source/cmStringAlgorithms.h @@ -7,6 +7,7 @@ #include "cmRange.h" #include "cm_string_view.hxx" +#include <cctype> #include <initializer_list> #include <sstream> #include <string.h> @@ -31,6 +32,21 @@ private: std::string const Test_; }; +/** Returns true if the character @a ch is a whitespace character. **/ +inline bool cmIsSpace(char ch) +{ + return ((ch & 0x80) == 0) && std::isspace(ch); +} + +/** Returns a string that has whitespace removed from the start and the end. */ +std::string cmTrimWhitespace(cm::string_view str); + +/** Returns a string that has quotes removed from the start and the end. */ +std::string cmRemoveQuotes(cm::string_view str); + +/** Escape quotes in a string. */ +std::string cmEscapeQuotes(cm::string_view str); + /** Joins elements of a range with separator into a single string. */ template <typename Range> std::string cmJoin(Range const& rng, cm::string_view separator) @@ -49,6 +65,51 @@ std::string cmJoin(Range const& rng, cm::string_view separator) return os.str(); } +/** Extract tokens that are separated by any of the characters in @a sep. */ +std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep); + +/** + * Expand the ; separated string @a arg into multiple arguments. + * All found arguments are appended to @a argsOut. + */ +void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut, + bool emptyArgs = false); + +/** + * Expand out any arguments in the string range [@a first, @a last) that have + * ; separated strings into multiple arguments. All found arguments are + * appended to @a argsOut. + */ +template <class InputIt> +void cmExpandLists(InputIt first, InputIt last, + std::vector<std::string>& argsOut) +{ + for (; first != last; ++first) { + ExpandList(*first, argsOut); + } +} + +/** + * Same as cmExpandList but a new vector is created containing + * the expanded arguments from the string @a arg. + */ +std::vector<std::string> cmExpandedList(cm::string_view arg, + bool emptyArgs = false); + +/** + * Same as cmExpandList but a new vector is created containing the expanded + * versions of all arguments in the string range [@a first, @a last). + */ +template <class InputIt> +std::vector<std::string> cmExpandedLists(InputIt first, InputIt last) +{ + std::vector<std::string> argsOut; + for (; first != last; ++first) { + cmExpandList(*first, argsOut); + } + return argsOut; +} + /** Concatenate string pieces into a single string. */ std::string cmCatViews(std::initializer_list<cm::string_view> views); @@ -119,6 +180,51 @@ std::string cmWrap(char prefix, Range const& rng, char suffix, sep); } +/** + * Does a string indicates that CMake/CPack/CTest internally + * forced this value. This is not the same as On, but this + * may be considered as "internally switched on". + */ +bool cmIsInternallyOn(cm::string_view val); +inline bool cmIsInternallyOn(const char* val) +{ + if (!val) { + return false; + } + return cmIsInternallyOn(cm::string_view(val)); +} + +/** Return true if value is NOTFOUND or ends in -NOTFOUND. */ +bool cmIsNOTFOUND(cm::string_view val); + +/** + * Does a string indicate a true or ON value? This is not the same as ifdef. + */ +bool cmIsOn(cm::string_view val); +inline bool cmIsOn(const char* val) +{ + if (!val) { + return false; + } + return cmIsOn(cm::string_view(val)); +} + +/** + * 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. + */ +bool cmIsOff(cm::string_view val); +inline bool cmIsOff(const char* val) +{ + if (!val) { + return true; + } + return cmIsOff(cm::string_view(val)); +} + /** Returns true if string @a str starts with the character @a prefix. */ inline bool cmHasPrefix(cm::string_view str, char prefix) { @@ -174,4 +280,13 @@ inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix) } } +/** Converts a string to long. Expects that the whole string is an integer. */ +bool cmStrToLong(const char* str, long* value); +bool cmStrToLong(std::string const& str, long* value); + +/** Converts a string to unsigned long. Expects that the whole string is an + * integer */ +bool cmStrToULong(const char* str, unsigned long* value); +bool cmStrToULong(std::string const& str, unsigned long* value); + #endif |