cmStringAlgorithms.h 9.6 KB

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