cmStringAlgorithms.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmStringAlgorithms_h
  4. #define cmStringAlgorithms_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <cctype>
  7. #include <cstring>
  8. #include <initializer_list>
  9. #include <sstream>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/string_view>
  14. #include "cmRange.h"
  15. /** String range type. */
  16. using cmStringRange = cmRange<std::vector<std::string>::const_iterator>;
  17. /** Check for non-empty string. */
  18. inline bool cmNonempty(const char* str)
  19. {
  20. return str && *str;
  21. }
  22. inline bool cmNonempty(cm::string_view str)
  23. {
  24. return !str.empty();
  25. }
  26. inline bool cmNonempty(std::string const* str)
  27. {
  28. return str && !str->empty();
  29. }
  30. /** Callable string comparison struct. */
  31. struct cmStrCmp
  32. {
  33. cmStrCmp(std::string str)
  34. : Test_(std::move(str))
  35. {
  36. }
  37. bool operator()(cm::string_view sv) const { return Test_ == sv; }
  38. private:
  39. std::string const Test_;
  40. };
  41. /** Returns true if the character @a ch is a whitespace character. **/
  42. inline bool cmIsSpace(char ch)
  43. {
  44. return ((ch & 0x80) == 0) && std::isspace(ch);
  45. }
  46. /** Returns a string that has whitespace removed from the start and the end. */
  47. std::string cmTrimWhitespace(cm::string_view str);
  48. /** Returns a string that has quotes removed from the start and the end. */
  49. std::string cmRemoveQuotes(cm::string_view str);
  50. /** Escape quotes in a string. */
  51. std::string cmEscapeQuotes(cm::string_view str);
  52. /** Joins elements of a range with separator into a single string. */
  53. template <typename Range>
  54. std::string cmJoin(Range const& rng, cm::string_view separator)
  55. {
  56. if (rng.empty()) {
  57. return std::string();
  58. }
  59. std::ostringstream os;
  60. auto it = rng.begin();
  61. auto const end = rng.end();
  62. os << *it;
  63. while (++it != end) {
  64. os << separator << *it;
  65. }
  66. return os.str();
  67. }
  68. /** Extract tokens that are separated by any of the characters in @a sep. */
  69. std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep);
  70. /**
  71. * Expand the ; separated string @a arg into multiple arguments.
  72. * All found arguments are appended to @a argsOut.
  73. */
  74. void cmExpandList(cm::string_view arg, std::vector<std::string>& argsOut,
  75. bool emptyArgs = false);
  76. /**
  77. * Expand out any arguments in the string range [@a first, @a last) that have
  78. * ; separated strings into multiple arguments. All found arguments are
  79. * appended to @a argsOut.
  80. */
  81. template <class InputIt>
  82. void cmExpandLists(InputIt first, InputIt last,
  83. std::vector<std::string>& argsOut)
  84. {
  85. for (; first != last; ++first) {
  86. cmExpandList(*first, argsOut);
  87. }
  88. }
  89. /**
  90. * Same as cmExpandList but a new vector is created containing
  91. * the expanded arguments from the string @a arg.
  92. */
  93. std::vector<std::string> cmExpandedList(cm::string_view arg,
  94. bool emptyArgs = false);
  95. /**
  96. * Same as cmExpandList but a new vector is created containing the expanded
  97. * versions of all arguments in the string range [@a first, @a last).
  98. */
  99. template <class InputIt>
  100. std::vector<std::string> cmExpandedLists(InputIt first, InputIt last)
  101. {
  102. std::vector<std::string> argsOut;
  103. for (; first != last; ++first) {
  104. cmExpandList(*first, argsOut);
  105. }
  106. return argsOut;
  107. }
  108. /** Concatenate string pieces into a single string. */
  109. std::string cmCatViews(std::initializer_list<cm::string_view> views);
  110. /** Utility class for cmStrCat. */
  111. class cmAlphaNum
  112. {
  113. public:
  114. cmAlphaNum(cm::string_view view)
  115. : View_(view)
  116. {
  117. }
  118. cmAlphaNum(std::string const& str)
  119. : View_(str)
  120. {
  121. }
  122. cmAlphaNum(const char* str)
  123. : View_(str)
  124. {
  125. }
  126. cmAlphaNum(char ch)
  127. : View_(Digits_, 1)
  128. {
  129. Digits_[0] = ch;
  130. }
  131. cmAlphaNum(int val);
  132. cmAlphaNum(unsigned int val);
  133. cmAlphaNum(long int val);
  134. cmAlphaNum(unsigned long int val);
  135. cmAlphaNum(long long int val);
  136. cmAlphaNum(unsigned long long int val);
  137. cmAlphaNum(float val);
  138. cmAlphaNum(double val);
  139. cm::string_view View() const { return View_; }
  140. private:
  141. cm::string_view View_;
  142. char Digits_[32];
  143. };
  144. /** Concatenate string pieces and numbers into a single string. */
  145. template <typename... AV>
  146. inline std::string cmStrCat(cmAlphaNum const& a, cmAlphaNum const& b,
  147. AV const&... args)
  148. {
  149. return cmCatViews(
  150. { a.View(), b.View(), static_cast<cmAlphaNum const&>(args).View()... });
  151. }
  152. /** Joins wrapped elements of a range with separator into a single string. */
  153. template <typename Range>
  154. std::string cmWrap(cm::string_view prefix, Range const& rng,
  155. cm::string_view suffix, cm::string_view sep)
  156. {
  157. if (rng.empty()) {
  158. return std::string();
  159. }
  160. return cmCatViews(
  161. { prefix, cmJoin(rng, cmCatViews({ suffix, sep, prefix })), suffix });
  162. }
  163. /** Joins wrapped elements of a range with separator into a single string. */
  164. template <typename Range>
  165. std::string cmWrap(char prefix, Range const& rng, char suffix,
  166. cm::string_view sep)
  167. {
  168. return cmWrap(cm::string_view(&prefix, 1), rng, cm::string_view(&suffix, 1),
  169. sep);
  170. }
  171. /**
  172. * Does a string indicates that CMake/CPack/CTest internally
  173. * forced this value. This is not the same as On, but this
  174. * may be considered as "internally switched on".
  175. */
  176. bool cmIsInternallyOn(cm::string_view val);
  177. inline bool cmIsInternallyOn(const char* val)
  178. {
  179. if (!val) {
  180. return false;
  181. }
  182. return cmIsInternallyOn(cm::string_view(val));
  183. }
  184. /** Return true if value is NOTFOUND or ends in -NOTFOUND. */
  185. bool cmIsNOTFOUND(cm::string_view val);
  186. /**
  187. * Does a string indicate a true or ON value? This is not the same as ifdef.
  188. */
  189. bool cmIsOn(cm::string_view val);
  190. inline bool cmIsOn(const char* val)
  191. {
  192. return val && cmIsOn(cm::string_view(val));
  193. }
  194. inline bool cmIsOn(std::string const* val)
  195. {
  196. return val && cmIsOn(*val);
  197. }
  198. /**
  199. * Does a string indicate a false or off value ? Note that this is
  200. * not the same as !IsOn(...) because there are a number of
  201. * ambiguous values such as "/usr/local/bin" a path will result in
  202. * IsON and IsOff both returning false. Note that the special path
  203. * NOTFOUND, *-NOTFOUND or IGNORE will cause IsOff to return true.
  204. */
  205. bool cmIsOff(cm::string_view val);
  206. inline bool cmIsOff(const char* val)
  207. {
  208. return !val || cmIsOff(cm::string_view(val));
  209. }
  210. inline bool cmIsOff(std::string const* val)
  211. {
  212. return !val || cmIsOff(*val);
  213. }
  214. /** Returns true if string @a str starts with the character @a prefix. */
  215. inline bool cmHasPrefix(cm::string_view str, char prefix)
  216. {
  217. return !str.empty() && (str.front() == prefix);
  218. }
  219. /** Returns true if string @a str starts with string @a prefix. */
  220. inline bool cmHasPrefix(cm::string_view str, cm::string_view prefix)
  221. {
  222. return str.compare(0, prefix.size(), prefix) == 0;
  223. }
  224. /** Returns true if string @a str starts with string @a prefix. */
  225. template <size_t N>
  226. inline bool cmHasLiteralPrefix(cm::string_view str, const char (&prefix)[N])
  227. {
  228. return cmHasPrefix(str, cm::string_view(prefix, N - 1));
  229. }
  230. /** Returns true if string @a str ends with the character @a suffix. */
  231. inline bool cmHasSuffix(cm::string_view str, char suffix)
  232. {
  233. return !str.empty() && (str.back() == suffix);
  234. }
  235. /** Returns true if string @a str ends with string @a suffix. */
  236. inline bool cmHasSuffix(cm::string_view str, cm::string_view suffix)
  237. {
  238. return str.size() >= suffix.size() &&
  239. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
  240. }
  241. /** Returns true if string @a str ends with string @a suffix. */
  242. template <size_t N>
  243. inline bool cmHasLiteralSuffix(cm::string_view str, const char (&suffix)[N])
  244. {
  245. return cmHasSuffix(str, cm::string_view(suffix, N - 1));
  246. }
  247. /** Removes an existing suffix character of from the string @a str. */
  248. inline void cmStripSuffixIfExists(std::string& str, char suffix)
  249. {
  250. if (cmHasSuffix(str, suffix)) {
  251. str.pop_back();
  252. }
  253. }
  254. /** Removes an existing suffix string of from the string @a str. */
  255. inline void cmStripSuffixIfExists(std::string& str, cm::string_view suffix)
  256. {
  257. if (cmHasSuffix(str, suffix)) {
  258. str.resize(str.size() - suffix.size());
  259. }
  260. }
  261. /** Converts a string to long. Expects that the whole string is an integer. */
  262. bool cmStrToLong(const char* str, long* value);
  263. bool cmStrToLong(std::string const& str, long* value);
  264. /** Converts a string to unsigned long. Expects that the whole string is an
  265. * integer */
  266. bool cmStrToULong(const char* str, unsigned long* value);
  267. bool cmStrToULong(std::string const& str, unsigned long* value);
  268. #endif